-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bf0400
commit de0eba8
Showing
4 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package bson_test | ||
|
||
import ( | ||
"testing" | ||
|
||
cristalhq "github.com/cristalhq/bson" | ||
globalsign "github.com/globalsign/mgo/bson" | ||
gomicro "github.com/go-micro/go-bson" | ||
mongo_driver "go.mongodb.org/mongo-driver/bson" | ||
mgo "gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
var marshalBenchData = testStruct{ | ||
Announce: "udp://tracker.publicbt.com:80/announce", | ||
AnnounceList: []any{ | ||
"udp://tracker.publicbt.com:80/announce", | ||
"udp://tracker.openbittorrent.com:80/announce", | ||
"udp://tracker.openbittorrent.com:80/announce", | ||
"udp://tracker.openbittorrent.com:80/announce", | ||
}, | ||
Comment: "Debian CD from cdimage.debian.org", | ||
Info: map[string]any{ | ||
"name": "debian-8.8.0-arm64-netinst.iso", | ||
"length": int64(170917888), | ||
"piece length": int64(262144), | ||
}, | ||
} | ||
|
||
// var marshalBenchData = map[string]any{ | ||
// "announce": "udp://tracker.publicbt.com:80/announce", | ||
// "announce-list": []any{ | ||
// "udp://tracker.publicbt.com:80/announce", | ||
// "udp://tracker.openbittorrent.com:80/announce", | ||
// "udp://tracker.openbittorrent.com:80/announce", | ||
// "udp://tracker.openbittorrent.com:80/announce", | ||
// }, | ||
// "comment": "Debian CD from cdimage.debian.org", | ||
// "info": map[string]any{ | ||
// "name": "debian-8.8.0-arm64-netinst.iso", | ||
// "length": int64(170917888), | ||
// "piece length": int64(262144), | ||
// }, | ||
// } | ||
|
||
type testStruct struct { | ||
Announce string | ||
AnnounceList []any | ||
Comment string | ||
Info map[string]any | ||
} | ||
|
||
func Benchmark_cristalhq_Marshal(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
var count int64 | ||
for n := 0; n < b.N; n++ { | ||
buf, err := cristalhq.Marshal(marshalBenchData) | ||
if err != nil { | ||
panic(err) | ||
} | ||
count += int64(len(buf)) | ||
} | ||
sink(b, count) | ||
} | ||
|
||
func Benchmark_cristalhq_MarshalTo(b *testing.B) { | ||
buf := make([]byte, 0, 512) | ||
b.ReportAllocs() | ||
|
||
var count int64 | ||
for n := 0; n < b.N; n++ { | ||
buf, err := cristalhq.MarshalTo(buf, marshalBenchData) | ||
if err != nil { | ||
panic(err) | ||
} | ||
count += int64(len(buf)) | ||
} | ||
sink(b, count) | ||
} | ||
|
||
func Benchmark_mongo_driver_Marshal(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
var count int64 | ||
for n := 0; n < b.N; n++ { | ||
buf, err := mongo_driver.Marshal(marshalBenchData) | ||
mustOk(b, err) | ||
count += int64(len(buf)) | ||
} | ||
sink(b, count) | ||
} | ||
|
||
func Benchmark_mgo_Marshal(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
var count int64 | ||
for n := 0; n < b.N; n++ { | ||
buf, err := mgo.Marshal(marshalBenchData) | ||
mustOk(b, err) | ||
count += int64(len(buf)) | ||
} | ||
sink(b, count) | ||
} | ||
|
||
func Benchmark_globalsign_Marshal(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
var count int64 | ||
for n := 0; n < b.N; n++ { | ||
buf, err := globalsign.Marshal(marshalBenchData) | ||
mustOk(b, err) | ||
count += int64(len(buf)) | ||
} | ||
sink(b, count) | ||
} | ||
|
||
func Benchmark_gomicro_Marshal(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
var count int64 | ||
for n := 0; n < b.N; n++ { | ||
buf, err := gomicro.Marshal(marshalBenchData) | ||
mustOk(b, err) | ||
count += int64(len(buf)) | ||
} | ||
sink(b, count) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package bson_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
func mustOk(tb testing.TB, err error) { | ||
tb.Helper() | ||
|
||
if err != nil { | ||
tb.Fatal(err) | ||
} | ||
} | ||
|
||
func sink[T any](tb testing.TB, v T) { | ||
if rand.Float32() > 2 { | ||
tb.Fatal(v) | ||
} | ||
} |
This file contains 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
This file contains 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