Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created March 23, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/97ba2d213e0ab6e845bde05bc828d8e7 to your computer and use it in GitHub Desktop.
Save Raynos/97ba2d213e0ab6e845bde05bc828d8e7 to your computer and use it in GitHub Desktop.
raynos at raynos-ThinkPad-T440p  
~/gocode/src/github.com/uber/zanzibar on master*
$ go test -benchmem -cpu 2 -bench . ./wut_test.go 
testing: warning: no tests to run
BenchmarkClosure-2   	10000000	       113 ns/op	      64 B/op	       2 allocs/op
BenchmarkStruct-2    	10000000	       129 ns/op	      64 B/op	       2 allocs/op
PASS
ok  	command-line-arguments	2.684s
package fixGlide_test
import (
"context"
"testing"
)
type Channel struct {
}
type Retrier func(ctx context.Context) error
func (ch *Channel) RunWithRetry(ctx context.Context, retrier Retrier) {
retrier(ctx)
}
type CallOptions struct {
Format string
}
type Call struct {
arg1 string
headers map[string]string
opts CallOptions
}
type Client struct {
ch *Channel
currentCall *Call
}
func (c *Client) DoStuff(
ctx context.Context,
headers map[string]string,
thriftService, methodName string,
) (bool, error) {
c.ch.RunWithRetry(ctx, func(ctx context.Context) error {
arg1 := thriftService + "::" + methodName
call := &Call{
arg1: arg1,
headers: headers,
opts: CallOptions{
Format: "thrift",
},
}
c.currentCall = call
return nil
})
return true, nil
}
type RetrierObj struct {
thriftService string
methodName string
headers map[string]string
c *Client
}
func (r RetrierObj) MakeCall(ctx context.Context) error {
arg1 := r.thriftService + "::" + r.methodName
call := &Call{
arg1: arg1,
headers: r.headers,
opts: CallOptions{
Format: "thrift",
},
}
r.c.currentCall = call
return nil
}
func (c *Client) DoStuffStruct(
ctx context.Context,
headers map[string]string,
thriftService, methodName string,
) (bool, error) {
r := RetrierObj{
thriftService: thriftService,
methodName: methodName,
headers: headers,
c: c,
}
c.ch.RunWithRetry(ctx, r.MakeCall)
return true, nil
}
func BenchmarkClosure(b *testing.B) {
client := &Client{
ch: &Channel{},
}
headers := map[string]string{
"key1": "key2",
"key3": "key4",
}
thriftService := "FooBar"
methodName := "DoStuff"
ctx := context.Background()
for i := 0; i < b.N; i++ {
client.DoStuff(ctx, headers, thriftService, methodName)
}
}
func BenchmarkStruct(b *testing.B) {
client := &Client{
ch: &Channel{},
}
headers := map[string]string{
"key1": "key2",
"key3": "key4",
}
thriftService := "FooBar"
methodName := "DoStuff"
ctx := context.Background()
for i := 0; i < b.N; i++ {
client.DoStuffStruct(ctx, headers, thriftService, methodName)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment