bench_gas.py (5736B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 import datetime 7 8 # external imports 9 from chainlib.eth.constant import ZERO_ADDRESS 10 from chainlib.eth.nonce import RPCNonceOracle 11 from chainlib.eth.tx import receipt 12 from chainlib.eth.block import ( 13 block_latest, 14 block_by_number, 15 ) 16 17 # local imports 18 from erc20_demurrage_token import DemurrageToken 19 20 # test imports 21 from erc20_demurrage_token.unittest import TestDemurrageDefault 22 23 logging.basicConfig(level=logging.INFO) 24 logg = logging.getLogger() 25 26 testdir = os.path.dirname(__file__) 27 28 29 30 class BenchBasic(TestDemurrageDefault): 31 32 def setUp(self): 33 super(BenchBasic, self).setUp() 34 self.bench = { 35 'mint': None, 36 'transfer_light': None, 37 'transfer_heavy': None, 38 'approve': None, 39 'transfer_from': None, 40 'period_light': None, 41 'period_heavy': None, 42 'period_catchup': None, 43 'demurrage': None, 44 } 45 46 47 def test_bench_min(self): 48 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 49 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 50 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) 51 r = self.rpc.do(o) 52 o = receipt(tx_hash) 53 r = self.rpc.do(o) 54 self.assertEqual(r['status'], 1) 55 self.bench['mint'] = r['gas_used'] 56 57 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 58 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 59 (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[2], 512) 60 r = self.rpc.do(o) 61 o = receipt(tx_hash) 62 r = self.rpc.do(o) 63 self.assertEqual(r['status'], 1) 64 self.bench['transfer_light'] = r['gas_used'] 65 66 (tx_hash, o) = c.approve(self.address, self.accounts[1], self.accounts[0], 512) 67 r = self.rpc.do(o) 68 o = receipt(tx_hash) 69 r = self.rpc.do(o) 70 self.assertEqual(r['status'], 1) 71 self.bench['approve'] = r['gas_used'] 72 73 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 74 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 75 (tx_hash, o) = c.transfer_from(self.address, self.accounts[0], self.accounts[1], self.accounts[3], 256) 76 r = self.rpc.do(o) 77 o = receipt(tx_hash) 78 r = self.rpc.do(o) 79 self.assertEqual(r['status'], 1) 80 self.bench['transfer_from'] = r['gas_used'] 81 82 z = 0 83 for i in range(100): 84 self.backend.time_travel(self.start_time + int(self.period_seconds / 2) + (10 * (i * (i + 1)))) 85 (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) 86 r = self.rpc.do(o) 87 o = receipt(tx_hash) 88 r = self.rpc.do(o) 89 self.assertEqual(r['status'], 1) 90 z += r['gas_used'] 91 logg.info('demurrage round {} gas {}'.format(i, r['gas_used'])) 92 z /= 100 93 self.bench['demurrage'] = int(z) 94 95 z = 0 96 for i in range(100): 97 self.backend.time_travel(self.start_time + (self.period_seconds * (i + 1))) 98 (tx_hash, o) = c.change_period(self.address, self.accounts[0]) 99 r = self.rpc.do(o) 100 o = receipt(tx_hash) 101 r = self.rpc.do(o) 102 self.assertEqual(r['status'], 1) 103 z += r['gas_used'] 104 logg.info('period with demurrage round {} gas {}'.format(i, r['gas_used'])) 105 106 z /= 100 107 self.bench['period_heavy'] = int(z) 108 109 z = 0 110 for i in range(100): 111 self.backend.time_travel(self.start_time + (self.period_seconds * ((i + 101)))) 112 (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) 113 r = self.rpc.do(o) 114 o = receipt(tx_hash) 115 r = self.rpc.do(o) 116 self.assertEqual(r['status'], 1) 117 118 (tx_hash, o) = c.change_period(self.address, self.accounts[0]) 119 r = self.rpc.do(o) 120 o = receipt(tx_hash) 121 r = self.rpc.do(o) 122 self.assertEqual(r['status'], 1) 123 z += r['gas_used'] 124 logg.info('period without demurrage round {} gas {}'.format(i, r['gas_used'])) 125 126 z /= 100 127 self.bench['period_light'] = int(z) 128 129 z = 0 130 self.backend.time_travel(self.start_time + (self.period_seconds * 401)) 131 for i in range(100): 132 (tx_hash, o) = c.change_period(self.address, self.accounts[0]) 133 r = self.rpc.do(o) 134 o = receipt(tx_hash) 135 r = self.rpc.do(o) 136 self.assertEqual(r['status'], 1) 137 z += r['gas_used'] 138 logg.info('period catchup round {} gas {}'.format(i, r['gas_used'])) 139 140 z /= 100 141 self.bench['period_catchup'] = int(z) 142 143 self.backend.time_travel(self.start_time + (self.period_seconds * 501)) 144 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], 1024) 145 r = self.rpc.do(o) 146 o = receipt(tx_hash) 147 r = self.rpc.do(o) 148 self.assertEqual(r['status'], 1) 149 self.backend.time_travel(self.start_time + (self.period_seconds * 502)) 150 151 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 152 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 153 (tx_hash, o) = c.transfer(self.address, self.accounts[2], self.accounts[4], 1) 154 r = self.rpc.do(o) 155 o = receipt(tx_hash) 156 r = self.rpc.do(o) 157 self.assertEqual(r['status'], 1) 158 self.bench['transfer_heavy'] = r['gas_used'] 159 160 print(json.dumps(self.bench)) 161 162 163 164 165 166 if __name__ == '__main__': 167 unittest.main()