func BenchmarkCheckRetryClosesBody(t *testing.B) { count := 0 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { count++ if count%3 == 0 { w.WriteHeader(http.StatusOK) return } w.Header().Set("Retry-After", "0") w.WriteHeader(apierrors.StatusTooManyRequests) })) defer testServer.Close() c := NewOrDie(&Config{Host: testServer.URL, Version: testapi.Version(), Username: "******", Password: "******"}) r := c.Verb("POST"). Prefix("foo", "bar"). Suffix("baz"). Timeout(time.Second). Body([]byte(strings.Repeat("abcd", 1000))) for i := 0; i < t.N; i++ { if _, err := r.DoRaw(); err != nil { t.Fatalf("Unexpected error: %v %#v", err, err) } } }
func BenchmarkServerHijack(b *testing.B) { clientsCount := 1000 requestsPerConn := 10000 ch := make(chan struct{}, b.N) responseBody := []byte("123") s := &Server{ Handler: func(ctx *RequestCtx) { ctx.Hijack(func(c net.Conn) { // emulate server loop :) err := ServeConn(c, func(ctx *RequestCtx) { ctx.Success("foobar", responseBody) registerServedRequest(b, ch) }) if err != nil { b.Fatalf("error when serving connection") } }) ctx.Success("foobar", responseBody) registerServedRequest(b, ch) }, Concurrency: 16 * clientsCount, } req := "GET /foo HTTP/1.1\r\nHost: google.com\r\n\r\n" benchmarkServer(b, s, clientsCount, requestsPerConn, req) verifyRequestsServed(b, ch) }
// Benchmark utility functions for ObjectLayer.PutObject(). // Creates Object layer setup ( MakeBucket ) and then runs the PutObject benchmark. func runPutObjectBenchmark(b *testing.B, obj ObjectLayer, objSize int) { var err error // obtains random bucket name. bucket := getRandomBucketName() // create bucket. err = obj.MakeBucket(bucket) if err != nil { b.Fatal(err) } // get text data generated for number of bytes equal to object size. textData := generateBytesData(objSize) // generate md5sum for the generated data. // md5sum of the data to written is required as input for PutObject. metadata := make(map[string]string) metadata["md5Sum"] = getMD5Hash(textData) sha256sum := "" // benchmark utility which helps obtain number of allocations and bytes allocated per ops. b.ReportAllocs() // the actual benchmark for PutObject starts here. Reset the benchmark timer. b.ResetTimer() for i := 0; i < b.N; i++ { // insert the object. objInfo, err := obj.PutObject(bucket, "object"+strconv.Itoa(i), int64(len(textData)), bytes.NewBuffer(textData), metadata, sha256sum) if err != nil { b.Fatal(err) } if objInfo.MD5Sum != metadata["md5Sum"] { b.Fatalf("Write no: %d: Md5Sum mismatch during object write into the bucket: Expected %s, got %s", i+1, objInfo.MD5Sum, metadata["md5Sum"]) } } // Benchmark ends here. Stop timer. b.StopTimer() }
func BenchmarkPublishJsonStruct(b *testing.B) { // stop benchmark for set-up b.StopTimer() s := test.RunDefaultServer() defer s.Shutdown() ec := NewJsonEncodedConn(b) defer ec.Close() ch := make(chan bool) me := &person{Name: "derek", Age: 22, Address: "140 New Montgomery St"} me.Children = make(map[string]*person) me.Children["sam"] = &person{Name: "sam", Age: 19, Address: "140 New Montgomery St"} me.Children["meg"] = &person{Name: "meg", Age: 17, Address: "140 New Montgomery St"} ec.Subscribe("json_struct", func(p *person) { if !reflect.DeepEqual(p, me) { b.Fatalf("Did not receive the correct struct response") } ch <- true }) // resume benchmark b.StartTimer() for n := 0; n < b.N; n++ { ec.Publish("json_struct", me) if e := test.Wait(ch); e != nil { b.Fatal("Did not receive the message") } } }
// BenchmarkDecodeNotFound performs a benchmark on how long it takes to decode // a notfound message with the maximum number of entries. func BenchmarkDecodeNotFound(b *testing.B) { // Create a message with the maximum number of entries. pver := ProtocolVersion var m MsgNotFound for i := 0; i < MaxInvPerMsg; i++ { hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i)) if err != nil { b.Fatalf("NewHashFromStr: unexpected error: %v", err) } m.AddInvVect(NewInvVect(InvTypeBlock, hash)) } // Serialize it so the bytes are available to test the decode below. var bb bytes.Buffer if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil { b.Fatalf("MsgNotFound.BtcEncode: unexpected error: %v", err) } buf := bb.Bytes() r := bytes.NewReader(buf) var msg MsgNotFound b.ResetTimer() for i := 0; i < b.N; i++ { r.Seek(0, 0) msg.BtcDecode(r, pver, LatestEncoding) } }
func BenchmarkWriteBoom(b *testing.B) { if *file == "" { b.Skip("no file specified") } b.StopTimer() br, err := boom.OpenBAM(*file) if err != nil { b.Fatalf("Open failed: %v", err) } f, err := os.OpenFile("/dev/null", os.O_APPEND|os.O_RDWR, 0666) if err != nil { b.Fatalf("Open failed: %v", err) } bw, err := boom.OpenBAMFile(f, "bw", br.Header()) if err != nil { b.Fatalf("NewWriter failed: %v", err) } r, _, err := br.Read() if err != nil { b.Fatalf("Read failed: %v", err) } b.StartTimer() for i := 0; i < b.N; i++ { _, err = bw.Write(r) if err != nil { b.Fatalf("Write failed: %v", err) } } br.Close() f.Close() }
func BenchmarkFilterLines(b *testing.B) { pattern := "sdk/*/cpp/*/*vars.html" lines := extractTestLines(b) var c uint b.ResetTimer() for i := 0; i < b.N; i++ { c = 0 for _, line := range lines { match, err := filter.Match(pattern, line) if err != nil { b.Fatal(err) } if match { c++ } } if c != 3 { b.Fatalf("wrong number of matches: expected 3, got %d", c) } } }
func BenchmarkReceiveRequestNoBuf(b *testing.B) { req := MCRequest{ Opcode: SET, Cas: 0, Opaque: 7242, VBucket: 824, Extras: []byte{1}, Key: []byte("somekey"), Body: []byte("somevalue"), } data := req.Bytes() data[0] = REQ_MAGIC rdr := bytes.NewReader(data) b.SetBytes(int64(len(data))) b.ResetTimer() for i := 0; i < b.N; i++ { req2 := MCRequest{} rdr.Seek(0, 0) _, err := req2.Receive(rdr, nil) if err != nil { b.Fatalf("Error receiving: %v", err) } } }
// This benchmark is mostly useful for very manual inspection and // debugging, so it's off by default. func BenchmarkInsertions(b *testing.B) { var prefixes []*net.IPNet for _, l := range []int{32, 24, 16, 8} { for n := 0; n < 1000; n++ { b := l / 8 ip := make([]byte, 4) for i := 0; i < b; i++ { ip[i] = byte(rand.Int()) } prefixes = append(prefixes, &net.IPNet{net.IP(ip), net.CIDRMask(l, 32)}) } } b.ResetTimer() for n := 0; n < b.N; n++ { db, err := New(":memory:") if err != nil { b.Fatal("Cannot create in-memory DB:", err) } r := db.Realm("prod") if err = r.Create(); err != nil { b.Fatalf("Creating realm: %s", err) } for _, p := range prefixes { r.Prefix(p).Create() } db.Close() } }
func BenchmarkUsersProd(b *testing.B) { prodfile := "./prods/users.prod" text, err := ioutil.ReadFile(prodfile) if err != nil { b.Fatal(err) } seed := uint64(time.Now().UnixNano()) s := parsec.NewScanner(text) root, _ := Y(s) scope := root.(common.Scope) nterms := scope["_nonterminals"].(common.NTForms) scope = BuildContext(scope, seed, "./bags", prodfile) b.ResetTimer() out := 0 for i := 0; i < b.N; i++ { scope = scope.RebuildContext() if val, ok := EvalForms("root", scope, nterms["s"]).(string); !ok { b.Fatalf("not string `%v`\n", val) } else { out += len(val) } } b.SetBytes(int64(float64(out) / float64(b.N))) }
func BenchmarkDecodeStruct(b *testing.B) { type s1 struct { Atom types.Atom priv0 int Uint8 uint8 Uint16 uint16 Uint32 uint32 priv1 string Byte byte Int int priv2 *s1 List []s1 Binary []byte } data := []byte{ 131, 104, 8, 100, 0, 12, 116, 104, 105, 115, 32, 105, 115, 32, 97, 116, 111, 109, 97, 255, 98, 0, 0, 255, 255, 110, 4, 0, 255, 255, 255, 255, 97, 128, 98, 255, 255, 253, 102, 106, 107, 0, 5, 1, 2, 3, 4, 5, } in := bytes.NewBuffer(data) var v s1 err := Decode(in, &v) if err != nil { b.Fatal(err) } else if l := in.Len(); l != 0 { b.Fatalf("buffer len %d", l) } for i := 0; i < b.N; i++ { in = bytes.NewBuffer(data) err = Decode(in, &v) } }
func benchmarkEndToEnd(dial func() (*Client, error), b *testing.B) { once.Do(startServer) client, err := dial() if err != nil { b.Fatal("error dialing:", err) } defer client.Close() // Synchronous calls args := &Args{7, 8} b.ResetTimer() b.RunParallel(func(pb *testing.PB) { reply := new(Reply) for pb.Next() { err := client.Call("Arith.Add", args, reply) if err != nil { b.Fatalf("rpc error: Add: expected no error but got string %q", err.Error()) } if reply.C != args.A+args.B { b.Fatalf("rpc error: Add: expected %d got %d", reply.C, args.A+args.B) } } }) }
func BenchmarkInsertRecursive(b *testing.B) { b.StopTimer() // Add 1k nodes to the tree. const count = 1000 nodes := make([]*remoteNode, 0, count) for i := 0; i < count; i++ { rId := make([]byte, 4) if _, err := rand.Read(rId); err != nil { b.Fatal("Couldnt produce random numbers for FindClosest:", err) } id := string(rId) + ffff if len(id) != 20 { b.Fatalf("Random infohash construction error, wrong len: want %d, got %d", 20, len(id)) } nodes = append(nodes, &remoteNode{id: id}) } b.StartTimer() // Each op is adding 1000 nodes to the tree. for i := 0; i < b.N; i++ { tree := &nTree{} for _, r := range nodes { tree.insert(r) } } }
func BenchmarkLargeQuery(b *testing.B) { intervals := IntervalSlice{ {4, 15, "First"}, {50, 72, "Second"}, {34, 90, "Third"}, {34, 45, "Fourth"}, {34, 40, "Fifth"}, {34, 34, "Sixth"}, {34, 45, "Seventh"}, } // Make a result for every q for i := 0; i < 65535; i++ { intervals = append(intervals, &Interval{uint16(i), uint16(i), i}) } stab, err := NewIntervalStabber(intervals) if err != nil { b.Fatalf("Unable to setup benchmark: ", err) } b.ResetTimer() for i := 0; i < b.N; i++ { results, err := stab.Intersect(42) if err != nil || len(results) != 4 { b.Fatal("Tests failed during benchmark") } } }
func BenchmarkWithHack(b *testing.B) { client, srv := net.Pipe() done := make(chan struct{}) req := []byte("GET /foo\nHost: /var/run/docker.sock\nUser-Agent: Docker\n") read := make([]byte, 4096) b.SetBytes(int64(len(req) * 30)) l := MalformedHostHeaderOverrideConn{client, true} go func() { for { if _, err := srv.Write(req); err != nil { srv.Close() break } l.first = true // make sure each subsequent run uses the hack parsing } close(done) }() for i := 0; i < b.N; i++ { for i := 0; i < 30; i++ { if n, err := l.Read(read); err != nil && err != io.EOF { b.Fatalf("read: %d - %d, err: %v\n%s", n, len(req), err, string(read[:n])) } } } l.Close() <-done }
func BenchmarkReplicationControllerConversion(b *testing.B) { data, err := ioutil.ReadFile("replication_controller_example.json") if err != nil { b.Fatalf("Unexpected error while reading file: %v", err) } var replicationController api.ReplicationController if err := runtime.DecodeInto(testapi.Default.Codec(), data, &replicationController); err != nil { b.Fatalf("Unexpected error decoding node: %v", err) } scheme := api.Scheme.Raw() var result *api.ReplicationController for i := 0; i < b.N; i++ { versionedObj, err := scheme.ConvertToVersion(&replicationController, testapi.Default.GroupVersion().String()) if err != nil { b.Fatalf("Conversion error: %v", err) } obj, err := scheme.ConvertToVersion(versionedObj, testapi.Default.InternalGroupVersion().String()) if err != nil { b.Fatalf("Conversion error: %v", err) } result = obj.(*api.ReplicationController) } if !api.Semantic.DeepDerivative(replicationController, *result) { b.Fatalf("Incorrect conversion: expected %v, got %v", replicationController, *result) } }
func BenchmarkCheckRetryClosesBody(b *testing.B) { count := 0 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { count++ if count%3 == 0 { w.WriteHeader(http.StatusOK) return } w.Header().Set("Retry-After", "0") w.WriteHeader(apierrors.StatusTooManyRequests) })) defer testServer.Close() c := testRESTClient(b, testServer) r := c.Verb("POST"). Prefix("foo", "bar"). Suffix("baz"). Timeout(time.Second). Body([]byte(strings.Repeat("abcd", 1000))) for i := 0; i < b.N; i++ { if _, err := r.DoRaw(); err != nil { b.Fatalf("Unexpected error: %v %#v", err, err) } } }
func BenchmarkConsecutivePut(b *testing.B) { r := rand.New() var blocks [][]byte var keys []datastore.Key for i := 0; i < b.N; i++ { blk := make([]byte, 256*1024) r.Read(blk) blocks = append(blocks, blk) key := base32.StdEncoding.EncodeToString(blk[:8]) keys = append(keys, datastore.NewKey(key)) } temp, cleanup := tempdir(b) defer cleanup() fs, err := flatfs.New(temp, 2) if err != nil { b.Fatalf("New fail: %v\n", err) } b.ResetTimer() for i := 0; i < b.N; i++ { err := fs.Put(keys[i], blocks[i]) if err != nil { b.Fatal(err) } } }
func benchmarkEndToEnd(dial func() (*Client, error), b *testing.B) { b.StopTimer() once.Do(startServer) client, err := dial() if err != nil { b.Fatal("error dialing:", err) } // Synchronous calls args := &Args{7, 8} procs := runtime.GOMAXPROCS(-1) N := int32(b.N) var wg sync.WaitGroup wg.Add(procs) b.StartTimer() for p := 0; p < procs; p++ { go func() { reply := new(Reply) for atomic.AddInt32(&N, -1) >= 0 { err := client.Call("Arith.Add", args, reply) if err != nil { b.Fatalf("rpc error: Add: expected no error but got string %q", err.Error()) } if reply.C != args.A+args.B { b.Fatalf("rpc error: Add: expected %d got %d", reply.C, args.A+args.B) } } wg.Done() }() } wg.Wait() }
func BenchmarkSecretConnection(b *testing.B) { b.StopTimer() fooSecConn, barSecConn := makeSecretConnPair(b) fooWriteText := RandStr(dataMaxSize) // Consume reads from bar's reader go func() { readBuffer := make([]byte, dataMaxSize) for { _, err := barSecConn.Read(readBuffer) if err == io.EOF { return } else if err != nil { b.Fatalf("Failed to read from barSecConn: %v", err) } } }() b.StartTimer() for i := 0; i < b.N; i++ { _, err := fooSecConn.Write([]byte(fooWriteText)) if err != nil { b.Fatalf("Failed to write to fooSecConn: %v", err) } } b.StopTimer() fooSecConn.Close() //barSecConn.Close() race condition }
func BenchmarkFilterPatterns(b *testing.B) { patterns := []string{ "sdk/*", "*.html", } lines := extractTestLines(b) var c uint b.ResetTimer() for i := 0; i < b.N; i++ { c = 0 for _, line := range lines { match, err := filter.List(patterns, line) if err != nil { b.Fatal(err) } if match { c++ } } if c != 22185 { b.Fatalf("wrong number of matches: expected 22185, got %d", c) } } }
// runMVCCScan first creates test data (and resets the benchmarking // timer). It then performs b.N MVCCScans in increments of numRows // keys over all of the data in the Engine instance, restarting at // the beginning of the keyspace, as many times as necessary. func runMVCCScan(emk engineMaker, numRows, numVersions, valueSize int, b *testing.B) { // Use the same number of keys for all of the mvcc scan // benchmarks. Using a different number of keys per test gives // preferential treatment to tests with fewer keys. Note that the // datasets all fit in cache and the cache is pre-warmed. const numKeys = 100000 eng, _ := setupMVCCData(emk, numVersions, numKeys, valueSize, b) defer eng.Close() b.SetBytes(int64(numRows * valueSize)) b.ResetTimer() keyBuf := append(make([]byte, 0, 64), []byte("key-")...) for i := 0; i < b.N; i++ { // Choose a random key to start scan. keyIdx := rand.Int31n(int32(numKeys - numRows)) startKey := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(keyIdx))) walltime := int64(5 * (rand.Int31n(int32(numVersions)) + 1)) ts := makeTS(walltime, 0) kvs, _, _, err := MVCCScan(context.Background(), eng, startKey, keyMax, int64(numRows), ts, true, nil) if err != nil { b.Fatalf("failed scan: %s", err) } if len(kvs) != numRows { b.Fatalf("failed to scan: %d != %d", len(kvs), numRows) } } b.StopTimer() }
func BenchmarkRunSequencial(b *testing.B) { runtime := mkRuntime(b) defer nuke(runtime) for i := 0; i < b.N; i++ { container, err := runtime.Create(&Config{ Image: GetTestImage(runtime).ID, Cmd: []string{"echo", "-n", "foo"}, }, ) if err != nil { b.Fatal(err) } defer runtime.Destroy(container) output, err := container.Output() if err != nil { b.Fatal(err) } if string(output) != "foo" { b.Fatalf("Unexpected output: %s", output) } if err := runtime.Destroy(container); err != nil { b.Fatal(err) } } }
func runMVCCConditionalPut(emk engineMaker, valueSize int, createFirst bool, b *testing.B) { rng, _ := randutil.NewPseudoRand() value := roachpb.MakeValueFromBytes(randutil.RandBytes(rng, valueSize)) keyBuf := append(make([]byte, 0, 64), []byte("key-")...) eng := emk(b, fmt.Sprintf("cput_%d", valueSize)) defer eng.Close() b.SetBytes(int64(valueSize)) var expected *roachpb.Value if createFirst { for i := 0; i < b.N; i++ { key := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(i))) ts := makeTS(timeutil.Now().UnixNano(), 0) if err := MVCCPut(context.Background(), eng, nil, key, ts, value, nil); err != nil { b.Fatalf("failed put: %s", err) } } expected = &value } b.ResetTimer() for i := 0; i < b.N; i++ { key := roachpb.Key(encoding.EncodeUvarintAscending(keyBuf[:4], uint64(i))) ts := makeTS(timeutil.Now().UnixNano(), 0) if err := MVCCConditionalPut(context.Background(), eng, nil, key, ts, value, expected, nil); err != nil { b.Fatalf("failed put: %s", err) } } b.StopTimer() }
func BenchmarkRunSequential(b *testing.B) { daemon := mkDaemon(b) defer nuke(daemon) for i := 0; i < b.N; i++ { container, _, err := daemon.Create(&runconfig.Config{ Image: GetTestImage(daemon).ID, Cmd: []string{"echo", "-n", "foo"}, }, "", ) if err != nil { b.Fatal(err) } defer daemon.Destroy(container) output, err := container.Output() if err != nil { b.Fatal(err) } if string(output) != "foo" { b.Fatalf("Unexpected output: %s", output) } if err := daemon.Destroy(container); err != nil { b.Fatal(err) } } }
func BenchmarkSingleConn(b *testing.B) { srv := NewTestServer(b, 3) defer srv.Stop() cluster := testCluster(srv.Address, 3) // Set the timeout arbitrarily low so that the query hits the timeout in a // timely manner. cluster.Timeout = 500 * time.Millisecond cluster.NumConns = 1 db, err := cluster.CreateSession() if err != nil { b.Fatalf("NewCluster: %v", err) } defer db.Close() b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { err := db.Query("void").Exec() if err != nil { b.Error(err) return } } }) }
func Benchmark_PubFourQueueSub(b *testing.B) { b.StopTimer() s := runBenchServer() c := createClientConn(b, "localhost", PERF_PORT) doDefaultConnect(b, c) sendProto(b, c, "SUB foo group1 1\r\n") sendProto(b, c, "SUB foo group1 2\r\n") sendProto(b, c, "SUB foo group1 3\r\n") sendProto(b, c, "SUB foo group1 4\r\n") bw := bufio.NewWriterSize(c, defaultSendBufSize) sendOp := []byte(fmt.Sprintf("PUB foo 2\r\nok\r\n")) ch := make(chan bool) expected := len("MSG foo 1 2\r\nok\r\n") * b.N go drainConnection(b, c, ch, expected) b.StartTimer() for i := 0; i < b.N; i++ { _, err := bw.Write(sendOp) if err != nil { b.Fatalf("Received error on PUB write: %v\n", err) } } err := bw.Flush() if err != nil { b.Fatalf("Received error on FLUSH write: %v\n", err) } // Wait for connection to be drained <-ch b.StopTimer() c.Close() s.Shutdown() }
func BenchmarkReadInts(b *testing.B) { var ls Struct bsr := &byteSliceReader{} var r io.Reader = bsr b.SetBytes(2 * (1 + 2 + 4 + 8)) b.ResetTimer() for i := 0; i < b.N; i++ { bsr.remain = big Read(r, BigEndian, &ls.Int8) Read(r, BigEndian, &ls.Int16) Read(r, BigEndian, &ls.Int32) Read(r, BigEndian, &ls.Int64) Read(r, BigEndian, &ls.Uint8) Read(r, BigEndian, &ls.Uint16) Read(r, BigEndian, &ls.Uint32) Read(r, BigEndian, &ls.Uint64) } b.StopTimer() want := s want.Float32 = 0 want.Float64 = 0 want.Complex64 = 0 want.Complex128 = 0 want.Array = [4]uint8{0, 0, 0, 0} want.Bool = false want.BoolArray = [4]bool{false, false, false, false} if b.N > 0 && !reflect.DeepEqual(ls, want) { b.Fatalf("struct doesn't match:\ngot %v;\nwant %v", ls, want) } }
func registerServedRequest(b *testing.B, ch chan<- struct{}) { select { case ch <- struct{}{}: default: b.Fatalf("More than %d requests served", cap(ch)) } }
func BenchmarkLongFormRender(b *testing.B) { tests := []struct { filename string buf []byte }{ {filename: "long_text_test.md"}, } for i, test := range tests { path := filepath.FromSlash(test.filename) f, err := os.Open(path) if err != nil { b.Fatalf("Unable to open %s: %s", path, err) } defer f.Close() membuf := new(bytes.Buffer) if _, err := io.Copy(membuf, f); err != nil { b.Fatalf("Unable to read %s: %s", path, err) } tests[i].buf = membuf.Bytes() } b.ResetTimer() for i := 0; i <= b.N; i++ { for _, test := range tests { ReadFrom(bytes.NewReader(test.buf)) } } }