-
Notifications
You must be signed in to change notification settings - Fork 0
/
size_heatmap.py
158 lines (126 loc) · 4.6 KB
/
size_heatmap.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
154
155
156
157
158
"""
plot size heatmap
usage:
1. run traceAnalyzer: `./traceAnalyzer /path/trace trace_format --common`,
this will generate some output, including size distribution result, trace.size
2. plot size heatmap using this script:
`python3 size_heatmap.py trace.sizeWindow_w300`
"""
import os, sys
import re
import numpy as np
import matplotlib.pyplot as plt
import copy
import numpy.ma as ma
from matplotlib.ticker import FuncFormatter
from typing import List, Dict, Tuple
import logging
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from utils.trace_utils import extract_dataname
from utils.plot_utils import FIG_DIR, FIG_TYPE
logger = logging.getLogger("size_heatmap")
def _load_size_heatmap_data(datapath) -> Tuple[np.ndarray, int, float, int]:
"""load size heatmap plot data from C++ computation
Args:
datapath (str): the path of size heatmap data file
Returns:
Tuple[np.ndarray, int, float, int]: plot_data, time_window, log_base, size_base
"""
ifile = open(datapath)
data_line = ifile.readline()
desc_line = ifile.readline()
m = re.search(
r"# (object_size): \w\w\w_cnt \(time window (?P<tw>\d+), log_base (?P<logb>\d+\.?\d*), size_base (?P<sizeb>\d+)\)",
desc_line,
)
assert m is not None, (
"the input file might not be size heatmap data file, desc line "
+ desc_line
+ " data "
+ datapath
)
time_window = int(m.group("tw"))
log_base = float(m.group("logb"))
size_base = int(m.group("sizeb"))
size_distribution_over_time = []
for line in ifile:
# if "obj_cnt" in line:
# curr_data = size_distribution_by_obj_over_time
# elif len(line.strip()) == 0:
# continue
# else:
count_list = line.strip("\n,").split(",")
size_distribution_over_time.append(count_list)
ifile.close()
dim = max([len(l) for l in size_distribution_over_time])
plot_data = np.zeros((len(size_distribution_over_time), dim))
for idx, l in enumerate(size_distribution_over_time):
l = np.array(l, dtype=np.float64)
l = l / np.sum(l)
plot_data[idx][: len(l)] = l
return plot_data.T, time_window, log_base, size_base
def plot_size_heatmap(datapath: str, figname_prefix: str = ""):
"""
plot size heatmap
Args:
datapath (str): the path of size heatmap data file
figname_prefix (str, optional): the prefix of figname. Defaults to "".
"""
if len(figname_prefix) == 0:
figname_prefix = extract_dataname(datapath)
plot_data, time_window, log_base, size_base = _load_size_heatmap_data(
datapath + "_req"
)
# plot heatmap
cmap = copy.copy(plt.cm.jet)
# cmap = copy.copy(plt.cm.viridis)
cmap.set_bad(color="white", alpha=1.0)
img = plt.imshow(plot_data, origin="lower", cmap=cmap, aspect="auto")
cb = plt.colorbar(img)
plt.gca().xaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(x * time_window / 3600))
)
plt.gca().yaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(log_base**x * size_base))
)
plt.xlabel("Time (hour)")
plt.ylabel("Request size (Byte)")
plt.savefig(
"{}/{}_size_heatmap_req.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.clf()
plot_data, time_window, log_base, size_base = _load_size_heatmap_data(
datapath + "_obj"
)
img = plt.imshow(plot_data, origin="lower", cmap=cmap, aspect="auto")
cb = plt.colorbar(img)
plt.gca().xaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(x * time_window / 3600))
)
plt.gca().yaxis.set_major_formatter(
FuncFormatter(lambda x, pos: "{:.0f}".format(log_base**x * size_base))
)
plt.xlabel("Time (hour)")
plt.ylabel("Object size (Byte)")
plt.savefig(
"{}/{}_size_heatmap_obj.{}".format(FIG_DIR, figname_prefix, FIG_TYPE),
bbox_inches="tight",
)
plt.clf()
logger.info(
"plot saved to {}/{}_size_heatmap_req.{} and {}/{}_size_heatmap_obj.{}".format(
FIG_DIR, figname_prefix, FIG_TYPE, FIG_DIR, figname_prefix, FIG_TYPE
)
)
if __name__ == "__main__":
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("datapath", type=str, help="data path")
ap.add_argument(
"--figname-prefix", type=str, default="", help="the prefix of figname"
)
p = ap.parse_args()
if p.datapath.endswith("_req") or p.datapath.endswith("_obj"):
p.datapath = p.datapath[:-4]
plot_size_heatmap(p.datapath, p.figname_prefix)