Пример #1
0
func benchAddRemove(amount int, b *testing.B) {
	g := New(16)
	if len(addRemoveList) > amount {
		addRemoveList = addRemoveList[:amount]
	}
	for n := 0; n < amount; n++ {
		if len(addRemoveList) == amount {
			break
		}
		addRemoveList = append(addRemoveList, random())
	}
	if len(addRemoveList) != amount {
		b.Log("len() reports", len(addRemoveList), "want", amount)
		b.Fail()
		return
	}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		for _, s := range addRemoveList {
			g.Add(s)
			if !g.Remove(s) {
				b.Log("failed to remove")
				b.Fail()
			}
		}
	}
}
Пример #2
0
func BenchmarkHasFactorBigIntRnd_128b(b *testing.B) {
	const N = 1 << 16
	b.StopTimer()
	type t struct {
		d *big.Int
		e uint32
	}
	a := make([]t, N)
	r, err := mathutil.NewFCBig(_1, New(128), true)
	if err != nil {
		b.Fatal(err)
	}
	r2 := r32()
	for i := range a {
		dd := r.Next()
		a[i] = t{
			dd.SetBit(dd, 0, 1),
			uint32(r2.Next()),
		}
	}
	once358.Do(func() { b.Log("Random 128 bit factor, random 32 bit exponent\n") })
	runtime.GC()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		v := a[i&(N-1)]
		HasFactorBigInt(v.d, v.e)
	}
}
Пример #3
0
func BenchmarkInsert(t *testing.B) {

	link := NewCycleLink(3, 10000)
	for i := 0; i < t.N; i++ {
		msg := NewMessage(1, 3, MESSAGE_TYPE_SIMPLE)
		link.Insert(uint32(i), msg)
	}

	ch := make(chan *Message)

	go func() {
		link.Remove(0, 0, ch, func(msg *Message) bool {
			return false
		})
	}()

	for {
		tmp := <-ch
		// t.Logf("GET REMOVE -------%t\n", tmp)
		if nil == tmp {
			close(ch)
			break
		}
	}

	t.Log("HEADER---------\n")

	// PrintLink(t, link)
}
Пример #4
0
func BenchmarkPMap(b *testing.B) {
	source := sourceData()
	for i := 0; i < b.N; i++ {
		fibs := PMaps(source, fib).([]int)
		b.Log(len(fibs))
	}
}
Пример #5
0
func BenchmarkEcho(b *testing.B) {
	b.StopTimer() // Initialize
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	// Start server
	s, err := server.NewServer(sockPath, dummy.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	defer s.Stop()
	// Start client
	proxy, err := NewRemoteAppConn(sockPath)
	if err != nil {
		Exit(err.Error())
	} else {
		b.Log("Connected")
	}
	echoString := strings.Repeat(" ", 200)
	b.StartTimer() // Start benchmarking tests

	for i := 0; i < b.N; i++ {
		proxy.EchoAsync(echoString)
	}
	proxy.FlushSync()

	b.StopTimer()
	// info := proxy.InfoSync()
	//b.Log("N: ", b.N, info)
}
Пример #6
0
func benchmarkInsertAll(b *testing.B, size uint32, creator func(int) Inserter) {

	if strData == nil {
		loadStringData("/home/dgryski/strings.out")
	}

	b.ResetTimer()

	var total uint32
	var hits int
	for i := 0; i < b.N; i++ {

		ht := creator(int(size))

		for j, s := range strData {
			v, ok := ht.Insert(s, uint32(j))
			total += v
			if ok {
				hits += 1
			}
		}
	}

	b.StopTimer()

	b.Log("N=", b.N, "size=", size, "total=", total, "hits=", hits)
}
Пример #7
0
func Benchmark_Proto_HTTP_Large(b *testing.B) {
	b.Skip()
	srv := &httpTestServer{&baseTestServer{}}
	port, err := srv.listen()
	assert.NoError(b, err)
	srv.start()
	defer srv.Stop()

	client, err := rpc.DialHTTPPath("tcp",
		fmt.Sprintf("127.0.0.1:%d", port), "/v1/rpc")
	assert.NoError(b, err)
	defer client.Close()

	b.Log(port, b.N)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StartTimer()
		var res TestMessage
		req := TestMessage{
			fmt.Sprintf("%d", i),
			[]byte(strings.Repeat("0", 1024*1024)),
		}
		err = client.Call("ServiceFixture.Ping", &req, &res)
		b.StopTimer()
		assert.NoError(b, err)
		assert.Equal(b, req, res)
		b.SetBytes(int64(len(req.Data) * 2))
	}
}
Пример #8
0
func Benchmark_Proto_GRPC_Large(b *testing.B) {
	b.Skip()
	logx.SetLevel(logx.DEBUG)
	srv1 := new(GRPCServer)
	srv1.Start()
	defer srv1.srv.Stop()

	conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", srv1.Port), grpc.WithInsecure())
	assert.NoError(b, err)
	defer conn.Close()

	c := pb3.NewBarClient(conn)

	b.Log(srv1.Port, b.N)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StartTimer()
		req := pb3.TestRep{
			fmt.Sprintf("%d", i),
			[]byte(strings.Repeat("m", 1024*1024)),
		}
		res, err := c.Test(context.Background(), &req)
		b.StopTimer()
		assert.NoError(b, err)
		assert.EqualValues(b, req, *res)
		b.SetBytes(int64(len(req.Body) * 2))
	}
}
Пример #9
0
func BenchmarkHashGet(b *testing.B) {
	mPath := "/test"
	cli := NewZKClient(testServers, time.Second*5, nil)
	defer cli.Close()
	cli.CreatePersistNode(mPath)
	defer cli.DeleteNode(mPath)

	cli.CreateEphemeralNode(mPath + "/127.0.0.1:6379")
	cli.CreateEphemeralNode(mPath + "/127.0.0.1:6380")

	fmt.Println("===============create addr dir===============")
	time.Sleep(2 * time.Second)

	zkm := NewZKMonitor(testServers, time.Second*5, &testService{}, nil, mPath)
	zkm.Run()
	defer zkm.Close()
	time.Sleep(2 * time.Second)

	hGetter := zkm.HashGetter(nil)
	key := []byte{1, 2, 3, 4, 5, 6, 7, 8}

	nilCnt := int32(0)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			key[0] = byte(rand.Int())
			conn := hGetter.GetConn(key)
			if conn == nil {
				atomic.AddInt32(&nilCnt, 1)
			}
		}
	})
	b.Log("nil connection count:", nilCnt)
}
Пример #10
0
func BenchmarkTCPShareRouter(b *testing.B) {
	r, err := NewRouter(nil, ServiceProcessPayload)
	if err != nil {
		b.FailNow()
	}

	hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

	r.Run()
	<-time.Tick(1 * time.Millisecond)

	network := "tcp"
	address := "localhost:10001"
	if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil {
		b.Log(err)
		b.FailNow()
	}

	name := "scheduler"
	n := ConcurrentNum
	m := GoRoutineRequests
	for i := 0; i < n; i++ {
		if err := r.Dial(name+string(i), network, address, hf); err != nil {
			b.Log(err)
			b.FailNow()
		}
	}

	<-time.Tick(1 * time.Millisecond)
	testShareRouter(b, r, n, m)
}
Пример #11
0
func testSeperateRouter(b *testing.B, server_r *Router, client_r *Router, n int, m int) {
	name := "scheduler"
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			var wg sync.WaitGroup
			for i := 0; i < m; i++ {
				req := pbt.NewResourceReq()
				req.Id = proto.Uint64(0)
				i := rand.Intn(n)
				if m == 1 {
					if _, err := client_r.CallWait(name+string(i), "rpc", req, 5); err != nil {
						b.Log(err)
						b.FailNow()
					}
				} else {
					wg.Add(1)
					client_r.Call(name+string(i), "rpc", req, ClientProcessReponseWaitGroup, &wg, 0)
				}
			}
			if m > 1 {
				wg.Wait()
			}
		}
	})

	client_r.Stop()
	server_r.Stop()
}
Пример #12
0
func BenchmarkYorikko16b(b *testing.B) {
	b.Log(Yorikko(test16b))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Yorikko(test16b)
	}
}
Пример #13
0
func Benchmark10k10k(b *testing.B) {
	f, err := os.Open("ips10k.txt")
	if err != nil {
		b.Log(err)
		b.FailNow()
	}
	loadSrc(f)
	b.StartTimer()
	fs, err := os.Open("query10k.txt")
	if err != nil {
		b.Log(err)
		b.FailNow()
	}
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		loadStats(fs)

		b.StopTimer()
		for _, v := range allRngs {
			v.count = 0
		}
		unkC = 0
		fs.Seek(0, 0)
		b.StartTimer()
	}
}
Пример #14
0
func BenchmarkPodConversion(b *testing.B) {
	apiObjectFuzzer := apitesting.FuzzerFor(nil, api.SchemeGroupVersion, rand.NewSource(benchmarkSeed))
	items := make([]api.Pod, 4)
	for i := range items {
		apiObjectFuzzer.Fuzz(&items[i])
	}

	// add a fixed item
	data, err := ioutil.ReadFile("pod_example.json")
	if err != nil {
		b.Fatalf("Unexpected error while reading file: %v", err)
	}
	var pod api.Pod
	if err := runtime.DecodeInto(testapi.Default.Codec(), data, &pod); err != nil {
		b.Fatalf("Unexpected error decoding pod: %v", err)
	}
	items = append(items, pod)
	width := len(items)

	scheme := api.Scheme
	var result *api.Pod
	for i := 0; i < b.N; i++ {
		pod := &items[i%width]
		versionedObj, err := scheme.UnsafeConvertToVersion(pod, *testapi.Default.GroupVersion())
		if err != nil {
			b.Fatalf("Conversion error: %v", err)
		}
		obj, err := scheme.UnsafeConvertToVersion(versionedObj, testapi.Default.InternalGroupVersion())
		if err != nil {
			b.Fatalf("Conversion error: %v", err)
		}
		result = obj.(*api.Pod)
	}
	b.Log(result)
}
Пример #15
0
func BenchmarkStoreObject(b *testing.B) {
	client := New("127.0.0.1:8087")
	err := client.Connect()
	if err != nil {
		b.FailNow()
	}

	for i := 0; i < b.N; i++ {
		bucket, err := client.Bucket("client_test.go")
		if err != nil {
			b.FailNow()
		}
		obj := bucket.New("abc", PW1, DW1)
		if obj == nil {
			b.FailNow()
		}
		obj.ContentType = "text/plain"
		obj.Data = []byte("some more data")
		err = obj.Store()
		if err != nil {
			b.Log(err)
			b.Fail()
		}
	}
}
Пример #16
0
func BenchmarkClosest1k(b *testing.B) {
	tree := New()

	n := 1000
	for i := 0; i < n; i++ {
		o := random()
		tree.Add(o)
	}
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		results := make(chan gfx.Boundable)
		tree.Closest(nil, results, nil)
		nResults := 0
		for {
			_, ok := <-results
			if !ok {
				break
			}
			nResults++
		}
		if len(results) != n {
			b.Log("results", nResults)
			b.Log("want", n)
			b.Fail()
		}
	}
}
Пример #17
0
func BenchmarkEcho(b *testing.B) {
	b.StopTimer() // Initialize
	sockPath := Fmt("unix:///tmp/echo_%v.sock", RandStr(6))
	_, err := server.StartListener(sockPath, example.NewDummyApplication())
	if err != nil {
		Exit(err.Error())
	}
	conn, err := Connect(sockPath)
	if err != nil {
		Exit(err.Error())
	} else {
		b.Log("Connected")
	}

	proxy := NewRemoteAppContext(conn, 10)
	proxy.Start()
	echoString := strings.Repeat(" ", 200)
	b.StartTimer() // Start benchmarking tests

	for i := 0; i < b.N; i++ {
		proxy.EchoAsync(echoString)
	}
	proxy.FlushSync()

	b.StopTimer()
	// info := proxy.InfoSync()
	//b.Log("N: ", b.N, info)
}
Пример #18
0
func BenchmarkMetalReviewExample(b *testing.B) {
	var n int
	var buf bytes.Buffer

	b.StopTimer()
	doc := LoadDoc("metalreview.html")
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		doc.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) {
			var band, title string
			var score float64
			var e error

			n++
			// For each item found, get the band, title and score, and print it
			band = s.Find("strong").Text()
			title = s.Find("em").Text()
			if score, e = strconv.ParseFloat(s.Find(".score").Text(), 64); e != nil {
				// Not a valid float, ignore score
				if n <= 4 {
					buf.WriteString(fmt.Sprintf("Review %d: %s - %s.\n", i, band, title))
				}
			} else {
				// Print all, including score
				if n <= 4 {
					buf.WriteString(fmt.Sprintf("Review %d: %s - %s (%2.1f).\n", i, band, title, score))
				}
			}
		})
	}
	b.Log(buf.String())
	b.Logf("MetalReviewExample=%d", n)
}
Пример #19
0
func Benchmark_Proto_Binary(b *testing.B) {
	b.Skip()
	srv := &binaryTestServer{&baseTestServer{}}
	port, err := srv.listen()
	assert.NoError(b, err)
	srv.start()
	defer srv.Stop()

	client, err := rpc.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
	assert.NoError(b, err)
	defer client.Close()

	b.Log(port)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StartTimer()
		var res TestMessage
		req := TestMessage{
			"12345678910",
			[]byte("mama myla ramu"),
		}
		err = client.Call("ServiceFixture.Ping", &req, &res)
		b.StopTimer()
		assert.NoError(b, err)
		assert.Equal(b, req, res)
		b.SetBytes(int64(len(req.Data) * 2))
	}
}
Пример #20
0
func BenchmarkPushKey(b *testing.B) {
	queueName := "test_crawler"
	cfg, err := config.Get("/etc/yascrapy/core.json")
	if err != nil {
		b.Error(err.Error())
	}
	conn, err := rabbitmq.GetConn(cfg)
	if err != nil {
		b.Error(err.Error())
	}
	err = rabbitmq.DeclareQueue(conn, queueName)
	if err != nil {
		b.Error(err.Error())
	}
	ch, err := rabbitmq.MakeChannel(conn)
	defer ch.Close()
	if err != nil {
		b.Error("TestPush fail", err.Error())
	}
	for i := 0; i < b.N; i++ {
		req := initRequest()
		req.Url = fmt.Sprintf("%s%d", req.Url, i)
		resp := initResponse(req)
		err = PushKey(ch, resp, queueName)
		if err != nil {
			b.Log(err.Error())
		}
	}
}
Пример #21
0
func Benchmark_words_load(t *testing.B) {
	// wordlist was downloaded from http://www-personal.umich.edu/~jlawler/wordlist
	f, err := os.Open("d:\\wordlist.txt")
	if err != nil {
		t.Skip()
		return
	}
	defer f.Close()

	s := bufio.NewScanner(f)
	words := make([]string, 0, 80000)
	for s.Scan() {
		txt := s.Text()
		words = append(words, txt)
	}
	sort.Stable(byStringLengthDesc(words))

	tree := NewPrefixedTree("")
	for i := range words {
		tree.Put(words[i], i)
	}

	of, err := os.Create("d:\\wordlist.json")
	panicif(err)
	defer of.Close()
	of.WriteString(tree.String())

	t.Log("Nodes: ", tree.NodeCount())
	t.Log("Max fanout: ", tree.root.MaxFanout())

	histogram := make(map[int]int)
	tree.root.visit(func(n *Node) bool {
		count := len(n.Nodes)
		histogram[count] += 1
		return true
	})

	kvs := make([]kv, len(histogram))
	i := 0

	for k, v := range histogram {
		kvs[i] = kv{k, v}
		i++
	}
	sort.Stable(byFanout(kvs))
	for i := range kvs {
		t.Logf("%+v", kvs[i])
	}
	t.Logf("=====")
	sort.Stable(byCount(kvs))
	for i := range kvs {
		t.Logf("%+v", kvs[i])
	}

	tree.root.Walk("abating", func(path string, h int) bool {
		t.Logf("%d(%s): %s ", h, path, words[h])
		return true
	})
}
Пример #22
0
func BenchmarkUnmarshal(t *testing.B) {
	t.Log("Running Decode Benchmark\n")
	packet, _ := hex.DecodeString(TestPackets[0])
	s, _ := NewGoSNMP("", "", Version2c, 5)
	for i := 0; i < t.N; i++ {
		s.Debug(packet)
	}
}
Пример #23
0
func benchmarkRand(b *testing.B, s Sampler) {
	// run sample b.N times
	for n := 0; n < b.N; n++ {
		s.Sample()
	}

	b.Log(Stats(s))
}
Пример #24
0
func Benchmarkb2Ss(bm *testing.B) {
	a := ""
	b := []byte("this is a short")
	for i := 0; i < bm.N; i++ {
		a = string(b)
	}
	bm.Log(a)
}
Пример #25
0
func BenchmarkAnalyzer(b *testing.B) {
	var score float64
	for i := 0; i < b.N; i++ {
		for _, tt := range testCases {
			score = Analyze(strings.NewReader(tt.in))
		}
	}
	b.Log("last score:", score)
}
Пример #26
0
func benchmark(b *testing.B, fn func(string, string, uint, uint) error) {
	dir, _ := ioutil.TempDir("", "")
	for i := 0; i < b.N; i++ {
		if err := fn(filepath.Join(dir, fmt.Sprintf("totem_640_480_%d.jpg", i)), "totem.jpg", 640, 480); err != nil {
			b.Log(err)
			b.FailNow()
		}
	}
}
Пример #27
0
func BenchmarkIsArrayDuplicateOpt(b *testing.B) {
	t := 0
	for i := 0; i < b.N; i++ {
		if IsArrayDuplicateOpt(arrA, arrB) {
			t++
		}
	}
	b.Log("Matched:", t, "/", b.N)
}
Пример #28
0
func BenchmarkSimpleCaller(b *testing.B) {
	for i := 0; i < b.N; i++ {
		file, line, _ := Lookup(1)
		if testing.Verbose() {
			s := fmt.Sprintf("%s:%d", file, line)
			b.Log(s)
		}
	}
}
Пример #29
0
func BenchmarkAppend(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_, err := gorethink.Db("test").Table("benchmark_keys_list").Get(table2search).Field("voted").Append(uniuri.New()).Run(session)
		if err != nil {
			b.Log(err)
			b.Fail()
		}
	}
}
Пример #30
0
// BenchmarkPKCS11 signs a certificate repeatedly using a PKCS11 token and
// measures speed. To run (with SoftHSM):
// go test -bench=. -benchtime 5s ./crypto/pkcs11key/ \
//   -module /usr/lib/softhsm/libsofthsm.so -token-label "softhsm token" \
//   -pin 1234 -private-key-label "my key" -cpu 4
// You can adjust benchtime if you want to run for longer or shorter, and change
// the number of CPUs to select the parallelism you want.
func BenchmarkPKCS11(b *testing.B) {
	if *module == "" || *tokenLabel == "" || *pin == "" || *privateKeyLabel == "" {
		b.Fatal("Must pass all flags: module, tokenLabel, pin, and privateKeyLabel")
		return
	}

	// A minimal, bogus certificate to be signed.
	// Note: we choose a large N to make up for some of the missing fields in the
	// bogus certificate, so we wind up something approximately the size of a real
	// certificate.
	N := big.NewInt(1)
	N.Lsh(N, 6000)
	template := x509.Certificate{
		SerialNumber:       big.NewInt(1),
		PublicKeyAlgorithm: x509.RSA,
		NotBefore:          time.Now(),
		NotAfter:           time.Now(),

		PublicKey: &rsa.PublicKey{
			N: N,
			E: 1 << 17,
		},
	}

	pool, err := NewPool(*sessionCount, *module, *tokenLabel, *pin, *privateKeyLabel)
	if err != nil {
		b.Fatal(err)
		return
	}
	defer pool.Destroy()

	instance := pool.get()
	if instance.alwaysAuthenticate {
		b.Log("WARNING: Token has CKA_ALWAYS_AUTHENTICATE attribute, which makes signing slow.")
	}
	pool.put(instance)

	// Reset the benchmarking timer so we don't include setup time.
	b.ResetTimer()

	// Start recording total time. Go's benchmarking code is interested in
	// nanoseconds per op, but we're also interested in the total throughput.
	start := time.Now()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			_, err = x509.CreateCertificate(rand.Reader, &template, &template, template.PublicKey, pool)
			if err != nil {
				b.Fatal(err)
				return
			}
		}
	})

	elapsedTime := time.Now().Sub(start)
	b.Logf("Time, count, ops / second: %s, %d, %g", elapsedTime, b.N, float64(b.N)*float64(time.Second)/float64(elapsedTime))
}