sunfmin / notebook

My Note Book
2 stars 0 forks source link

Micro Service Call are even slower with less objects created #7

Open sunfmin opened 8 years ago

sunfmin commented 8 years ago

With everything created for each run

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