Skip to content

Commit

Permalink
Fix bugs and improve trafficgen and taskset
Browse files Browse the repository at this point in the history
  • Loading branch information
harrywong committed Jul 9, 2018
1 parent 65429ce commit 5f81518
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
16 changes: 13 additions & 3 deletions loadtest/taskset/evt_taskset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@
from trafficgen.utils import Reader


regions = set()


def generate_traffic(hosturl):
nonce = ''.join(random.choice(string.ascii_letters[26:]) for _ in range(5))
while True:
nonce = ''.join(random.choice(
string.ascii_letters[26:]) for _ in range(2))
if nonce in regions:
continue
regions.add(nonce)
break

path = '/tmp/evt_loadtest'
if not os.path.exists(path):
os.mkdir(path)

traffic = '{}/{}.lz4'.format(path, nonce)
gen = TrafficGenerator(nonce, hosturl, 'actions.config', traffic)
gen.initialize()

print('{} Generating traffic'.format(nonce))
with tqdm.tqdm(total=100) as pbar:
with tqdm.tqdm(total=gen.total) as pbar:
gen.generate(True, lambda x: pbar.update(x))

return Reader(traffic), nonce
Expand Down Expand Up @@ -57,4 +67,4 @@ def push_trx(self):
class WebsiteUser(FastHttpLocust):
task_set = EVTTaskSet
min_wait = 0
max_wait = 1000
max_wait = 0
2 changes: 1 addition & 1 deletion loadtest/trafficgen/trafficgen/actions.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"total":10,
"total":10000,
"max_user_number":10000,
"actions":{
"newdomain":1,
Expand Down
31 changes: 23 additions & 8 deletions loadtest/trafficgen/trafficgen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ def __init__(self, message):

class TrafficGenerator:
def __init__(self, name, url, config='actions.config', output='traffic_data.lz4'):
assert len(name) <= 2, "Length of region name should be less than 2"

self.conf = self.load_conf(config)
self.name = name

self.rp = randompool.RandomPool(
tg_name=self.name, max_user_num=self.conf['max_user_number'])
self.rp = None
self.writer = utils.Writer(output)

self.trxgen = TrxGenerator(url)
self.actgen = ActionGenerator()

self.limits, self.currs, self.total = self.init_actions()
self.limits = None
self.currs = None
self.total = 0

@staticmethod
def load_conf(config):
with open(config, 'r') as f:
conf = json.load(f, object_pairs_hook=OrderedDict)
return conf

def initialize(self):
self.rp = randompool.RandomPool(
tg_name=self.name, max_user_num=self.conf['max_user_number'])
self.init_actions()

def init_actions(self):
limits = {}
currs = {}
Expand All @@ -43,11 +51,13 @@ def init_actions(self):
for action, ratio in actions.items():
if ratio == 0:
continue
limits[action] = ratio / ratio_sum * total
limits[action] = round(ratio / ratio_sum * total)
currs[action] = 0

total = sum(limits.values())
return limits, currs, total
self.limits = limits
self.currs = currs
self.total = total

def generate(self, shuffle=True, process_cb=None):
actions = list(self.limits.keys())
Expand Down Expand Up @@ -79,11 +89,16 @@ def generate(self, shuffle=True, process_cb=None):

i = i + 1
if process_cb is not None:
process_cb(i / self.total)
process_cb(1)

self.writer.close()


if __name__ == '__main__':
TG = TrafficGenerator(name='TESTA', url="http://127.0.0.1:8888")
TG.generate()
import tqdm

gen = TrafficGenerator(name='TE', url="http://127.0.0.1:8888")
gen.initialize()

with tqdm.tqdm(total=gen.total) as pbar:
gen.generate(True, lambda x: pbar.update(x))
17 changes: 11 additions & 6 deletions loadtest/trafficgen/trafficgen/randompool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def fake_name(prefix):

def fake_symbol(prefix, syms):
while True:
sym = prefix.upper() + ''.join(random.choice(string.ascii_letters[26:]) for _ in range(2))
sym = prefix.upper() + ''.join(random.choice(string.ascii_letters[26:]) for _ in range(5))
if sym in syms:
continue
syms.add(sym)
Expand All @@ -42,6 +42,10 @@ def __init__(self, name, users):
self.users = users
self.create_time = time.time()

def pub_key(self):
assert len(self.users) == 1
return str(self.users[0].pub_key)

def pub_keys(self):
return [str(user.pub_key) for user in self.users]

Expand All @@ -53,6 +57,7 @@ class Domain(Item):
def __init__(self, name, user):
super().__init__(name, [user])


class Group(Item):
def __init__(self, name, user):
super().__init__(name, [user])
Expand Down Expand Up @@ -117,7 +122,7 @@ def get_user(self):
def newdomain(self):
domain = Domain(fake_name(self.tg_name), self.get_user())
self.add_item('domain', domain)
return {'name': domain.name, 'creator': domain.pub_keys()}, domain.priv_keys()
return {'name': domain.name, 'creator': domain.pub_key()}, domain.priv_keys()

def updatedomain(self):
pass
Expand All @@ -133,7 +138,7 @@ def newfungible(self):
asset = base.new_asset(sym)
fungible = Fungible(sym, self.get_user(), total_supply=100000)
self.add_item('fungible', fungible)
return {'sym': sym, 'creator': fungible.pub_keys(), 'total_supply': asset(100000)}, fungible.priv_keys()
return {'sym': sym, 'creator': fungible.pub_key(), 'total_supply': asset(100000)}, fungible.priv_keys()

def updfungible(self):
pass
Expand All @@ -158,7 +163,7 @@ def transferft(self):
user1 = random.choice(fungible.accounts)
user2 = self.get_user() # not add user2 into accounts for convinient
return {
'from': str(user1.pub_key),
'_from': str(user1.pub_key),
'to': str(user2.pub_key),
'number': asset(0.0001),
'memo': fake_name('memo')
Expand All @@ -172,7 +177,7 @@ def issuetoken(self):
return {
'domain': domain.name,
'names': [token.name],
'owner': [token.pub_keys()]
'owner': token.pub_keys()
}, domain.priv_keys()

def transfer(self):
Expand Down Expand Up @@ -202,7 +207,7 @@ def addmeta(self):
break

item = self.get_item(item_type)
ar = base.AuthorizerRef('A', item.pub_keys())
ar = base.AuthorizerRef('A', item.pub_key())

get_domain = {
'domain': lambda i: i.name,
Expand Down

0 comments on commit 5f81518

Please sign in to comment.