Web3SpeedTest.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { web3, ethers } from "hardhat";
  2. import axios from 'axios'
  3. import logger from "../../utils/logger";
  4. import contracts from "../../config/contracts";
  5. function test1() {}
  6. function test2() {
  7. new web3.eth.Contract([], contracts.UNIV3)
  8. }
  9. function test21() {
  10. new ethers.Contract(contracts.UNIV3, [])
  11. }
  12. function test3() {
  13. web3.utils.toChecksumAddress('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')
  14. }
  15. function test31() {
  16. ethers.utils.getAddress('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')
  17. }
  18. async function test4() {
  19. await web3.eth.getBlockNumber()
  20. }
  21. async function test5() {
  22. // @ts-ignore
  23. await web3.eth.getBlockNumber2()
  24. }
  25. async function test6() {
  26. await axios.post('http://127.0.0.1:18545', {
  27. 'jsonrpc': '2.0',
  28. 'method': 'eth_blockNumber',
  29. 'params': [],
  30. 'id': new Date().getTime()
  31. }, {
  32. headers: {'Content-type': 'application/json'}
  33. })
  34. }
  35. async function test7() {
  36. // @ts-ignore
  37. await web3.eth.ethCall({
  38. 'from':'0x1712B216FdD1943E5FCd75ec945588dDB4765c0f',
  39. 'to':'0xe0208a3B4C3cd0583F04fB002ebD5dCDd0D3FEB5',
  40. 'data':'0x0ad5a3af0000000000000000000000008b4a890b407c0a2e85a5c915546b8d9aeb731b98000000000000000000000000053b77ac4f8ef8d19830a429fa55e56a7529fac100000000000000000000000000000000000000000000000000000000000006a40000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000cb730e11b12beab590b623a83534803491059ede00000000000000000000000000000000000000000000000000038d7ea4c68000'
  41. }, "latest")
  42. }
  43. async function test8() {
  44. // @ts-ignore
  45. await web3.eth.call({
  46. 'from':'0x1712B216FdD1943E5FCd75ec945588dDB4765c0f',
  47. 'to':'0xe0208a3B4C3cd0583F04fB002ebD5dCDd0D3FEB5',
  48. 'data':'0x0ad5a3af0000000000000000000000008b4a890b407c0a2e85a5c915546b8d9aeb731b98000000000000000000000000053b77ac4f8ef8d19830a429fa55e56a7529fac100000000000000000000000000000000000000000000000000000000000006a40000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000cb730e11b12beab590b623a83534803491059ede00000000000000000000000000000000000000000000000000038d7ea4c68000'
  49. }, "latest")
  50. }
  51. async function run(fun: Function) {
  52. const startTs = new Date().getTime()
  53. let count = 0
  54. // logger.debug(`start test...`)
  55. while (true) {
  56. if (new Date().getTime() < startTs + 1000) {
  57. await fun()
  58. count++
  59. } else {
  60. break
  61. }
  62. }
  63. logger.debug(`${count}, end test.`)
  64. }
  65. async function main() {
  66. const testCount = 10
  67. // logger.debug('web3 getBlockNumber:')
  68. // for (let i = 0; i < testCount; i++) {
  69. // await run(test4)
  70. // }
  71. //
  72. //
  73. // web3.extend({
  74. // property: 'eth',
  75. // methods: [
  76. // {
  77. // name: 'getBlockNumber2',
  78. // call: 'eth_blockNumber'
  79. // }
  80. // ]
  81. // })
  82. // logger.debug('web3 getBlockNumber2.send():')
  83. // for (let i = 0; i < testCount; i++) {
  84. // await run(test5)
  85. // }
  86. //
  87. //
  88. //
  89. // logger.debug('web3 http,http,http,http,http test.')
  90. // for (let i = 0; i < testCount; i++) {
  91. // await run(test6)
  92. // }
  93. //
  94. //
  95. // web3.extend({
  96. // property: 'eth',
  97. // methods: [
  98. // {
  99. // name: 'ethCall',
  100. // call: 'eth_call',
  101. // params: 2
  102. // }
  103. // ]
  104. // })
  105. // logger.debug('web3 ethCall,ethCall,ethCall,ethCall,ethCall test.')
  106. // for (let i = 0; i < testCount; i++) {
  107. // await run(test7)
  108. // }
  109. //
  110. //
  111. // logger.debug('web3 call,call,call,call,call test.')
  112. // for (let i = 0; i < testCount; i++) {
  113. // await run(test8)
  114. // }
  115. // web3.extend({
  116. // property: 'eth',
  117. // methods: [
  118. // {
  119. // name: 'batchCall',
  120. // call: 'eth_batchCall',
  121. // params: 1,
  122. // }
  123. // ]
  124. // })
  125. //
  126. // // @ts-ignore
  127. // const rst = await web3.eth.batchCall({
  128. // Block: "latest",
  129. // Calls:[{
  130. // 'from':'0x1712B216FdD1943E5FCd75ec945588dDB4765c0f',
  131. // 'to':'0xe0208a3B4C3cd0583F04fB002ebD5dCDd0D3FEB5',
  132. // 'data':'0x0ad5a3af0000000000000000000000008b4a890b407c0a2e85a5c915546b8d9aeb731b98000000000000000000000000053b77ac4f8ef8d19830a429fa55e56a7529fac100000000000000000000000000000000000000000000000000000000000006a40000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000cb730e11b12beab590b623a83534803491059ede00000000000000000000000000000000000000000000000000038d7ea4c68000'
  133. // }]
  134. // })
  135. // logger.debug(rst)
  136. }
  137. main().catch((error) => {
  138. console.error(error);
  139. process.exitCode = 1;
  140. })