Created
August 14, 2020 20:37
-
-
Save shinyfoil/db31e6140cd2618e1bd9356a5fe850ac to your computer and use it in GitHub Desktop.
Polkadot locust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from functools import wraps | |
import random | |
from time import time | |
from locust import Locust, TaskSet, events, task | |
from substrateinterface import SubstrateInterface | |
import json | |
def polkadot_locust_task(f): | |
''' | |
Simple timing wrapper which fires off the necessary | |
success and failure events for locust. | |
''' | |
@wraps(f) | |
def wrapped(*args, **kwargs): | |
start_time = time() | |
try: | |
result = f(*args, **kwargs) | |
print(result) | |
except Exception as e: | |
print('Exception in {}'.format(f.__name__)) | |
total_time = int((time() - start_time) * 1000) | |
events.request_failure.fire( | |
request_type="jsonrpc", | |
name=f.__name__, | |
response_time=total_time, | |
exception=e) | |
return False | |
else: | |
total_time = int((time() - start_time) * 1000) | |
events.request_success.fire( | |
request_type="jsonrpc", | |
name=f.__name__, | |
response_time=total_time, | |
response_length=0) | |
return result | |
return wrapped | |
class PolkadotLocust(Locust): | |
def __init__(self, *args, **kwargs): | |
super(PolkadotLocust, self).__init__(*args, **kwargs) | |
self.client = SubstrateInterface(self.host) | |
self.head = self.get_highest_block_number() | |
def get_highest_block_number(self): | |
head_hash = self.client.get_chain_head() | |
head = self.client.get_block_number(self, head_hash) | |
return head | |
class PolkadotUser(PolkadotLocust): | |
host = 'localhost:8545' | |
min_wait = 100 | |
max_wait = 1000 | |
class task_set(TaskSet): | |
@polkadot_locust_task | |
@task | |
def find_parent_of_random_block(self): | |
target_block = random.randrange(1, self.locust.head) | |
target_hash = self.client.get_block_hash(target_block) | |
target_header = self.client.get_block_header(target_hash) | |
header_json = json.loads(json.dumps(target_header)) | |
return header_json['parentHash'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests>=2.9.1 | |
locustio>=0.7.5 | |
substrate-interface>=0.9.11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment