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
/
workload_graphs.py
153 lines (124 loc) · 5.01 KB
/
workload_graphs.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
146
147
148
149
150
151
152
153
import sys
import pandas as pd
from matplotlib import gridspec
import numpy as np
import logparser
from matplotlib import dates
from plot import plt, sns
FONT_SIZE=25
plt.rcParams.update({
"font.size": FONT_SIZE,
"axes.labelsize" : FONT_SIZE,
"font.size" : FONT_SIZE,
"text.fontsize" : FONT_SIZE,
"legend.fontsize": FONT_SIZE,
"xtick.labelsize" : FONT_SIZE * 0.8,
"ytick.labelsize" : FONT_SIZE * 0.8,
})
def add_text(axe, text):
axe.axis('off')
axe.set(xlabel="")
axe.text(0.5, 0.3, text,
horizontalalignment='center',
verticalalignment='center',
fontsize='small',
transform = axe.transAxes)
def describe_series(series, filename):
fig, axis = plt.subplots(3)
ax = sns.boxplot(series, ax=axis[0])
ax.set(ylabel='Number of users', xlabel='')
ax = sns.distplot(series, ax=axis[1], kde=False, norm_hist=False)
ax.set(ylabel='Number of users', xlabel='')
add_text(axis[2], series.describe())
fig.savefig(filename)
def retention_time(df, filename):
g = df.groupby([pd.Grouper(key='timestamp', freq='1d'), df.client_id])
retention_time = g.nth(-1).timestamp - g.nth(0).timestamp
seconds = retention_time / np.timedelta64(1, 'm')
seconds = seconds.mean(level=1)
seconds.name = "Daily retention time [min]"
describe_series(seconds, filename)
def user_requests(df, filename):
actions = df.groupby([pd.Grouper(key="timestamp", freq="1d"), df.client_id]).size()
actions = actions.mean(level=1)
actions.name = "Daily number of requests per User"
describe_series(actions, filename)
def count_request(df):
return df.pivot_table("client_id", index="timestamp", aggfunc='count')
def top5_vs_rest_requests(df, filename):
requests = df.groupby([pd.Grouper(key="timestamp", freq="1d"), df.client_id]).size()
requests = requests.mean(level=1)
top5 = requests.nlargest(5).index
fig, axis = plt.subplots(2, 2, figsize=(15, 5), gridspec_kw={'width_ratios':[3, 1]})
top5_requests = count_request(df[df.client_id.isin(top5)]).resample("1h").sum()
top5_requests.name = "Requests per minute of top 5 users"
plot = top5_requests.plot(ax=axis[0, 0])
add_text(axis[0, 1], top5_requests.describe())
rest_requests = count_request(df[~df.client_id.isin(top5)]).resample("1h").sum()
rest_requests.name = "Requests per minute of rest"
plot2 = rest_requests.plot(ax=axis[1, 0])
add_text(axis[1, 1], rest_requests.describe())
plot.set_xlim(plot2.get_xlim())
fig.savefig(filename)
def request_rate(df, title, filename):
fig, axis = plt.subplots(1, 2, figsize=(15, 5), gridspec_kw={'width_ratios':[3, 1]})
fig.suptitle(title, fontsize=14)
agg = count_request(df)
minutely = agg.resample("60s").sum()
minutely.name = "Requests per minute"
minutely.plot(ax=axis[0, 0])
add_text(axis[0, 1], minutely.describe())
hourly = agg.resample("1h").sum()
hourly.plot(ax=axis[1, 0])
hourly.name = "Requests per hour"
add_text(axis[1, 1], hourly.describe())
fig.savefig(filename)
def request_rate2(df, filename):
df = df[(df.timestamp > pd.Timestamp("1998-06-02 08:50:00")) & (df.timestamp < pd.Timestamp("1998-06-02 09:50:00"))]
plt.clf()
agg = count_request(df)
minutely = agg.resample("60s").sum()
minutely.name = "Requests per minute"
xticks = pd.date_range(start=df.iloc[0].timestamp, end=df.iloc[-1].timestamp, freq="15min")
fig = minutely.plot(xticks=xticks.to_pydatetime(), color="k")
fig.set_xticklabels([x.strftime('%H:%M') for x in xticks]);
#fig.set_title("Request rate for Worldcup 98 (Paris)", fontsize=14)
plt.minorticks_off()
fig.set_ylabel("Requests per minute [1/min]")
plt.gcf().tight_layout()
plt.savefig(filename)
def main(type, args):
logs = []
if type == "worldcup":
read_log = logparser.read_worldcup
elif type == "nasa":
read_log = logparser.read_nasa
else:
sys.stderr.write("type must be nasa or worldcup")
for arg in args:
df = read_log(arg)
filter_ = df["type"].isin(["HTML", "DYNAMIC", "DIRECTORY"])
if type == "worldcup":
filter_ = filter_ & (df.region == "Paris")
df = df[filter_]
df = df.sort_values("timestamp")
logs.append(df)
df = pd.concat(logs)
title = {
"worldcup": "10 days, request rate for WorldCup98, Paris, Server 4",
"nasa": "NASA http log, Jul95"
}
#if type == "worldcup":
# top5_vs_rest_requests(df, "top5_vs_rest_requests.png")
#request_rate(df, title[type], "requests-per-time-%s.png" % type)
request_rate2(df, "requests-per-time-%s-1-hour.pdf" % type)
#retention_time(df, "rentention-time-%s.png" % type)
#user_requests(df, "user-requests-%s.png" % type)
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.stderr.write("Draw graphs for nasa and worldcup web logs\n")
sys.stderr.write("USAGE: %s nasa|worldcup logfile\n")
sys.exit(1)
type = sys.argv[1]
args = sys.argv[2:]
main(type, args)