Commit 3e0e80eb authored by Ubuntu's avatar Ubuntu

add MB/s metric

parent 09da1d19
...@@ -3,25 +3,40 @@ package benchmark_test ...@@ -3,25 +3,40 @@ package benchmark_test
import ( import (
"fmt" "fmt"
"math" "math"
"sort"
"testing" "testing"
) )
//date && go test -v -run BenchmarkHello -bench=BenchmarkHello -benchtime=3s -benchmem && date //date && go test -v -run BenchmarkHello -bench=BenchmarkHello -benchtime=3s -benchmem && date
//go test -v -run BenchmarkAnyTxEth -bench BenchmarkRepeated -benchmem -count 10 | tee RepeatedTxEthAsAny.txt
// go test -v -run BenchmarkHello -bench BenchmarkHello -benchmem -count 10 | tee RepeatedTxEthAsAny.txt
func BenchmarkHello(b *testing.B) { func BenchmarkHello(b *testing.B) {
b.Log(b.N) // b.Log(b.N)
//b.Logf("begin time: %s \n", time.Now()) // //b.Logf("begin time: %s \n", time.Now())
b.ResetTimer() // b.ResetTimer()
for i := 0; i < b.N; i++ { // for i := 0; i < b.N; i++ {
//b.Logf("idx %d time: %s \n", i, time.Now()) // //b.Logf("idx %d time: %s \n", i, time.Now())
fmt.Sprintf("hello") // fmt.Sprintf("hello")
} // }
//b.StopTimer() //b.StopTimer()
//b.Logf("end time: %s \n", time.Now()) //b.Logf("end time: %s \n", time.Now())
var compares int64
for i := 0; i < b.N; i++ {
s := []int{5, 4, 3, 2, 1}
sort.Slice(s, func(i, j int) bool {
compares++
return s[i] < s[j]
})
}
// This metric is per-operation, so divide by b.N and
// report it as a "/op" unit.
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
} }
func BenchmarkCalculate(b *testing.B) { func BenchmarkCalculate(b *testing.B) {
...@@ -115,3 +130,238 @@ func BenchmarkSieveOfErastosthenes(b *testing.B) { ...@@ -115,3 +130,238 @@ func BenchmarkSieveOfErastosthenes(b *testing.B) {
}) })
} }
} }
func TestReportMetric(t *testing.T) {
res := testing.Benchmark(func(b *testing.B) {
b.ReportMetric(12345, "ns/op")
b.ReportMetric(0.2, "frobs/op")
})
// Test built-in overriding.
if res.NsPerOp() != 12345 {
t.Errorf("NsPerOp: expected %v, actual %v", 12345, res.NsPerOp())
}
// Test stringing.
res.N = 1 // Make the output stable
want := " 1\t 12345 ns/op\t 0.2000 frobs/op"
if want != res.String() {
t.Errorf("expected %q, actual %q", want, res.String())
}
}
// go test -v -run BenchmarkMetric -bench BenchmarkMetric -benchmem
func BenchmarkMetric(b *testing.B) {
var compares int64
for i := 0; i < b.N; i++ {
s := []int{5, 4, 3, 2, 1}
sort.Slice(s, func(i, j int) bool {
compares++
return s[i] < s[j]
})
}
// This metric is per-operation, so divide by b.N and
// report it as a "/op" unit.
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
}
func ExampleB_ReportMetric() {
// This reports a custom benchmark metric relevant to a
// specific algorithm (in this case, sorting).
testing.Benchmark(func(b *testing.B) {
var compares int64
for i := 0; i < b.N; i++ {
s := []int{5, 4, 3, 2, 1}
sort.Slice(s, func(i, j int) bool {
compares++
return s[i] < s[j]
})
}
// This metric is per-operation, so divide by b.N and
// report it as a "/op" unit.
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
})
}
// func TestBRun(t *T) {
// work := func(b *B) {
// for i := 0; i < b.N; i++ {
// time.Sleep(time.Nanosecond)
// }
// }
// testCases := []struct {
// desc string
// failed bool
// chatty bool
// output string
// f func(*B)
// }{{
// desc: "simulate sequential run of subbenchmarks.",
// f: func(b *B) {
// b.Run("", func(b *B) { work(b) })
// time1 := b.result.NsPerOp()
// b.Run("", func(b *B) { work(b) })
// time2 := b.result.NsPerOp()
// if time1 >= time2 {
// t.Errorf("no time spent in benchmark t1 >= t2 (%d >= %d)", time1, time2)
// }
// },
// }, {
// desc: "bytes set by all benchmarks",
// f: func(b *B) {
// b.Run("", func(b *B) { b.SetBytes(10); work(b) })
// b.Run("", func(b *B) { b.SetBytes(10); work(b) })
// if b.result.Bytes != 20 {
// t.Errorf("bytes: got: %d; want 20", b.result.Bytes)
// }
// },
// }, {
// desc: "bytes set by some benchmarks",
// // In this case the bytes result is meaningless, so it must be 0.
// f: func(b *B) {
// b.Run("", func(b *B) { b.SetBytes(10); work(b) })
// b.Run("", func(b *B) { work(b) })
// b.Run("", func(b *B) { b.SetBytes(10); work(b) })
// if b.result.Bytes != 0 {
// t.Errorf("bytes: got: %d; want 0", b.result.Bytes)
// }
// },
// }, {
// desc: "failure carried over to root",
// failed: true,
// output: "--- FAIL: root",
// f: func(b *B) { b.Fail() },
// }, {
// desc: "skipping without message, chatty",
// chatty: true,
// output: "--- SKIP: root",
// f: func(b *B) { b.SkipNow() },
// }, {
// desc: "chatty with recursion",
// chatty: true,
// f: func(b *B) {
// b.Run("", func(b *B) {
// b.Run("", func(b *B) {})
// })
// },
// }, {
// desc: "skipping without message, not chatty",
// f: func(b *B) { b.SkipNow() },
// }, {
// desc: "skipping after error",
// failed: true,
// output: `
// --- FAIL: root
// sub_test.go:NNN: an error
// sub_test.go:NNN: skipped`,
// f: func(b *B) {
// b.Error("an error")
// b.Skip("skipped")
// },
// }, {
// desc: "memory allocation",
// f: func(b *B) {
// const bufSize = 256
// alloc := func(b *B) {
// var buf [bufSize]byte
// for i := 0; i < b.N; i++ {
// _ = append([]byte(nil), buf[:]...)
// }
// }
// b.Run("", func(b *B) {
// alloc(b)
// b.ReportAllocs()
// })
// b.Run("", func(b *B) {
// alloc(b)
// b.ReportAllocs()
// })
// // runtime.MemStats sometimes reports more allocations than the
// // benchmark is responsible for. Luckily the point of this test is
// // to ensure that the results are not underreported, so we can
// // simply verify the lower bound.
// if got := b.result.MemAllocs; got < 2 {
// t.Errorf("MemAllocs was %v; want 2", got)
// }
// if got := b.result.MemBytes; got < 2*bufSize {
// t.Errorf("MemBytes was %v; want %v", got, 2*bufSize)
// }
// },
// }, {
// desc: "cleanup is called",
// f: func(b *B) {
// var calls, cleanups, innerCalls, innerCleanups int
// b.Run("", func(b *B) {
// calls++
// b.Cleanup(func() {
// cleanups++
// })
// b.Run("", func(b *B) {
// b.Cleanup(func() {
// innerCleanups++
// })
// innerCalls++
// })
// work(b)
// })
// if calls == 0 || calls != cleanups {
// t.Errorf("mismatched cleanups; got %d want %d", cleanups, calls)
// }
// if innerCalls == 0 || innerCalls != innerCleanups {
// t.Errorf("mismatched cleanups; got %d want %d", cleanups, calls)
// }
// },
// }, {
// desc: "cleanup is called on failure",
// failed: true,
// f: func(b *B) {
// var calls, cleanups int
// b.Run("", func(b *B) {
// calls++
// b.Cleanup(func() {
// cleanups++
// })
// b.Fatalf("failure")
// })
// if calls == 0 || calls != cleanups {
// t.Errorf("mismatched cleanups; got %d want %d", cleanups, calls)
// }
// },
// }}
// for _, tc := range testCases {
// t.Run(tc.desc, func(t *T) {
// var ok bool
// buf := &bytes.Buffer{}
// // This is almost like the Benchmark function, except that we override
// // the benchtime and catch the failure result of the subbenchmark.
// root := &B{
// common: common{
// signal: make(chan bool),
// name: "root",
// w: buf,
// },
// benchFunc: func(b *B) { ok = b.Run("test", tc.f) }, // Use Run to catch failure.
// benchTime: durationOrCountFlag{d: 1 * time.Microsecond},
// }
// if tc.chatty {
// root.chatty = newChattyPrinter(root.w)
// }
// root.runN(1)
// if ok != !tc.failed {
// t.Errorf("%s:ok: got %v; want %v", tc.desc, ok, !tc.failed)
// }
// if !ok != root.Failed() {
// t.Errorf("%s:root failed: got %v; want %v", tc.desc, !ok, root.Failed())
// }
// // All tests are run as subtests
// if root.result.N != 1 {
// t.Errorf("%s: N for parent benchmark was %d; want 1", tc.desc, root.result.N)
// }
// got := strings.TrimSpace(buf.String())
// want := strings.TrimSpace(tc.output)
// re := makeRegexp(want)
// if ok, err := regexp.MatchString(re, got); !ok || err != nil {
// t.Errorf("%s:output:\ngot:\n%s\nwant:\n%s", tc.desc, got, want)
// }
// })
// }
// }
...@@ -2,36 +2,37 @@ goos: linux ...@@ -2,36 +2,37 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/CaduceusMetaverseProtocol/metaprotocol/benchmark pkg: github.com/CaduceusMetaverseProtocol/metaprotocol/benchmark
cpu: Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz cpu: Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz
BenchmarkRepeated BenchmarkQueueEthTx-8 7838 149282 ns/op
BenchmarkRepeated/input_size_200 --- BENCH: BenchmarkQueueEthTx-8
BenchmarkRepeated/input_size_200-8 4838 235895 ns/op 206960 B/op 1030 allocs/op grpc-queue-tx_test.go:30: b.N: 1
BenchmarkRepeated/input_size_200-8 5028 240751 ns/op 206712 B/op 1029 allocs/op grpc-queue-tx_test.go:109: response: 9fa50e67f63b7059a92a5ffe0f4f8201e88c38b572364f8140275b2f445c1b8f local: 9fa50e67f63b7059a92a5ffe0f4f8201e88c38b572364f8140275b2f445c1b8f
BenchmarkRepeated/input_size_200-8 5065 224936 ns/op 206671 B/op 1029 allocs/op grpc-queue-tx_test.go:30: b.N: 100
BenchmarkRepeated/input_size_200-8 4790 224242 ns/op 206669 B/op 1029 allocs/op grpc-queue-tx_test.go:30: b.N: 3288
BenchmarkRepeated/input_size_200-8 4849 232470 ns/op 206667 B/op 1029 allocs/op grpc-queue-tx_test.go:30: b.N: 5540
BenchmarkRepeated/input_size_500 grpc-queue-tx_test.go:30: b.N: 7838
BenchmarkRepeated/input_size_500-8 3127 380410 ns/op 294254 B/op 1652 allocs/op BenchmarkQueueEthTx-8 10000 156028 ns/op
BenchmarkRepeated/input_size_500-8 2966 374246 ns/op 294329 B/op 1652 allocs/op --- BENCH: BenchmarkQueueEthTx-8
BenchmarkRepeated/input_size_500-8 3009 393651 ns/op 294321 B/op 1651 allocs/op grpc-queue-tx_test.go:30: b.N: 1
BenchmarkRepeated/input_size_500-8 3063 372750 ns/op 294320 B/op 1651 allocs/op grpc-queue-tx_test.go:30: b.N: 100
BenchmarkRepeated/input_size_500-8 2985 393548 ns/op 294337 B/op 1652 allocs/op grpc-queue-tx_test.go:30: b.N: 10000
BenchmarkRepeated/input_size_1000 BenchmarkQueueEthTx-8 10000 154042 ns/op
BenchmarkRepeated/input_size_1000-8 1988 591392 ns/op 427929 B/op 2652 allocs/op --- BENCH: BenchmarkQueueEthTx-8
BenchmarkRepeated/input_size_1000-8 1899 612673 ns/op 427931 B/op 2652 allocs/op grpc-queue-tx_test.go:30: b.N: 1
BenchmarkRepeated/input_size_1000-8 1996 607685 ns/op 427922 B/op 2652 allocs/op grpc-queue-tx_test.go:30: b.N: 100
BenchmarkRepeated/input_size_1000-8 2022 607354 ns/op 427935 B/op 2652 allocs/op grpc-queue-tx_test.go:30: b.N: 10000
BenchmarkRepeated/input_size_1000-8 2078 605480 ns/op 427925 B/op 2652 allocs/op BenchmarkQueueStdTx-8 10000 157969 ns/op
BenchmarkRepeated/input_size_2000 --- BENCH: BenchmarkQueueStdTx-8
BenchmarkRepeated/input_size_2000-8 1226 1069362 ns/op 695546 B/op 4670 allocs/op grpc-queue-tx_test.go:118: b.N: 1
BenchmarkRepeated/input_size_2000-8 1147 1052716 ns/op 695544 B/op 4670 allocs/op grpc-queue-tx_test.go:118: b.N: 100
BenchmarkRepeated/input_size_2000-8 1077 1068582 ns/op 695582 B/op 4670 allocs/op grpc-queue-tx_test.go:118: b.N: 10000
BenchmarkRepeated/input_size_2000-8 1087 1057335 ns/op 695547 B/op 4670 allocs/op BenchmarkQueueStdTx-8 10000 150678 ns/op
BenchmarkRepeated/input_size_2000-8 1106 1045020 ns/op 695558 B/op 4670 allocs/op --- BENCH: BenchmarkQueueStdTx-8
BenchmarkRepeated/input_size_5000 grpc-queue-tx_test.go:118: b.N: 1
BenchmarkRepeated/input_size_5000-8 444 2444132 ns/op 1514766 B/op 10709 allocs/op grpc-queue-tx_test.go:118: b.N: 100
BenchmarkRepeated/input_size_5000-8 442 2406409 ns/op 1514738 B/op 10708 allocs/op grpc-queue-tx_test.go:118: b.N: 10000
BenchmarkRepeated/input_size_5000-8 435 2440909 ns/op 1514581 B/op 10705 allocs/op BenchmarkQueueStdTx-8 10000 153660 ns/op
BenchmarkRepeated/input_size_5000-8 458 2390721 ns/op 1514734 B/op 10708 allocs/op --- BENCH: BenchmarkQueueStdTx-8
BenchmarkRepeated/input_size_5000-8 447 2439328 ns/op 1514725 B/op 10709 allocs/op grpc-queue-tx_test.go:118: b.N: 1
PASS grpc-queue-tx_test.go:118: b.N: 100
ok github.com/CaduceusMetaverseProtocol/metaprotocol/benchmark 34.716s grpc-queue-tx_test.go:118: b.N: 10000
BenchmarkQueueAnyTx-8
\ No newline at end of file
...@@ -88,6 +88,7 @@ func RepeatedEthTx(txl int, b *testing.B) { ...@@ -88,6 +88,7 @@ func RepeatedEthTx(txl int, b *testing.B) {
txs := make([]*base.TransactionEth, 0, txl) txs := make([]*base.TransactionEth, 0, txl)
for i := 0; i < txl; i++ { for i := 0; i < txl; i++ {
b.SetBytes(88)
txs = append(txs, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}}) txs = append(txs, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}})
} }
...@@ -170,10 +171,14 @@ func RepeatedTxEthAsAny(txl int, b *testing.B) { ...@@ -170,10 +171,14 @@ func RepeatedTxEthAsAny(txl int, b *testing.B) {
txs := make([]*base.TransactionEth, 0, txl) txs := make([]*base.TransactionEth, 0, txl)
for i := 0; i < txl; i++ { for i := 0; i < txl; i++ {
b.SetBytes(88)
txs = append(txs, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}}) txs = append(txs, &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}})
} }
ethTxAsAny, err := anypb.New(&base.RepeatedEthTx{Txs: txs}) ethTxAsAny, err := anypb.New(&base.RepeatedEthTx{Txs: txs})
if err != nil {
b.Fatal(err)
}
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny}) res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny})
......
...@@ -323,6 +323,8 @@ func TestSizeOf(t *testing.T) { ...@@ -323,6 +323,8 @@ func TestSizeOf(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
fmt.Printf("%T \n", unsafe.Sizeof(*tx))
fmt.Println(unsafe.Sizeof(*tx)) fmt.Println(unsafe.Sizeof(*tx))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment