func BenchmarkMicroServiceCall(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
c := client.NewClient()
aa := proto.NewArticleAppClient("qor.article", c)
ctx := metadata.NewContext(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})
art, err := aa.Get(ctx, &proto.ArticleGetInput{Id: "Hello"})
if err != nil {
fmt.Println(err)
return
}
_ = art
// fmt.Println("article: ", art)
}
}
Reuse objects
func BenchmarkMicroServiceCall_WithLessObjects(b *testing.B) {
c := client.NewClient()
aa := proto.NewArticleAppClient("qor.article", c)
ctx := metadata.NewContext(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
art, err := aa.Get(ctx, &proto.ArticleGetInput{Id: "Hello"})
if err != nil {
fmt.Println(err)
return
}
_ = art
// fmt.Println("article: ", art)
}
}
Results are counter-intuitive
Felix:tests sunfmin$ go test -bench=.
testing: warning: no tests to run
PASS
BenchmarkMicroServiceCall-8 1000 2130049 ns/op
BenchmarkMicroServiceCallMinObjects-8 500 3059991 ns/op
ok github.com/sunfmin/article/tests 5.590s
With everything created for each run
Reuse objects
Results are counter-intuitive