|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +from collections import ( |
| 4 | + defaultdict, |
| 5 | +) |
| 6 | +import logging |
| 7 | +import sys |
| 8 | +import timeit |
| 9 | +from typing import ( |
| 10 | + Any, |
| 11 | + Callable, |
| 12 | + Dict, |
| 13 | + Union, |
| 14 | +) |
| 15 | + |
| 16 | +from web3 import ( |
| 17 | + AsyncHTTPProvider, |
| 18 | + HTTPProvider, |
| 19 | + Web3, |
| 20 | +) |
| 21 | +from web3.eth import ( |
| 22 | + AsyncEth, |
| 23 | +) |
| 24 | +from web3.tools.benchmark.node import ( |
| 25 | + GethBenchmarkFixture, |
| 26 | +) |
| 27 | +from web3.tools.benchmark.reporting import ( |
| 28 | + print_entry, |
| 29 | + print_footer, |
| 30 | + print_header, |
| 31 | +) |
| 32 | +from web3.tools.benchmark.utils import ( |
| 33 | + wait_for_aiohttp, |
| 34 | + wait_for_http, |
| 35 | +) |
| 36 | + |
| 37 | +parser = argparse.ArgumentParser() |
| 38 | +parser.add_argument( |
| 39 | + "--num-calls", type=int, default=10, help="The number of RPC calls to make", |
| 40 | +) |
| 41 | + |
| 42 | +# TODO - layers to test: |
| 43 | +# contract.functions.method(...).call() |
| 44 | +# w3.eth.call(...) |
| 45 | +# HTTPProvider.make_request(...) |
| 46 | + |
| 47 | + |
| 48 | +def build_web3_http(endpoint_uri: str) -> Web3: |
| 49 | + wait_for_http(endpoint_uri) |
| 50 | + _web3 = Web3(HTTPProvider(endpoint_uri), middlewares=[]) |
| 51 | + return _web3 |
| 52 | + |
| 53 | + |
| 54 | +async def build_async_w3_http(endpoint_uri: str) -> Web3: |
| 55 | + await wait_for_aiohttp(endpoint_uri) |
| 56 | + _web3 = Web3( |
| 57 | + AsyncHTTPProvider(endpoint_uri), # type: ignore |
| 58 | + middlewares=[], |
| 59 | + modules={"async_eth": (AsyncEth,)}, |
| 60 | + ) |
| 61 | + return _web3 |
| 62 | + |
| 63 | + |
| 64 | +def sync_benchmark(func: Callable[..., Any], n: int) -> Union[float, str]: |
| 65 | + try: |
| 66 | + starttime = timeit.default_timer() |
| 67 | + for _ in range(n): |
| 68 | + func() |
| 69 | + endtime = timeit.default_timer() |
| 70 | + execution_time = endtime - starttime |
| 71 | + return execution_time |
| 72 | + except Exception: |
| 73 | + return "N/A" |
| 74 | + |
| 75 | + |
| 76 | +async def async_benchmark(func: Callable[..., Any], n: int) -> Union[float, str]: |
| 77 | + try: |
| 78 | + starttime = timeit.default_timer() |
| 79 | + for result in asyncio.as_completed([func() for _ in range(n)]): |
| 80 | + await result |
| 81 | + execution_time = timeit.default_timer() - starttime |
| 82 | + return execution_time |
| 83 | + except Exception: |
| 84 | + return "N/A" |
| 85 | + |
| 86 | + |
| 87 | +def main(logger: logging.Logger, num_calls: int) -> None: |
| 88 | + fixture = GethBenchmarkFixture() |
| 89 | + for built_fixture in fixture.build(): |
| 90 | + for process in built_fixture: |
| 91 | + w3_http = build_web3_http(fixture.endpoint_uri) |
| 92 | + loop = asyncio.get_event_loop() |
| 93 | + async_w3_http = loop.run_until_complete(build_async_w3_http(fixture.endpoint_uri)) |
| 94 | + |
| 95 | + methods = [ |
| 96 | + { |
| 97 | + "name": "eth_gasPrice", |
| 98 | + "params": {}, |
| 99 | + "exec": lambda: w3_http.eth.gas_price, |
| 100 | + "async_exec": lambda: async_w3_http.async_eth.gas_price, |
| 101 | + }, |
| 102 | + { |
| 103 | + "name": "eth_blockNumber", |
| 104 | + "params": {}, |
| 105 | + "exec": lambda: w3_http.eth.block_number, |
| 106 | + "async_exec": lambda: (_ for _ in ()).throw(Exception("not implemented yet")), |
| 107 | + }, |
| 108 | + { |
| 109 | + "name": "eth_getBlock", |
| 110 | + "params": {}, |
| 111 | + "exec": lambda: w3_http.eth.get_block("1"), |
| 112 | + "async_exec": lambda: (_ for _ in ()).throw(Exception("not implemented yet")), |
| 113 | + }, |
| 114 | + ] |
| 115 | + |
| 116 | + def benchmark(method: Dict[str, Any]) -> None: |
| 117 | + outcomes: Dict[str, Union[str, float]] = defaultdict(lambda: "N/A") |
| 118 | + outcomes["name"] = method["name"] |
| 119 | + outcomes["HTTPProvider"] = sync_benchmark(method["exec"], num_calls,) |
| 120 | + outcomes["AsyncHTTPProvider"] = loop.run_until_complete( |
| 121 | + async_benchmark(method["async_exec"], num_calls) |
| 122 | + ) |
| 123 | + print_entry(logger, outcomes) |
| 124 | + |
| 125 | + print_header(logger, num_calls) |
| 126 | + |
| 127 | + for method in methods: |
| 128 | + benchmark(method) |
| 129 | + |
| 130 | + print_footer(logger) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + logger = logging.getLogger() |
| 137 | + logger.setLevel(logging.INFO) |
| 138 | + logger.addHandler(logging.StreamHandler(sys.stdout)) |
| 139 | + |
| 140 | + main(logger, args.num_calls) |
0 commit comments