Skip to content

Commit

Permalink
protobuf: add marshaling benchmarks for some protobuf messages
Browse files Browse the repository at this point in the history
The benchmarks that are added are related to certain areas that are
considered hotspots for performance where many messages are marshaled.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
  • Loading branch information
jsternberg committed Sep 24, 2024
1 parent 856ae24 commit 89b9515
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
118 changes: 118 additions & 0 deletions api/services/control/control_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package moby_buildkit_v1 //nolint:revive

import (
"testing"
"time"

digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/require"
)

// Buf is used to prevent the benchmark from being optimized away.
var Buf []byte

func BenchmarkMarshalVertex(b *testing.B) {
v := sampleVertex()
for i := 0; i < b.N; i++ {
var err error
Buf, err = v.Marshal()
require.NoError(b, err)
}
}

func BenchmarkMarshalVertexStatus(b *testing.B) {
v := sampleVertexStatus()
for i := 0; i < b.N; i++ {
var err error
Buf, err = v.Marshal()
require.NoError(b, err)
}
}

func BenchmarkMarshalVertexLog(b *testing.B) {
v := sampleVertexLog()
for i := 0; i < b.N; i++ {
var err error
Buf, err = v.Marshal()
require.NoError(b, err)
}
}

var VertexOutput Vertex

func BenchmarkUnmarshalVertex(b *testing.B) {
v := sampleVertex()
buf, err := v.Marshal()
require.NoError(b, err)

for i := 0; i < b.N; i++ {
err := VertexOutput.Unmarshal(buf)
require.NoError(b, err)
}
}

var VertexStatusOutput VertexStatus

func BenchmarkUnmarshalVertexStatus(b *testing.B) {
v := sampleVertexStatus()
buf, err := v.Marshal()
require.NoError(b, err)

for i := 0; i < b.N; i++ {
err := VertexStatusOutput.Unmarshal(buf)
require.NoError(b, err)
}
}

var VertexLogOutput VertexLog

func BenchmarkUnmarshalVertexLog(b *testing.B) {
v := sampleVertexLog()
buf, err := v.Marshal()
require.NoError(b, err)

for i := 0; i < b.N; i++ {
err := VertexLogOutput.Unmarshal(buf)
require.NoError(b, err)
}
}

func sampleVertex() *Vertex {
now := time.Now()
started := now.Add(-time.Minute)
return &Vertex{
Digest: digest.FromString("abc"),
Inputs: []digest.Digest{
digest.FromString("dep1"),
digest.FromString("dep2"),
},
Name: "abc",
Started: &started,
Completed: &now,
}
}

func sampleVertexStatus() *VertexStatus {
now := time.Now()
started := now.Add(-time.Minute)
return &VertexStatus{
ID: "abc",
Vertex: digest.FromString("abc"),
Name: "abc",
Current: 1024,
Total: 1024,
Timestamp: now,
Started: &started,
Completed: &now,
}
}

func sampleVertexLog() *VertexLog {
now := time.Now()
return &VertexLog{
Vertex: digest.FromString("abc"),
Timestamp: now,
Stream: 1,
Msg: []byte("this is a log message"),
}
}
69 changes: 69 additions & 0 deletions cache/contenthash/checksum_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package contenthash

import (
"testing"

digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/require"
)

// Buf is used to prevent the benchmark from being optimized away.
var Buf []byte

func BenchmarkMarshalCacheRecords(b *testing.B) {
v := sampleCacheRecords()
for i := 0; i < b.N; i++ {
var err error
Buf, err = v.Marshal()
require.NoError(b, err)
}
}

var CacheRecordsOutput CacheRecords

func BenchmarkUnmarshalCacheRecords(b *testing.B) {
v := sampleCacheRecords()
buf, err := v.Marshal()
require.NoError(b, err)

for i := 0; i < b.N; i++ {
err := CacheRecordsOutput.Unmarshal(buf)
require.NoError(b, err)
}
}

func sampleCacheRecords() *CacheRecords {
return &CacheRecords{
Paths: []*CacheRecordWithPath{
{
Path: "/foo",
Record: &CacheRecord{
Digest: digest.FromString("/foo"),
Type: CacheRecordTypeDir,
},
},
{
Path: "/foo/",
Record: &CacheRecord{
Digest: digest.FromString("/foo/"),
Type: CacheRecordTypeDirHeader,
},
},
{
Path: "/foo/bar.txt",
Record: &CacheRecord{
Digest: digest.FromString("/foo/bar.txt"),
Type: CacheRecordTypeFile,
},
},
{
Path: "/foo/link",
Record: &CacheRecord{
Digest: digest.FromString("/foo/link"),
Type: CacheRecordTypeSymlink,
Linkname: "/foo/bar.txt",
},
},
},
}
}

0 comments on commit 89b9515

Please sign in to comment.