mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 13:16:42 +00:00
common: op benchmark time by using testing.B.Loop
This commit is contained in:
parent
6492751355
commit
d42f77ba8b
7 changed files with 25 additions and 30 deletions
|
|
@ -119,8 +119,7 @@ func BenchmarkFastXOR4KB(b *testing.B) { benchmarkFastXOR(b, 4096) }
|
||||||
|
|
||||||
func benchmarkFastXOR(b *testing.B, size int) {
|
func benchmarkFastXOR(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
XORBytes(p, p, q)
|
XORBytes(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -132,8 +131,7 @@ func BenchmarkBaseXOR4KB(b *testing.B) { benchmarkBaseXOR(b, 4096) }
|
||||||
|
|
||||||
func benchmarkBaseXOR(b *testing.B, size int) {
|
func benchmarkBaseXOR(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
safeXORBytes(p, p, q)
|
safeXORBytes(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -145,8 +143,7 @@ func BenchmarkFastAND4KB(b *testing.B) { benchmarkFastAND(b, 4096) }
|
||||||
|
|
||||||
func benchmarkFastAND(b *testing.B, size int) {
|
func benchmarkFastAND(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ANDBytes(p, p, q)
|
ANDBytes(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -158,8 +155,7 @@ func BenchmarkBaseAND4KB(b *testing.B) { benchmarkBaseAND(b, 4096) }
|
||||||
|
|
||||||
func benchmarkBaseAND(b *testing.B, size int) {
|
func benchmarkBaseAND(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
safeANDBytes(p, p, q)
|
safeANDBytes(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -171,8 +167,7 @@ func BenchmarkFastOR4KB(b *testing.B) { benchmarkFastOR(b, 4096) }
|
||||||
|
|
||||||
func benchmarkFastOR(b *testing.B, size int) {
|
func benchmarkFastOR(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ORBytes(p, p, q)
|
ORBytes(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -184,8 +179,7 @@ func BenchmarkBaseOR4KB(b *testing.B) { benchmarkBaseOR(b, 4096) }
|
||||||
|
|
||||||
func benchmarkBaseOR(b *testing.B, size int) {
|
func benchmarkBaseOR(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
safeORBytes(p, p, q)
|
safeORBytes(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -200,7 +194,7 @@ func BenchmarkFastTest4KB(b *testing.B) { benchmarkFastTest(b, 4096) }
|
||||||
func benchmarkFastTest(b *testing.B, size int) {
|
func benchmarkFastTest(b *testing.B, size int) {
|
||||||
p := make([]byte, size)
|
p := make([]byte, size)
|
||||||
a := false
|
a := false
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
a = a != TestBytes(p)
|
a = a != TestBytes(p)
|
||||||
}
|
}
|
||||||
GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
|
GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
|
||||||
|
|
@ -214,7 +208,7 @@ func BenchmarkBaseTest4KB(b *testing.B) { benchmarkBaseTest(b, 4096) }
|
||||||
func benchmarkBaseTest(b *testing.B, size int) {
|
func benchmarkBaseTest(b *testing.B, size int) {
|
||||||
p := make([]byte, size)
|
p := make([]byte, size)
|
||||||
a := false
|
a := false
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
a = a != safeTestBytes(p)
|
a = a != safeTestBytes(p)
|
||||||
}
|
}
|
||||||
GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
|
GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
|
||||||
|
|
|
||||||
|
|
@ -178,9 +178,8 @@ func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
|
||||||
data[idx] |= 1 << bit
|
data[idx] |= 1 << bit
|
||||||
}
|
}
|
||||||
// Reset the benchmark and measure encoding/decoding
|
// Reset the benchmark and measure encoding/decoding
|
||||||
b.ResetTimer()
|
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
|
bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ func BenchmarkEncodeBig(b *testing.B) {
|
||||||
b.Run(bench.want, func(b *testing.B) {
|
b.Run(bench.want, func(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
bigint := bench.input.(*big.Int)
|
bigint := bench.input.(*big.Int)
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
EncodeBig(bigint)
|
EncodeBig(bigint)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ func TestUnmarshalBytes(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkUnmarshalBytes(b *testing.B) {
|
func BenchmarkUnmarshalBytes(b *testing.B) {
|
||||||
input := []byte(`"0x123456789abcdef123456789abcdef"`)
|
input := []byte(`"0x123456789abcdef123456789abcdef"`)
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
var v Bytes
|
var v Bytes
|
||||||
if err := v.UnmarshalJSON(input); err != nil {
|
if err := v.UnmarshalJSON(input); err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
|
|
@ -239,7 +239,7 @@ func TestUnmarshalU256(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkUnmarshalBig(b *testing.B) {
|
func BenchmarkUnmarshalBig(b *testing.B) {
|
||||||
input := []byte(`"0x123456789abcdef123456789abcdef"`)
|
input := []byte(`"0x123456789abcdef123456789abcdef"`)
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
var v Big
|
var v Big
|
||||||
if err := v.UnmarshalJSON(input); err != nil {
|
if err := v.UnmarshalJSON(input); err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
|
|
@ -305,7 +305,7 @@ func TestUnmarshalUint64(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkUnmarshalUint64(b *testing.B) {
|
func BenchmarkUnmarshalUint64(b *testing.B) {
|
||||||
input := []byte(`"0x123456789abcdf"`)
|
input := []byte(`"0x123456789abcdf"`)
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
var v Uint64
|
var v Uint64
|
||||||
v.UnmarshalJSON(input)
|
v.UnmarshalJSON(input)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,8 +206,10 @@ func BenchmarkLRU(b *testing.B) {
|
||||||
|
|
||||||
b.Run("Add/BasicLRU", func(b *testing.B) {
|
b.Run("Add/BasicLRU", func(b *testing.B) {
|
||||||
cache := NewBasicLRU[int, int](capacity)
|
cache := NewBasicLRU[int, int](capacity)
|
||||||
for i := 0; i < b.N; i++ {
|
i := 0
|
||||||
|
for b.Loop() {
|
||||||
cache.Add(i, i)
|
cache.Add(i, i)
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
b.Run("Get/BasicLRU", func(b *testing.B) {
|
b.Run("Get/BasicLRU", func(b *testing.B) {
|
||||||
|
|
@ -217,13 +219,14 @@ func BenchmarkLRU(b *testing.B) {
|
||||||
cache.Add(keys[index], values[index])
|
cache.Add(keys[index], values[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
b.ResetTimer()
|
i := 0
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
k := keys[indexes[i%len(indexes)]]
|
k := keys[indexes[i%len(indexes)]]
|
||||||
v, ok := cache.Get(k)
|
v, ok := cache.Get(k)
|
||||||
if ok {
|
if ok {
|
||||||
sink = v
|
sink = v
|
||||||
}
|
}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,28 +88,28 @@ func TestPaddedBigBytes(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
|
func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
|
||||||
bigint := MustParseBig256("123456789123456789123456789123456789")
|
bigint := MustParseBig256("123456789123456789123456789123456789")
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
PaddedBigBytes(bigint, 200)
|
PaddedBigBytes(bigint, 200)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
|
func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
|
||||||
bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
|
bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
PaddedBigBytes(bigint, 5)
|
PaddedBigBytes(bigint, 5)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
|
func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
|
||||||
bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
|
bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
PaddedBigBytes(bigint, 32)
|
PaddedBigBytes(bigint, 32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkByteAtOld(b *testing.B) {
|
func BenchmarkByteAtOld(b *testing.B) {
|
||||||
bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
|
bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
|
||||||
for i := 0; i < b.N; i++ {
|
for b.Loop() {
|
||||||
PaddedBigBytes(bigint, 32)
|
PaddedBigBytes(bigint, 32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ func TestAddressHexChecksum(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkAddressHex(b *testing.B) {
|
func BenchmarkAddressHex(b *testing.B) {
|
||||||
testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
|
testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
|
||||||
for n := 0; n < b.N; n++ {
|
for b.Loop() {
|
||||||
testAddr.Hex()
|
testAddr.Hex()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -590,8 +590,7 @@ func BenchmarkPrettyDuration(b *testing.B) {
|
||||||
var x = PrettyDuration(time.Duration(int64(1203123912312)))
|
var x = PrettyDuration(time.Duration(int64(1203123912312)))
|
||||||
b.Logf("Pre %s", time.Duration(x).String())
|
b.Logf("Pre %s", time.Duration(x).String())
|
||||||
var a string
|
var a string
|
||||||
b.ResetTimer()
|
for b.Loop() {
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
a = x.String()
|
a = x.String()
|
||||||
}
|
}
|
||||||
b.Logf("Post %s", a)
|
b.Logf("Post %s", a)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue