forked from ipchama/dhammer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoTuner.py
81 lines (55 loc) · 2.96 KB
/
AutoTuner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python3
import requests
import json
import logging
import os
import time
import math
class AutoTuner:
"""A class for using the dhammer API to find the optimal rps rate."""
def __init__(self, options):
self._options = options
self._run = True
self._rps = 1
self._previous_ramped_up_rps = 1
def _tune(self):
logging.debug("Going to tune...")
response = requests.get(f"http://{self._options.api_address}:{self._options.api_port}/stats")
response.raise_for_status()
stats = json.loads(response.text)
target_stat = None
compare_stat = None
for stat in stats:
if stat['stat_name'] == self._options.tune_stat_name:
target_stat = stat
elif stat['stat_name'] == self._options.tune_stat_compare_name:
compare_stat = stat
diff_perc = target_stat['stat_rate_per_second'] / compare_stat['stat_rate_per_second']
info = {}
if diff_perc >= self._options.tune_compare_min_percentage:
self._previous_ramped_up_rps = self._rps
self._rps = self._rps * self._options.ramp_up_factor
response = requests.put(f"http://{self._options.api_address}:{self._options.api_port}/update", data=f'{{"rps": {math.floor(self._rps)}}}')
response.raise_for_status()
print(f"{target_stat['stat_name']} / {compare_stat['stat_name']} = {diff_perc}: ramped up. Target RPS is {self._rps}.")
else:
self._options.ramp_up_factor = self._options.ramp_up_factor * self._options.ramp_down_factor
if self._options.ramp_up_factor <= 1:
print(f"{target_stat['stat_name']} / {compare_stat['stat_name']} = {diff_perc}: Ramp down triggered, but next ramp-up difference too low.")
print(f"Target reached. Optimal RPS is approximately {math.floor(self._previous_ramped_up_rps)}.")
return(False)
self._rps = self._previous_ramped_up_rps * self._options.ramp_up_factor
response = requests.put(f"http://{self._options.api_address}:{self._options.api_port}/update", data=f'{{"rps": {math.floor(self._rps)}}}')
response.raise_for_status()
print(f"{target_stat['stat_name']} / {compare_stat['stat_name']} = {diff_perc}: ramped down. Target RPS is {self._rps}.")
return(True)
def prepare(self):
response = requests.put(f"http://{self._options.api_address}:{self._options.api_port}/update", data='{"rps": 1}')
response.raise_for_status()
time.sleep(self._options.refresh_rate_seconds)
def stop(self): # GIL for now ;D
self._run = False
return(True)
def start(self):
while self._run and self._tune(): # GIL for now ;D
time.sleep(self._options.refresh_rate_seconds)