jit_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "math/big"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. )
  26. const maxRun = 1000
  27. type vmBench struct {
  28. precompile bool // compile prior to executing
  29. nojit bool // ignore jit (sets DisbaleJit = true
  30. forcejit bool // forces the jit, precompile is ignored
  31. code []byte
  32. input []byte
  33. }
  34. func runVmBench(test vmBench, b *testing.B) {
  35. db, _ := ethdb.NewMemDatabase()
  36. sender := state.NewStateObject(common.Address{}, db)
  37. if test.precompile && !test.forcejit {
  38. NewProgram(test.code)
  39. }
  40. env := NewEnv()
  41. EnableJit = !test.nojit
  42. ForceJit = test.forcejit
  43. b.ResetTimer()
  44. for i := 0; i < b.N; i++ {
  45. context := NewContext(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
  46. context.Code = test.code
  47. context.CodeAddr = &common.Address{}
  48. _, err := New(env).Run(context, test.input)
  49. if err != nil {
  50. b.Error(err)
  51. b.FailNow()
  52. }
  53. }
  54. }
  55. var benchmarks = map[string]vmBench{
  56. "pushes": vmBench{
  57. false, false, false,
  58. common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
  59. },
  60. }
  61. func BenchmarkPushes(b *testing.B) {
  62. runVmBench(benchmarks["pushes"], b)
  63. }
  64. type Env struct {
  65. gasLimit *big.Int
  66. depth int
  67. }
  68. func NewEnv() *Env {
  69. return &Env{big.NewInt(10000), 0}
  70. }
  71. func (self *Env) Origin() common.Address { return common.Address{} }
  72. func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
  73. func (self *Env) AddStructLog(log StructLog) {
  74. }
  75. func (self *Env) StructLogs() []StructLog {
  76. return nil
  77. }
  78. //func (self *Env) PrevHash() []byte { return self.parent }
  79. func (self *Env) Coinbase() common.Address { return common.Address{} }
  80. func (self *Env) Time() *big.Int { return big.NewInt(time.Now().Unix()) }
  81. func (self *Env) Difficulty() *big.Int { return big.NewInt(0) }
  82. func (self *Env) State() *state.StateDB { return nil }
  83. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  84. func (self *Env) VmType() Type { return StdVmTy }
  85. func (self *Env) GetHash(n uint64) common.Hash {
  86. return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
  87. }
  88. func (self *Env) AddLog(log *state.Log) {
  89. }
  90. func (self *Env) Depth() int { return self.depth }
  91. func (self *Env) SetDepth(i int) { self.depth = i }
  92. func (self *Env) CanTransfer(from Account, balance *big.Int) bool {
  93. return from.Balance().Cmp(balance) >= 0
  94. }
  95. func (self *Env) Transfer(from, to Account, amount *big.Int) error {
  96. return nil
  97. }
  98. func (self *Env) Call(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  99. return nil, nil
  100. }
  101. func (self *Env) CallCode(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  102. return nil, nil
  103. }
  104. func (self *Env) Create(caller ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) {
  105. return nil, nil, nil
  106. }