Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MetaProtocol
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nebula
MetaProtocol
Commits
3e0e80eb
Commit
3e0e80eb
authored
Dec 04, 2022
by
Ubuntu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add MB/s metric
parent
09da1d19
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
298 additions
and
40 deletions
+298
-40
benchmark_test.go
benchmark/benchmark_test.go
+257
-7
old.txt
benchmark/old.txt
+34
-33
stat_test.go
benchmark/stat_test.go
+5
-0
test_test.go
benchmark/test_test.go
+2
-0
No files found.
benchmark/benchmark_test.go
View file @
3e0e80eb
...
...
@@ -3,25 +3,40 @@ package benchmark_test
import
(
"fmt"
"math"
"sort"
"testing"
)
//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
)
{
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
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
//b.Logf("idx %d time: %s \n", i, time.Now())
fmt
.
Sprintf
(
"hello"
)
}
//
b.ResetTimer()
//
for i := 0; i < b.N; i++ {
//
//b.Logf("idx %d time: %s \n", i, time.Now())
//
fmt.Sprintf("hello")
//
}
//b.StopTimer()
//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
)
{
...
...
@@ -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)
// }
// })
// }
// }
benchmark/old.txt
View file @
3e0e80eb
...
...
@@ -2,36 +2,37 @@ goos: linux
goarch: amd64
pkg: github.com/CaduceusMetaverseProtocol/metaprotocol/benchmark
cpu: Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz
BenchmarkRepeated
BenchmarkRepeated/input_size_200
BenchmarkRepeated/input_size_200-8 4838 235895 ns/op 206960 B/op 1030 allocs/op
BenchmarkRepeated/input_size_200-8 5028 240751 ns/op 206712 B/op 1029 allocs/op
BenchmarkRepeated/input_size_200-8 5065 224936 ns/op 206671 B/op 1029 allocs/op
BenchmarkRepeated/input_size_200-8 4790 224242 ns/op 206669 B/op 1029 allocs/op
BenchmarkRepeated/input_size_200-8 4849 232470 ns/op 206667 B/op 1029 allocs/op
BenchmarkRepeated/input_size_500
BenchmarkRepeated/input_size_500-8 3127 380410 ns/op 294254 B/op 1652 allocs/op
BenchmarkRepeated/input_size_500-8 2966 374246 ns/op 294329 B/op 1652 allocs/op
BenchmarkRepeated/input_size_500-8 3009 393651 ns/op 294321 B/op 1651 allocs/op
BenchmarkRepeated/input_size_500-8 3063 372750 ns/op 294320 B/op 1651 allocs/op
BenchmarkRepeated/input_size_500-8 2985 393548 ns/op 294337 B/op 1652 allocs/op
BenchmarkRepeated/input_size_1000
BenchmarkRepeated/input_size_1000-8 1988 591392 ns/op 427929 B/op 2652 allocs/op
BenchmarkRepeated/input_size_1000-8 1899 612673 ns/op 427931 B/op 2652 allocs/op
BenchmarkRepeated/input_size_1000-8 1996 607685 ns/op 427922 B/op 2652 allocs/op
BenchmarkRepeated/input_size_1000-8 2022 607354 ns/op 427935 B/op 2652 allocs/op
BenchmarkRepeated/input_size_1000-8 2078 605480 ns/op 427925 B/op 2652 allocs/op
BenchmarkRepeated/input_size_2000
BenchmarkRepeated/input_size_2000-8 1226 1069362 ns/op 695546 B/op 4670 allocs/op
BenchmarkRepeated/input_size_2000-8 1147 1052716 ns/op 695544 B/op 4670 allocs/op
BenchmarkRepeated/input_size_2000-8 1077 1068582 ns/op 695582 B/op 4670 allocs/op
BenchmarkRepeated/input_size_2000-8 1087 1057335 ns/op 695547 B/op 4670 allocs/op
BenchmarkRepeated/input_size_2000-8 1106 1045020 ns/op 695558 B/op 4670 allocs/op
BenchmarkRepeated/input_size_5000
BenchmarkRepeated/input_size_5000-8 444 2444132 ns/op 1514766 B/op 10709 allocs/op
BenchmarkRepeated/input_size_5000-8 442 2406409 ns/op 1514738 B/op 10708 allocs/op
BenchmarkRepeated/input_size_5000-8 435 2440909 ns/op 1514581 B/op 10705 allocs/op
BenchmarkRepeated/input_size_5000-8 458 2390721 ns/op 1514734 B/op 10708 allocs/op
BenchmarkRepeated/input_size_5000-8 447 2439328 ns/op 1514725 B/op 10709 allocs/op
PASS
ok github.com/CaduceusMetaverseProtocol/metaprotocol/benchmark 34.716s
BenchmarkQueueEthTx-8 7838 149282 ns/op
--- BENCH: BenchmarkQueueEthTx-8
grpc-queue-tx_test.go:30: b.N: 1
grpc-queue-tx_test.go:109: response: 9fa50e67f63b7059a92a5ffe0f4f8201e88c38b572364f8140275b2f445c1b8f local: 9fa50e67f63b7059a92a5ffe0f4f8201e88c38b572364f8140275b2f445c1b8f
grpc-queue-tx_test.go:30: b.N: 100
grpc-queue-tx_test.go:30: b.N: 3288
grpc-queue-tx_test.go:30: b.N: 5540
grpc-queue-tx_test.go:30: b.N: 7838
BenchmarkQueueEthTx-8 10000 156028 ns/op
--- BENCH: BenchmarkQueueEthTx-8
grpc-queue-tx_test.go:30: b.N: 1
grpc-queue-tx_test.go:30: b.N: 100
grpc-queue-tx_test.go:30: b.N: 10000
BenchmarkQueueEthTx-8 10000 154042 ns/op
--- BENCH: BenchmarkQueueEthTx-8
grpc-queue-tx_test.go:30: b.N: 1
grpc-queue-tx_test.go:30: b.N: 100
grpc-queue-tx_test.go:30: b.N: 10000
BenchmarkQueueStdTx-8 10000 157969 ns/op
--- BENCH: BenchmarkQueueStdTx-8
grpc-queue-tx_test.go:118: b.N: 1
grpc-queue-tx_test.go:118: b.N: 100
grpc-queue-tx_test.go:118: b.N: 10000
BenchmarkQueueStdTx-8 10000 150678 ns/op
--- BENCH: BenchmarkQueueStdTx-8
grpc-queue-tx_test.go:118: b.N: 1
grpc-queue-tx_test.go:118: b.N: 100
grpc-queue-tx_test.go:118: b.N: 10000
BenchmarkQueueStdTx-8 10000 153660 ns/op
--- BENCH: BenchmarkQueueStdTx-8
grpc-queue-tx_test.go:118: b.N: 1
grpc-queue-tx_test.go:118: b.N: 100
grpc-queue-tx_test.go:118: b.N: 10000
BenchmarkQueueAnyTx-8
\ No newline at end of file
benchmark/stat_test.go
View file @
3e0e80eb
...
...
@@ -88,6 +88,7 @@ func RepeatedEthTx(txl int, b *testing.B) {
txs
:=
make
([]
*
base
.
TransactionEth
,
0
,
txl
)
for
i
:=
0
;
i
<
txl
;
i
++
{
b
.
SetBytes
(
88
)
txs
=
append
(
txs
,
&
base
.
TransactionEth
{
Tx
:
&
base
.
EthTx
{
Inner
:
&
inner
}})
}
...
...
@@ -170,10 +171,14 @@ func RepeatedTxEthAsAny(txl int, b *testing.B) {
txs
:=
make
([]
*
base
.
TransactionEth
,
0
,
txl
)
for
i
:=
0
;
i
<
txl
;
i
++
{
b
.
SetBytes
(
88
)
txs
=
append
(
txs
,
&
base
.
TransactionEth
{
Tx
:
&
base
.
EthTx
{
Inner
:
&
inner
}})
}
ethTxAsAny
,
err
:=
anypb
.
New
(
&
base
.
RepeatedEthTx
{
Txs
:
txs
})
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
res
,
err
:=
c
.
SendTxAsAny
(
ctx
,
&
base
.
Transaction
{
Tx
:
ethTxAsAny
})
...
...
benchmark/test_test.go
View file @
3e0e80eb
...
...
@@ -323,6 +323,8 @@ func TestSizeOf(t *testing.T) {
t
.
Fatal
(
err
)
}
fmt
.
Printf
(
"%T
\n
"
,
unsafe
.
Sizeof
(
*
tx
))
fmt
.
Println
(
unsafe
.
Sizeof
(
*
tx
))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment