mirror of https://github.com/hashicorp/boundary
refactor(bsr): optimize ChunkEncoder.Encode() (#3476)
parent
1f6e000357
commit
a67e42b00f
@ -0,0 +1,89 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package bsr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type testChunk struct {
|
||||
*BaseChunk
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// MarshalData serializes the data portion of a chunk.
|
||||
func (t *testChunk) MarshalData(_ context.Context) ([]byte, error) {
|
||||
return t.Data, nil
|
||||
}
|
||||
|
||||
func newTestChunk(d int) *testChunk {
|
||||
data := make([]byte, d)
|
||||
ts := time.Date(2023, time.March, 16, 10, 47, 3, 14, time.UTC)
|
||||
return &testChunk{
|
||||
BaseChunk: &BaseChunk{
|
||||
Protocol: "TEST",
|
||||
Direction: Inbound,
|
||||
Timestamp: NewTimestamp(ts),
|
||||
Type: "TEST",
|
||||
},
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkEncodeParallel(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
cases := []int{16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}
|
||||
for _, chunkSize := range cases {
|
||||
b.StopTimer()
|
||||
ctx := context.Background()
|
||||
chunks := make([]Chunk, 250)
|
||||
for i := range chunks {
|
||||
chunks[i] = newTestChunk(chunkSize)
|
||||
}
|
||||
b.StartTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var buf bytes.Buffer
|
||||
enc, _ := NewChunkEncoder(ctx, &buf, NoCompression, NoEncryption)
|
||||
for _, c := range chunks {
|
||||
if _, err := enc.Encode(ctx, c); err != nil {
|
||||
b.Fatal("Encode:", err)
|
||||
}
|
||||
}
|
||||
b.SetBytes(int64(len(buf.Bytes())))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkEncodeSequential(b *testing.B) {
|
||||
cases := []int{16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}
|
||||
for _, chunkSize := range cases {
|
||||
b.Run(fmt.Sprintf("%d", chunkSize), func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.StopTimer()
|
||||
ctx := context.Background()
|
||||
chunks := make([]Chunk, 250)
|
||||
for i := range chunks {
|
||||
chunks[i] = newTestChunk(chunkSize)
|
||||
}
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
var buf bytes.Buffer
|
||||
enc, _ := NewChunkEncoder(ctx, &buf, NoCompression, NoEncryption)
|
||||
for _, c := range chunks {
|
||||
if _, err := enc.Encode(ctx, c); err != nil {
|
||||
b.Fatal("Encode:", err)
|
||||
}
|
||||
}
|
||||
b.SetBytes(int64(len(buf.Bytes())))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue