github.com/klaytn/klaytn@v1.10.2/tests/benchmarks/benchmarks_test.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package benchmarks
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/klaytn/klaytn/common"
    23  )
    24  
    25  func TestInterpreterMload100000(t *testing.T) {
    26  	//
    27  	// Test code
    28  	//       Initialize memory with memory write (PUSH PUSH MSTORE)
    29  	//       Loop 10000 times for below code
    30  	//              memory read 10 times //  (PUSH MLOAD POP) x 10
    31  	//
    32  	code := common.Hex2Bytes(mload100000)
    33  	intrp, contract := prepareInterpreterAndContract(code)
    34  
    35  	intrp.Run(contract, nil)
    36  }
    37  
    38  func BenchmarkInterpreterMload100000(bench *testing.B) {
    39  	//
    40  	// Test code
    41  	//       Initialize memory with memory write (PUSH PUSH MSTORE)
    42  	//       Loop 10000 times for below code
    43  	//              memory read 10 times //  (PUSH MLOAD POP) x 10
    44  	//
    45  	code := common.Hex2Bytes("60ca60205260005b612710811015630000004557602051506020515060205150602051506020515060205150602051506020515060205150602051506001016300000007565b00")
    46  	intrp, contract := prepareInterpreterAndContract(code)
    47  
    48  	bench.ResetTimer()
    49  	for i := 0; i < bench.N; i++ {
    50  		intrp.Run(contract, nil)
    51  	}
    52  }
    53  
    54  func TestInterpreterMstore100000(t *testing.T) {
    55  	//
    56  	// Test code
    57  	//       Initialize memory with memory write (PUSH PUSH MSTORE)
    58  	//       Loop 10000 times for below code
    59  	//              memory write 10 times //  (PUSH PUSH MSTORE) x 10
    60  	//
    61  	code := common.Hex2Bytes(mstore100000)
    62  	intrp, contract := prepareInterpreterAndContract(code)
    63  
    64  	intrp.Run(contract, nil)
    65  }
    66  
    67  func BenchmarkInterpreterMstore100000(bench *testing.B) {
    68  	//
    69  	// Test code
    70  	//       Initialize memory with memory write (PUSH PUSH MSTORE)
    71  	//       Loop 10000 times for below code
    72  	//              memory write 10 times //  (PUSH PUSH MSTORE) x 10
    73  	//
    74  	code := common.Hex2Bytes(mstore100000)
    75  	intrp, contract := prepareInterpreterAndContract(code)
    76  
    77  	bench.ResetTimer()
    78  	for i := 0; i < bench.N; i++ {
    79  		intrp.Run(contract, nil)
    80  	}
    81  }
    82  
    83  func TestInterpreterSload100000(t *testing.T) {
    84  	//
    85  	// Test code
    86  	//       Initialize (PUSH SSTORE)
    87  	//       Loop 10000 times for below code
    88  	//              Read from database 10 times //  (PUSH SLOAD POP) x 10
    89  	//
    90  	code := common.Hex2Bytes(sload100000)
    91  	intrp, contract := prepareInterpreterAndContract(code)
    92  
    93  	intrp.Run(contract, nil)
    94  }
    95  
    96  func BenchmarkInterpreterSload100000(bench *testing.B) {
    97  	//
    98  	// Test code
    99  	//       Initialize (PUSH SSTORE)
   100  	//       Loop 10000 times for below code
   101  	//              Read from database 10 times //  (PUSH SLOAD POP) x 10
   102  	//
   103  	code := common.Hex2Bytes(sload100000)
   104  	intrp, contract := prepareInterpreterAndContract(code)
   105  
   106  	bench.ResetTimer()
   107  	for i := 0; i < bench.N; i++ {
   108  		intrp.Run(contract, nil)
   109  	}
   110  }
   111  
   112  func TestInterpreterSstoreNonZero2NonZero100000(t *testing.T) {
   113  	//
   114  	// Test code
   115  	//       Initialize (PUSH)
   116  	//       Loop 10000 times for below code
   117  	//              Write to database 10 times //  (PUSH PUSH SSTORE) x 10
   118  	//
   119  	code := common.Hex2Bytes(sstoreNonZero2NonZero100000)
   120  	intrp, contract := prepareInterpreterAndContract(code)
   121  
   122  	intrp.Run(contract, nil)
   123  }
   124  
   125  func BenchmarkInterpreterSstoreZero2Zero100000(bench *testing.B) {
   126  	//
   127  	// Test code
   128  	//       Initialize (PUSH)
   129  	//       Loop 10000 times for below code
   130  	//              Write to database 10 times //  (PUSH PUSH SSTORE) x 10
   131  	//
   132  	code := common.Hex2Bytes(sstoreZero2Zero100000)
   133  	intrp, contract := prepareInterpreterAndContract(code)
   134  
   135  	bench.ResetTimer()
   136  	for i := 0; i < bench.N; i++ {
   137  		intrp.Run(contract, nil)
   138  	}
   139  }
   140  
   141  func BenchmarkInterpreterSstoreNonZero2NonZero100000(bench *testing.B) {
   142  	//
   143  	// Test code
   144  	//       Initialize (PUSH)
   145  	//       Loop 10000 times for below code
   146  	//              Write to database 10 times //  (PUSH PUSH SSTORE) x 10
   147  	//
   148  	code := common.Hex2Bytes(sstoreNonZero2NonZero100000)
   149  	intrp, contract := prepareInterpreterAndContract(code)
   150  
   151  	bench.ResetTimer()
   152  	for i := 0; i < bench.N; i++ {
   153  		intrp.Run(contract, nil)
   154  	}
   155  }
   156  
   157  func BenchmarkInterpreterSstoreMixed100000(bench *testing.B) {
   158  	//
   159  	// Test code
   160  	//       Initialize (PUSH)
   161  	//       Loop 10000 times for below code
   162  	//              Write to database 10 times //  (PUSH PUSH SSTORE) x 10
   163  	//
   164  	code := common.Hex2Bytes(sstoreMixed100000)
   165  	intrp, contract := prepareInterpreterAndContract(code)
   166  
   167  	bench.ResetTimer()
   168  	for i := 0; i < bench.N; i++ {
   169  		intrp.Run(contract, nil)
   170  	}
   171  }
   172  
   173  func TestInterpreterAdd100000(t *testing.T) {
   174  	//
   175  	// Test code
   176  	//       Initialize value (PUSH)
   177  	//       Loop 10000 times for below code
   178  	//              initialize value and increment 10 times //  (PUSH x 1) + ((PUSH ADD) x 10)
   179  	//
   180  	code := common.Hex2Bytes(add100000)
   181  	intrp, contract := prepareInterpreterAndContract(code)
   182  
   183  	intrp.Run(contract, nil)
   184  }
   185  
   186  func BenchmarkInterpreterAdd100000(bench *testing.B) {
   187  	//
   188  	// Test code
   189  	//       Initialize value (PUSH)
   190  	//       Loop 10000 times for below code
   191  	//              initialize value and increment 10 times //  (PUSH x 1) + ((PUSH ADD) x 10)
   192  	//
   193  	code := common.Hex2Bytes(add100000)
   194  	intrp, contract := prepareInterpreterAndContract(code)
   195  
   196  	bench.ResetTimer()
   197  	for i := 0; i < bench.N; i++ {
   198  		intrp.Run(contract, nil)
   199  	}
   200  }
   201  
   202  func TestInterpreterPush1Mul1byte100000(t *testing.T) {
   203  	//
   204  	// Test code
   205  	//       Initialize value (PUSH)
   206  	//       Loop 10000 times for below code
   207  	//              initialize value and increment 10 times //  (PUSH1 PUSH1 MUL POP) x 10
   208  	//
   209  	code := common.Hex2Bytes(push1mul1byte100000)
   210  	intrp, contract := prepareInterpreterAndContract(code)
   211  
   212  	intrp.Run(contract, nil)
   213  }
   214  
   215  func BenchmarkInterpreterPush1Mul1byte100000(bench *testing.B) {
   216  	//
   217  	// Test code
   218  	//       Initialize value (PUSH)
   219  	//       Loop 10000 times for below code
   220  	//              initialize value and increment 10 times //  (PUSH1 PUSH1 MUL POP) x 10
   221  	//
   222  	code := common.Hex2Bytes(push1mul1byte100000)
   223  	intrp, contract := prepareInterpreterAndContract(code)
   224  
   225  	bench.ResetTimer()
   226  	for i := 0; i < bench.N; i++ {
   227  		intrp.Run(contract, nil)
   228  	}
   229  }
   230  
   231  func BenchmarkInterpreterPush5Mul1byte100000(bench *testing.B) {
   232  	//
   233  	// Test code
   234  	//       Initialize value (PUSH)
   235  	//       Loop 10000 times for below code
   236  	//              initialize value and increment 10 times //  (PUSH5 PUSH5 MUL POP) x 10
   237  	//
   238  	code := common.Hex2Bytes(push5mul1byte100000)
   239  	intrp, contract := prepareInterpreterAndContract(code)
   240  
   241  	bench.ResetTimer()
   242  	for i := 0; i < bench.N; i++ {
   243  		intrp.Run(contract, nil)
   244  	}
   245  }
   246  
   247  func BenchmarkInterpreterPush5Mul5bytes100000(bench *testing.B) {
   248  	//
   249  	// Test code
   250  	//       Initialize value (PUSH)
   251  	//       Loop 10000 times for below code
   252  	//              initialize value and increment 10 times //  (PUSH5 PUSH5 MUL POP) x 10
   253  	//
   254  	code := common.Hex2Bytes(push5mul5bytes100000)
   255  	intrp, contract := prepareInterpreterAndContract(code)
   256  
   257  	bench.ResetTimer()
   258  	for i := 0; i < bench.N; i++ {
   259  		intrp.Run(contract, nil)
   260  	}
   261  }
   262  
   263  func TestInterpreterPush1Div1byte100000(t *testing.T) {
   264  	//
   265  	// Test code
   266  	//       Initialize value (PUSH)
   267  	//       Loop 10000 times for below code
   268  	//              initialize value and increment 10 times //  (PUSH1 PUSH1 DIV POP) x 10
   269  	//
   270  	code := common.Hex2Bytes(push1div1byte100000)
   271  	intrp, contract := prepareInterpreterAndContract(code)
   272  
   273  	intrp.Run(contract, nil)
   274  }
   275  
   276  func BenchmarkInterpreterPush1Div1byte100000(bench *testing.B) {
   277  	//
   278  	// Test code
   279  	//       Initialize value (PUSH)
   280  	//       Loop 10000 times for below code
   281  	//              initialize value and increment 10 times //  (PUSH1 PUSH1 DIV POP) x 10
   282  	//
   283  	code := common.Hex2Bytes(push1div1byte100000)
   284  	intrp, contract := prepareInterpreterAndContract(code)
   285  
   286  	bench.ResetTimer()
   287  	for i := 0; i < bench.N; i++ {
   288  		intrp.Run(contract, nil)
   289  	}
   290  }
   291  
   292  func BenchmarkInterpreterPush5Div1byte100000(bench *testing.B) {
   293  	//
   294  	// Test code
   295  	//       Initialize value (PUSH)
   296  	//       Loop 10000 times for below code
   297  	//              initialize value and increment 10 times //  (PUSH5 PUSH5 DIV POP) x 10
   298  	//
   299  	code := common.Hex2Bytes(push5div1byte100000)
   300  	intrp, contract := prepareInterpreterAndContract(code)
   301  
   302  	bench.ResetTimer()
   303  	for i := 0; i < bench.N; i++ {
   304  		intrp.Run(contract, nil)
   305  	}
   306  }
   307  
   308  func BenchmarkInterpreterPush5Div5bytes100000(bench *testing.B) {
   309  	//
   310  	// Test code
   311  	//       Initialize value (PUSH)
   312  	//       Loop 10000 times for below code
   313  	//              initialize value and increment 10 times //  (PUSH5 PUSH5 DIV POP) x 10
   314  	//
   315  	code := common.Hex2Bytes(push5div5bytes100000)
   316  	intrp, contract := prepareInterpreterAndContract(code)
   317  
   318  	bench.ResetTimer()
   319  	for i := 0; i < bench.N; i++ {
   320  		intrp.Run(contract, nil)
   321  	}
   322  }