This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench-metrics-speedup.py
145 lines (127 loc) · 5.04 KB
/
bench-metrics-speedup.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from influxdb import InfluxDBClient
from pandas.tseries.offsets import DateOffset
import pandas as pd
import subprocess
import time
import argparse
import metadata
import os
import json
from plot import plt, sns
from docker import Client
from collections import defaultdict
def parse_args():
parser = argparse.ArgumentParser(prog='bench-metrics-speedup', usage='%(prog)s [options]')
parser.add_argument('measurements', nargs="+", help="measurement directories")
return parser.parse_args()
class Container():
def __init__(self, **kwargs):
self.api = Client()
self.container_args = kwargs
def stats(self):
s = next(self.api.stats(self.id))
return json.loads(s.decode("utf-8"))
def inspect(self):
return self.api.inspect_container(self.id)
def inspect_volume(self, id):
return self.api.inspect_volume(id)
def __enter__(self):
self.id = self.api.create_container(**self.container_args).get("Id")
self.api.start(container=self.id)
data = self.api.inspect_container(self.id)
self.ip = data["NetworkSettings"]["IPAddress"]
return self
def __exit__(self, type, value, tb):
self.api.stop(container=self.id)
self.api.remove_container(container=self.id)
def insert_influxdb(client, path, reduced_set=False):
m = metadata.load(path)
for srv in m["services"]:
if srv["name"] in ["loadgenerator"]:
continue
if srv["name"] != "web":
continue
print(srv["name"])
if reduced_set:
for k, cluster in srv["clusters"].items():
granger_fields = cluster.get("grangercausality-metrics")
if granger_fields is not None:
break
assert granger_fields is not None, "No grangercausality-metrics found"
fields = []
for f in granger_fields:
if f is None:
continue
fields.append(f.replace("-diff", ""))
else:
fields = srv["fields"]
df = pd.read_csv(os.path.join(path, srv["filename"]), sep="\t", parse_dates=True, index_col='time')
points = []
filtered_fields = fields + srv["tags"]
df = df[filtered_fields]
start = df.iloc[0].name
end = start + DateOffset(minutes=10)
df = df[(df.index >= start) & (df.index <= end)]
for idx, series in df.iterrows():
f = dict(series[fields][series.notnull()])
if len(f) == 0:
continue
point = {
"time": idx,
"measurement": srv["name"],
"fields": dict(f),
"tags": dict(series[srv["tags"]][series.notnull()]),
}
points.append(point)
if len(points) > 1000:
client.write_points(points)
points.clear()
if len(points) > 0:
client.write_points(points)
def get_io(obj):
read = 0
write = 0
for item in obj["blkio_stats"]["io_service_time_recursive"]:
if item["op"] == "Read":
read += item["value"]
if item["op"] == "Write":
write += item["value"]
return read, write
def du(path):
out = subprocess.check_output(["sudo", "du", "-s", path])
return int(out.decode("utf-8").split("\t")[0])
def packet_size(data, key):
return int(data["networks"]["eth0"][key])
def benchmark_import(stats, path, reduced_set=False):
with Container(image='influxdb:alpine', ports=[8083, 8086]) as container:
time.sleep(2)
client = InfluxDBClient(container.ip, 8086, database="metrics")
client.create_database("metrics")
time.sleep(2)
before = container.stats()
mountpoint = os.path.join(container.inspect()["Mounts"][0]["Source"])
before_size = du(mountpoint)
result = client.query('show stats;')
insert_influxdb(client, path, reduced_set)
after = container.stats()
container.api.stop(container=container.id)
after_size = du(mountpoint)
stats["cpu_usage"].append(after["cpu_stats"]["cpu_usage"]["total_usage"] - before["cpu_stats"]["cpu_usage"]["total_usage"])
stats["blkio_read"].append(get_io(after)[0] - get_io(before)[0])
stats["blkio_write"].append(get_io(after)[1] - get_io(before)[1])
stats["netio_read"].append(packet_size(after, "rx_bytes") - packet_size(before, "rx_bytes"))
stats["netio_write"].append(packet_size(after, "tx_bytes") - packet_size(before, "tx_bytes"))
stats["db_size"].append(after_size - before_size)
def main():
args = parse_args()
stats1 = defaultdict(list)
stats2 = defaultdict(list)
for m in args.measurements:
stats1["measurement"].append(m)
benchmark_import(stats1, m)
stats2["measurement"].append(m)
benchmark_import(stats2, m, reduced_set=True)
df = pd.DataFrame(stats1).merge(pd.DataFrame(stats2), on='measurement', suffixes=["_native", "_reduced"])
df.to_csv("result2.tsv", sep="\t")
if __name__ == '__main__':
main()