github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/core/vm/jit_test.go (about)

     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  
    18  import (
    19  	"math/big"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/state"
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  	"github.com/ethereum/go-ethereum/ethdb"
    27  )
    28  
    29  const maxRun = 1000
    30  
    31  type vmBench struct {
    32  	precompile bool // compile prior to executing
    33  	nojit      bool // ignore jit (sets DisbaleJit = true
    34  	forcejit   bool // forces the jit, precompile is ignored
    35  
    36  	code  []byte
    37  	input []byte
    38  }
    39  
    40  func runVmBench(test vmBench, b *testing.B) {
    41  	db, _ := ethdb.NewMemDatabase()
    42  	sender := state.NewStateObject(common.Address{}, db)
    43  
    44  	if test.precompile && !test.forcejit {
    45  		NewProgram(test.code)
    46  	}
    47  	env := NewEnv()
    48  
    49  	EnableJit = !test.nojit
    50  	ForceJit = test.forcejit
    51  
    52  	b.ResetTimer()
    53  
    54  	for i := 0; i < b.N; i++ {
    55  		context := NewContext(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
    56  		context.Code = test.code
    57  		context.CodeAddr = &common.Address{}
    58  		_, err := New(env).Run(context, test.input)
    59  		if err != nil {
    60  			b.Error(err)
    61  			b.FailNow()
    62  		}
    63  	}
    64  }
    65  
    66  var benchmarks = map[string]vmBench{
    67  	"pushes": vmBench{
    68  		false, false, false,
    69  		common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
    70  	},
    71  }
    72  
    73  func BenchmarkPushes(b *testing.B) {
    74  	runVmBench(benchmarks["pushes"], b)
    75  }
    76  
    77  type Env struct {
    78  	gasLimit *big.Int
    79  	depth    int
    80  }
    81  
    82  func NewEnv() *Env {
    83  	return &Env{big.NewInt(10000), 0}
    84  }
    85  
    86  func (self *Env) Origin() common.Address { return common.Address{} }
    87  func (self *Env) BlockNumber() *big.Int  { return big.NewInt(0) }
    88  func (self *Env) AddStructLog(log StructLog) {
    89  }
    90  func (self *Env) StructLogs() []StructLog {
    91  	return nil
    92  }
    93  
    94  //func (self *Env) PrevHash() []byte      { return self.parent }
    95  func (self *Env) Coinbase() common.Address { return common.Address{} }
    96  func (self *Env) Time() *big.Int           { return big.NewInt(time.Now().Unix()) }
    97  func (self *Env) Difficulty() *big.Int     { return big.NewInt(0) }
    98  func (self *Env) State() *state.StateDB    { return nil }
    99  func (self *Env) GasLimit() *big.Int       { return self.gasLimit }
   100  func (self *Env) VmType() Type             { return StdVmTy }
   101  func (self *Env) GetHash(n uint64) common.Hash {
   102  	return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
   103  }
   104  func (self *Env) AddLog(log *state.Log) {
   105  }
   106  func (self *Env) Depth() int     { return self.depth }
   107  func (self *Env) SetDepth(i int) { self.depth = i }
   108  func (self *Env) CanTransfer(from Account, balance *big.Int) bool {
   109  	return from.Balance().Cmp(balance) >= 0
   110  }
   111  func (self *Env) Transfer(from, to Account, amount *big.Int) error {
   112  	return nil
   113  }
   114  func (self *Env) Call(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
   115  	return nil, nil
   116  }
   117  func (self *Env) CallCode(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
   118  	return nil, nil
   119  }
   120  func (self *Env) Create(caller ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) {
   121  	return nil, nil, nil
   122  }