Last active
July 15, 2025 04:50
-
-
Save prestonvasquez/77eecc18699736b3d7d62a7a30c5769e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bson_test | |
import ( | |
"log" | |
"testing" | |
bson1 "go.mongodb.org/mongo-driver/bson" | |
bson2 "go.mongodb.org/mongo-driver/v2/bson" | |
) | |
// BenchmarkBSONv1vs2Comparison runs both v1 and v2 benchmarks for direct comparison | |
func BenchmarkBSONv1vs2Comparison(b *testing.B) { | |
type BusinessUsecaseData struct { | |
// Primitive types | |
IntType int | |
Int8Type int8 | |
Int16Type int16 | |
Int32Type int32 | |
Int64Type int64 | |
UintType uint | |
Uint8Type uint8 | |
Uint16Type uint16 | |
Uint32Type uint32 | |
Uint64Type uint64 | |
Float32Type float32 | |
Float64Type float64 | |
BoolType bool | |
StringType string | |
ByteType byte // Alias for uint8 | |
RuneType rune // Alias for int32 | |
// Composite types | |
ArrayType [3]int | |
SliceType []string | |
MapType map[string]int | |
StructType struct{ A int } | |
// Reference types | |
PointerType *int | |
InterfaceType interface{} | |
// Others | |
EmptyStruct struct{} | |
} | |
i := 42 | |
originalData := BusinessUsecaseData{ | |
IntType: 1, | |
Int8Type: 2, | |
Int16Type: 3, | |
Int32Type: 4, | |
Int64Type: 5, | |
UintType: 6, | |
Uint8Type: 7, | |
Uint16Type: 8, | |
Uint32Type: 9, | |
Uint64Type: 10, | |
Float32Type: 12.34, | |
Float64Type: 56.78, | |
BoolType: true, | |
StringType: "hello", | |
ByteType: 'a', | |
RuneType: '字', | |
ArrayType: [3]int{1, 2, 3}, | |
SliceType: []string{"a", "b"}, | |
MapType: map[string]int{"key": 1}, | |
StructType: struct{ A int }{A: 10}, | |
PointerType: &i, | |
InterfaceType: 100, | |
EmptyStruct: struct{}{}, | |
} | |
// Marshal data using BSON v1 | |
var err error | |
BSONV1Data, err := bson1.Marshal(originalData) | |
if err != nil { | |
log.Fatalf("Failed to marshal data using BSON v1: %v", err) | |
} | |
// Marshal data using BSON v2 | |
BSONV2Data, err := bson2.Marshal(originalData) | |
if err != nil { | |
log.Fatalf("Failed to marshal data using BSON v2: %v", err) | |
} | |
b.Run("BSON_v1", func(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
var result BusinessUsecaseData | |
err := bson1.Unmarshal(BSONV1Data, &result) | |
if err != nil { | |
b.Fatal(err) | |
} | |
} | |
}) | |
b.Run("BSON_v2", func(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
var result BusinessUsecaseData | |
err := bson2.Unmarshal(BSONV2Data, &result) | |
if err != nil { | |
b.Fatal(err) | |
} | |
} | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!bin/bash | |
set -ex | |
# compile the benchmark test | |
go test -c -o bench.test | |
# Profile BSON v1 | |
./bench.test \ | |
-test.bench='^BenchmarkBSONv1vs2Comparison/BSON_v1$' \ | |
-test.cpuprofile=cpu_v1.prof \ | |
-test.benchmem | |
# Profile BSON v2 | |
./bench.test \ | |
-test.bench='^BenchmarkBSONv1vs2Comparison/BSON_v2$' \ | |
-test.cpuprofile=cpu_v2.prof \ | |
-test.benchmem | |
# Use go tools to open browser with comparison | |
go tool pprof \ | |
-http="localhost:8080" \ | |
-diff_base=cpu_v1.prof \ | |
bench.test cpu_v2.prof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment