From 83be52eb724e579e811dd0d3fc2ac864ec2de6f7 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 19 Jun 2017 10:46:25 +0200 Subject: [PATCH] core/vm : benchmarking precompiles, fix gas-error --- core/vm/instructions_test.go | 187 +++++++++++++++++------------------ 1 file changed, 90 insertions(+), 97 deletions(-) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 5fef92a51b..03c42c561c 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -42,63 +42,56 @@ func TestByteOp(t *testing.T) { } } -func getBenchmarker(op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) func(b *testing.B) { - x := func(bench *testing.B) { - var ( - env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) - stack = newstack() - ) - // convert args - byteArgs := make([][]byte, len(args)) - for i, arg := range args { - byteArgs[i] = common.Hex2Bytes(arg) - } - pc := uint64(0) - bench.ResetTimer() - for i := 0; i < bench.N; i++ { - for _, arg := range byteArgs { - a := new(big.Int).SetBytes(arg) - stack.push(a) - } - op(&pc, env, nil, nil, stack) - stack.pop() - } +func opBenchmark(bench *testing.B, op func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error), args ...string) { + var ( + env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) + stack = newstack() + ) + // convert args + byteArgs := make([][]byte, len(args)) + for i, arg := range args { + byteArgs[i] = common.Hex2Bytes(arg) + } + pc := uint64(0) + bench.ResetTimer() + for i := 0; i < bench.N; i++ { + for _, arg := range byteArgs { + a := new(big.Int).SetBytes(arg) + stack.push(a) + } + op(&pc, env, nil, nil, stack) + stack.pop() } - return x } -func precompiledBenchMarker(addr, input, expected string) func(*testing.B) { +func precompiledBenchmark(addr, input, expected string, gas uint64, bench *testing.B) { - x := func(bench *testing.B) { - - contract := NewContract( - AccountRef(common.HexToAddress("1337")), - nil, - new(big.Int), - 4000000) - - p := PrecompiledContracts[common.HexToAddress(addr)] - in := common.Hex2Bytes(input) - - //Check if it is correct - res, err := RunPrecompiledContract(p, in, contract) - if err != nil { - bench.Error(err) - return - } - if common.Bytes2Hex(res) != expected { - bench.Error(fmt.Sprintf("Expected %v, got %v", expected, common.Bytes2Hex(res))) - return - } - data := make([]byte, len(in)) - bench.ResetTimer() - for i := 0; i < bench.N; i++ { - copy(data, in) - RunPrecompiledContract(p, data, contract) - } + contract := NewContract(AccountRef(common.HexToAddress("1337")), + nil, new(big.Int), gas) + p := PrecompiledContracts[common.HexToAddress(addr)] + in := common.Hex2Bytes(input) + var ( + res []byte + err error + ) + data := make([]byte, len(in)) + bench.ResetTimer() + for i := 0; i < bench.N; i++ { + contract.Gas = gas + copy(data, in) + res, err = RunPrecompiledContract(p, data, contract) + } + bench.StopTimer() + //Check if it is correct + if err != nil { + bench.Error(err) + return + } + if common.Bytes2Hex(res) != expected { + bench.Error(fmt.Sprintf("Expected %v, got %v", expected, common.Bytes2Hex(res))) + return } - return x } func BenchmarkPrecompiledEcdsa(bench *testing.B) { @@ -106,162 +99,162 @@ func BenchmarkPrecompiledEcdsa(bench *testing.B) { addr = "01" inp = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02" exp = "000000000000000000000000ceaccac640adf55b2028469bd36ba501f28b699d" + gas = uint64(4000000) ) - precompiledBenchMarker(addr, inp, exp)(bench) + precompiledBenchmark(addr, inp, exp, gas, bench) } func BenchmarkPrecompiledSha256(bench *testing.B) { var ( addr = "02" inp = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02" exp = "811c7003375852fabd0d362e40e68607a12bdabae61a7d068fe5fdd1dbbf2a5d" + gas = uint64(4000000) ) - precompiledBenchMarker(addr, inp, exp)(bench) + precompiledBenchmark(addr, inp, exp, gas, bench) } func BenchmarkPrecompiledRipeMD(bench *testing.B) { var ( addr = "03" inp = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02" exp = "0000000000000000000000009215b8d9882ff46f0dfde6684d78e831467f65e6" + gas = uint64(4000000) ) - precompiledBenchMarker(addr, inp, exp)(bench) + precompiledBenchmark(addr, inp, exp, gas, bench) } func BenchmarkPrecompiledIdentity(bench *testing.B) { var ( addr = "04" inp = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02" exp = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02" + gas = uint64(4000000) ) - precompiledBenchMarker(addr, inp, exp)(bench) + precompiledBenchmark(addr, inp, exp, gas, bench) } func BenchmarkOpAdd(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opAdd, x, y) - f(b) + opBenchmark(b, opAdd, x, y) + } func BenchmarkOpSub(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opSub, x, y) - f(b) + opBenchmark(b, opSub, x, y) + } func BenchmarkOpMul(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opMul, x, y) + opBenchmark(b, opMul, x, y) - f(b) } func BenchmarkOpDiv(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opDiv, x, y) - f(b) + opBenchmark(b, opDiv, x, y) + } func BenchmarkOpSdiv(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opSdiv, x, y) - f(b) + opBenchmark(b, opSdiv, x, y) + } func BenchmarkOpMod(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opMod, x, y) - f(b) + opBenchmark(b, opMod, x, y) + } func BenchmarkOpSmod(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opSmod, x, y) - f(b) + opBenchmark(b, opSmod, x, y) + } func BenchmarkOpExp(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opExp, x, y) + opBenchmark(b, opExp, x, y) - f(b) } func BenchmarkOpSignExtend(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opSignExtend, x, y) + opBenchmark(b, opSignExtend, x, y) - f(b) } func BenchmarkOpLt(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opLt, x, y) - f(b) + opBenchmark(b, opLt, x, y) + } func BenchmarkOpGt(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opGt, x, y) - f(b) + opBenchmark(b, opGt, x, y) + } func BenchmarkOpSlt(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opSlt, x, y) - f(b) + opBenchmark(b, opSlt, x, y) + } func BenchmarkOpSgt(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opSgt, x, y) + opBenchmark(b, opSgt, x, y) - f(b) } func BenchmarkOpEq(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opEq, x, y) - f(b) + opBenchmark(b, opEq, x, y) + } func BenchmarkOpAnd(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opAnd, x, y) - f(b) + opBenchmark(b, opAnd, x, y) + } func BenchmarkOpOr(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opOr, x, y) - f(b) + opBenchmark(b, opOr, x, y) + } func BenchmarkOpXor(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opXor, x, y) - f(b) + opBenchmark(b, opXor, x, y) + } func BenchmarkOpByte(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opByte, x, y) - f(b) + opBenchmark(b, opByte, x, y) + } func BenchmarkOpAddmod(b *testing.B) { @@ -269,23 +262,23 @@ func BenchmarkOpAddmod(b *testing.B) { y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opAddmod, x, y, z) - f(b) + opBenchmark(b, opAddmod, x, y, z) + } func BenchmarkOpMulmod(b *testing.B) { x := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" y := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" z := "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" - f := getBenchmarker(opMulmod, x, y, z) - f(b) + opBenchmark(b, opMulmod, x, y, z) + } //func BenchmarkOpSha3(b *testing.B) { // x := "0" // y := "32" // -// f := getBenchmarker(opSha3, x, y) +// opBenchmark(b,opSha3, x, y) +// // -// f(b) //}