Exemplo n.º 1
1
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)
		}
	}
}
Exemplo n.º 2
1
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)
}
Exemplo n.º 3
1
func BenchmarkCacheComplete(b *testing.B) {
	b.StopTimer()

	paths := DefaultPaths()
	if len(paths) == 0 {
		b.Skip("No default paths available")
	}

	tests := []string{"mscorlib.dll", "System.dll"}

	c := Cache{paths: paths}
	for _, test := range tests {
		if _, err := c.Load(test); err != nil {
			b.Error(err)
		}
	}

	tests2 := []content.Type{
		content.Type{Name: content.FullyQualifiedName{Absolute: "net://type/System.String"}},
	}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		for _, test := range tests2 {
			if _, err := c.Complete(&test); err != nil {
				b.Error(err)
			}
		}
	}
}
Exemplo n.º 4
1
func getParseChi(b *testing.B) xhandler.HandlerC {
	defer b.ResetTimer()
	if parseChi == nil {
		parseChi = loadChi(parseAPI)
	}
	return parseChi
}
Exemplo n.º 5
1
func getParseHTTPRouter(b *testing.B) http.Handler {
	defer b.ResetTimer()
	if parseHTTPRouter == nil {
		parseHTTPRouter = loadHTTPRouter(parseAPI)
	}
	return parseHTTPRouter
}
Exemplo n.º 6
1
func BenchmarkHeaderWriteSubset(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		buf.Reset()
		testHeader.WriteSubset(&buf, nil)
	}
}
Exemplo n.º 7
0
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()
}
Exemplo n.º 8
0
func BenchmarkConcurrentTxStmtQuery(b *testing.B) {
	b.ReportAllocs()
	ct := new(concurrentTxStmtQueryTest)
	for i := 0; i < b.N; i++ {
		doConcurrentTest(b, ct)
	}
}
Exemplo n.º 9
0
func BenchmarkConcurrentRandom(b *testing.B) {
	b.ReportAllocs()
	ct := new(concurrentRandomTest)
	for i := 0; i < b.N; i++ {
		doConcurrentTest(b, ct)
	}
}
Exemplo n.º 10
0
func BenchmarkTruncIndexNew(b *testing.B) {
	ids := []string{"banana", "bananaa", "bananab"}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		NewTruncIndex(ids)
	}
}
Exemplo n.º 11
0
func BenchmarkSmall(t *testing.B) {
	for i := 0; i < t.N; i++ {
		if _, err := ParseString(SmallJson); err != nil {
			t.Fatal(err)
		}
	}
}
func BenchmarkUnmarshalText(b *testing.B) {
	pb := new(MyMessage)
	for i := 0; i < b.N; i++ {
		UnmarshalText(benchInput, pb)
	}
	b.SetBytes(int64(len(benchInput)))
}
Exemplo n.º 13
0
func BenchmarkCopyImagePaletted(b *testing.B) {
	img := image.NewPaletted(image.Rect(0, 0, 4096, 4096), palette.Plan9)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		CopyImage(img)
	}
}
Exemplo n.º 14
0
func BenchmarkCopyImageNRGBA(b *testing.B) {
	img := image.NewNRGBA(image.Rect(0, 0, 4096, 4096))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		CopyImage(img)
	}
}
Exemplo n.º 15
0
func BenchmarkReplicaSnapshot(b *testing.B) {
	defer tracing.Disable()()
	storeCfg := TestStoreConfig(nil)
	storeCfg.TestingKnobs.DisableSplitQueue = true
	stopper := stop.NewStopper()
	defer stopper.Stop()
	store := createTestStoreWithConfig(b, stopper, &storeCfg)
	// We want to manually control the size of the raft log.
	store.SetRaftLogQueueActive(false)

	rep, err := store.GetReplica(rangeID)
	if err != nil {
		b.Fatal(err)
	}

	snapSize := rep.GetMaxBytes()
	if err := fillTestRange(rep, snapSize); err != nil {
		b.Fatal(err)
	}
	b.SetBytes(snapSize)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if _, err := rep.GetSnapshot(context.Background(), "bench"); err != nil {
			b.Fatal(err)
		}
		rep.CloseOutSnap()
	}
}
Exemplo n.º 16
0
func BenchmarkSprintfPadding(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("%16f", 1.0)
		}
	})
}
Exemplo n.º 17
0
func BenchmarkMiddleware(b *testing.B) {
	g := New()
	g.Sub("/a", func(a *Composer) {
		a.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
		})
		a.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
		})
		a.Sub("/c", func(c *Composer) {
			c.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
			})
			c.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
			})
			c.Get("/action", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
				fmt.Fprintf(rw, "hello")
			}))
		})
	})
	routes := g.BuildRoutes()
	router := httprouter.New()
	for _, route := range routes {
		router.Handle(route.Method, route.Pattern, func(rw http.ResponseWriter, r *http.Request, params httprouter.Params) {
			route.Handler.ServeHTTP(rw, r)
		})
	}

	b.ReportAllocs()
	b.ResetTimer()
	recorder := httptest.NewRecorder()
	request, _ := http.NewRequest("GET", "/a/c/action", nil)
	for i := 0; i < b.N; i++ {
		router.ServeHTTP(recorder, request)
	}
}
Exemplo n.º 18
0
func BenchmarkSprintfEmpty(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("")
		}
	})
}
Exemplo n.º 19
0
// ReadWrite does read and write in parallel.
// qRead is num goroutines for reading.
// qWrite is num goroutines for writing.
// Assume n divisible by (qRead + qWrite).
func ReadWrite(n, qRead, qWrite int, newFunc func() HashMap, b *testing.B) {
	q := qRead + qWrite
	check(n, q)
	work := intPairArray(n)
	b.StartTimer()
	for i := 0; i < b.N; i++ { // N reps.
		h := newFunc()
		var wg sync.WaitGroup
		for j := 0; j < qRead; j++ { // Read goroutines.
			wg.Add(1)
			go func(j int) {
				defer wg.Done()
				start, end := workRange(n, q, j)
				for k := start; k < end; k++ {
					h.Get(work[k].Key)
				}
			}(j)
		}

		for j := qRead; j < q; j++ { // Write goroutines.
			wg.Add(1)
			go func(j int) {
				defer wg.Done()
				start, end := workRange(n, q, j)
				for k := start; k < end; k++ {
					h.Put(work[k].Key, work[k].Val)
				}
			}(j)
		}
		wg.Wait()
	}
}
Exemplo n.º 20
0
func BenchmarkSprintfString(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("%s", "hello")
		}
	})
}
Exemplo n.º 21
0
func getParseXmux(b *testing.B) xhandler.HandlerC {
	defer b.ResetTimer()
	if parseXmux == nil {
		parseXmux = loadXmux(parseAPI)
	}
	return parseXmux
}
Exemplo n.º 22
0
func BenchmarkSprintfIntInt(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("%d %d", 5, 6)
		}
	})
}
Exemplo n.º 23
0
func getParseGoji(b *testing.B) http.Handler {
	defer b.ResetTimer()
	if parseGoji == nil {
		parseGoji = loadGoji(parseAPI)
	}
	return parseGoji
}
Exemplo n.º 24
0
func BenchmarkSprintfPrefixedInt(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("This is some meaningless prefix text that needs to be scanned %d", 6)
		}
	})
}
Exemplo n.º 25
0
func BenchmarkTopicToChannelPut(b *testing.B) {
	b.StopTimer()
	log.SetOutput(ioutil.Discard)
	defer log.SetOutput(os.Stdout)
	topicName := "bench_topic_to_channel_put" + strconv.Itoa(b.N)
	channelName := "bench"
	options := NewNSQDOptions()
	options.MemQueueSize = int64(b.N)
	_, _, nsqd := mustStartNSQD(options)
	defer nsqd.Exit()
	channel := nsqd.GetTopic(topicName).GetChannel(channelName)
	b.StartTimer()

	for i := 0; i <= b.N; i++ {
		topic := nsqd.GetTopic(topicName)
		msg := nsq.NewMessage(<-nsqd.idChan, []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
		topic.PutMessage(msg)
	}

	for {
		if len(channel.memoryMsgChan) == b.N {
			break
		}
		runtime.Gosched()
	}
}
Exemplo n.º 26
0
func BenchmarkSprintfFloat(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("%g", 5.23184)
		}
	})
}
Exemplo n.º 27
0
func registerServedRequest(b *testing.B, ch chan<- struct{}) {
	select {
	case ch <- struct{}{}:
	default:
		b.Fatalf("More than %d requests served", cap(ch))
	}
}
Exemplo n.º 28
0
func BenchmarkSprintfBoolean(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			Sprintf("%t", true)
		}
	})
}
Exemplo n.º 29
0
func benchCore(b *testing.B, hostAndPort string) {
	b.StopTimer()

	client, err := DialTimeout(hostAndPort, 1*time.Second)
	if err != nil {
		b.Fatalf("Failed to connect: %v", err)
	}
	jobBody := []byte("testing")

	b.StartTimer()

	for i := 0; i < b.N; i += 1 {
		_, _, err := client.Put(0, 0, 0, jobBody)
		if err != nil {
			b.Fatalf("Failed to Put: %v", err)
		}
	}

	for i := 0; i < b.N; i += 1 {
		_, _, err := client.Reserve()
		if err != nil {
			b.Fatalf("Failed to Reserve: %v", err)
		}
	}

	client.Quit()
}
Exemplo n.º 30
0
func BenchmarkExecuteInParallel(b *testing.B) {
	numItems := int64(1000)

	qs := make([]*Queue, 0, b.N)

	for i := 0; i < b.N; i++ {
		q := New(numItems)
		for j := int64(0); j < numItems; j++ {
			q.Put(j)
		}
		qs = append(qs, q)
	}

	var counter int64
	fn := func(ifc interface{}) {
		c := ifc.(int64)
		atomic.AddInt64(&counter, c)
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		q := qs[i]
		ExecuteInParallel(q, fn)
	}
}