Skip to content

Instantly share code, notes, and snippets.

@benjirewis
Last active September 15, 2022 20:05
Show Gist options
  • Save benjirewis/8b088d422788a4beceeb5076652db256 to your computer and use it in GitHub Desktop.
Save benjirewis/8b088d422788a4beceeb5076652db256 to your computer and use it in GitHub Desktop.
Replace Errors Benchmark
package main
import (
"benjirewis/go-bootstrap/investigations/util"
"context"
"log"
"testing"
"go.mongodb.org/mongo-driver/bson"
)
func BenchmarkReplaceErrors(b *testing.B) {
ctx := context.TODO()
cli, disc := util.GetClient()
defer func() {
if err := disc(ctx); err != nil {
log.Fatal(err)
}
}()
// Call Watch to create a new change stream.
cs, err := cli.Watch(ctx, bson.D{})
if err != nil {
log.Fatal(err)
}
// Call cs.Err() repeatedly to call replaceErrors() on nil.
// See the call to replaceErrors on a potentially nil error here:
// https://github.com/mongodb/mongo-go-driver/blob/master/mongo/change_stream.go#L541
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := cs.Err()
if err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment