github.com/klaytn/klaytn@v1.12.1/blockchain/types/signing_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 types
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/klaytn/klaytn/common"
    24  	"github.com/klaytn/klaytn/crypto"
    25  )
    26  
    27  func BenchmarkSingleRecoverEIP155Signer(b *testing.B) {
    28  	signer := NewEIP155Signer(big.NewInt(2018))
    29  
    30  	from, _ := crypto.GenerateKey()
    31  	to, _ := crypto.GenerateKey()
    32  
    33  	toAddr := crypto.PubkeyToAddress(to.PublicKey)
    34  
    35  	tx := NewTransaction(
    36  		0,
    37  		toAddr,
    38  		big.NewInt(1),
    39  		100000,
    40  		big.NewInt(0),
    41  		nil)
    42  
    43  	signTx, _ := SignTx(tx, NewEIP155Signer(big.NewInt(2018)), from)
    44  
    45  	b.ResetTimer()
    46  	for i := 0; i < b.N; i++ {
    47  		_, _ = signer.Sender(signTx)
    48  	}
    49  }
    50  
    51  func BenchmarkDoubleRecoverEIP155Signer(b *testing.B) {
    52  	signer := NewEIP155Signer(big.NewInt(2018))
    53  
    54  	from, _ := crypto.GenerateKey()
    55  	to, _ := crypto.GenerateKey()
    56  
    57  	toAddr := crypto.PubkeyToAddress(to.PublicKey)
    58  
    59  	tx := NewTransaction(
    60  		0,
    61  		toAddr,
    62  		big.NewInt(1),
    63  		100000,
    64  		big.NewInt(0),
    65  		nil)
    66  
    67  	signTx, _ := SignTx(tx, NewEIP155Signer(big.NewInt(2018)), from)
    68  
    69  	b.ResetTimer()
    70  	for i := 0; i < b.N; i++ {
    71  		_, _ = signer.Sender(signTx)
    72  		_, _ = signer.Sender(signTx)
    73  	}
    74  }
    75  
    76  func BenchmarkDoubleRecoverEIP155SignerDoubleGoroutine(b *testing.B) {
    77  	signer := NewEIP155Signer(big.NewInt(2018))
    78  
    79  	from, _ := crypto.GenerateKey()
    80  	to, _ := crypto.GenerateKey()
    81  
    82  	toAddr := crypto.PubkeyToAddress(to.PublicKey)
    83  
    84  	tx := NewTransaction(
    85  		0,
    86  		toAddr,
    87  		big.NewInt(1),
    88  		100000,
    89  		big.NewInt(0),
    90  		nil)
    91  
    92  	signTx, _ := SignTx(tx, NewEIP155Signer(big.NewInt(2018)), from)
    93  
    94  	b.ResetTimer()
    95  	for i := 0; i < b.N; i++ {
    96  		channel1 := make(chan common.Address)
    97  		channel2 := make(chan common.Address)
    98  		go func() {
    99  			addr, _ := signer.Sender(signTx)
   100  			channel1 <- addr
   101  		}()
   102  		go func() {
   103  			addr, _ := signer.Sender(signTx)
   104  			channel2 <- addr
   105  		}()
   106  		_ = <-channel1
   107  		_ = <-channel2
   108  	}
   109  }
   110  
   111  func BenchmarkDoubleRecoverEIP155SignerSingleGoroutine(b *testing.B) {
   112  	signer := NewEIP155Signer(big.NewInt(2018))
   113  
   114  	from, _ := crypto.GenerateKey()
   115  	to, _ := crypto.GenerateKey()
   116  
   117  	toAddr := crypto.PubkeyToAddress(to.PublicKey)
   118  
   119  	tx := NewTransaction(
   120  		0,
   121  		toAddr,
   122  		big.NewInt(1),
   123  		100000,
   124  		big.NewInt(0),
   125  		nil)
   126  
   127  	signTx, _ := SignTx(tx, NewEIP155Signer(big.NewInt(2018)), from)
   128  
   129  	b.ResetTimer()
   130  	for i := 0; i < b.N; i++ {
   131  		channel1 := make(chan common.Address)
   132  		go func() {
   133  			addr, _ := signer.Sender(signTx)
   134  			channel1 <- addr
   135  		}()
   136  		_, _ = signer.Sender(signTx)
   137  		_ = <-channel1
   138  	}
   139  }
   140  
   141  type param struct {
   142  	signTx   *Transaction
   143  	addrChan chan common.Address
   144  }
   145  
   146  func launchSenderGoroutines(signer *EIP155Signer, num int, paramCh chan param, quitCh chan struct{}) {
   147  	for i := 0; i < num; i++ {
   148  		go func() {
   149  			for {
   150  				select {
   151  				case p := <-paramCh:
   152  					addr, _ := signer.Sender(p.signTx)
   153  					p.addrChan <- addr
   154  				case <-quitCh:
   155  					return
   156  				}
   157  			}
   158  		}()
   159  	}
   160  }
   161  
   162  func BenchmarkDoubleRecoverEIP155SignerReusedGoroutines(b *testing.B) {
   163  	signer := NewEIP155Signer(big.NewInt(2018))
   164  
   165  	from, _ := crypto.GenerateKey()
   166  	to, _ := crypto.GenerateKey()
   167  
   168  	toAddr := crypto.PubkeyToAddress(to.PublicKey)
   169  
   170  	tx := NewTransaction(
   171  		0,
   172  		toAddr,
   173  		big.NewInt(1),
   174  		100000,
   175  		big.NewInt(0),
   176  		nil)
   177  
   178  	signTx, _ := SignTx(tx, NewEIP155Signer(big.NewInt(2018)), from)
   179  
   180  	numGoroutine := 2
   181  	paramCh := make(chan param, numGoroutine)
   182  
   183  	quitCh := make(chan struct{}, numGoroutine)
   184  	launchSenderGoroutines(&signer, numGoroutine, paramCh, quitCh)
   185  
   186  	ch0 := make(chan common.Address)
   187  	ch1 := make(chan common.Address)
   188  
   189  	b.ResetTimer()
   190  	for i := 0; i < b.N; i++ {
   191  		paramCh <- param{signTx, ch0}
   192  		paramCh <- param{signTx, ch1}
   193  
   194  		_ = <-ch0
   195  		_ = <-ch1
   196  	}
   197  	b.StopTimer()
   198  
   199  	for i := 0; i < numGoroutine; i++ {
   200  		quitCh <- struct{}{}
   201  	}
   202  }