-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
195 lines (169 loc) · 5.74 KB
/
graph.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import glob
import os
import subprocess
import agenda
def write_rmd(experiment_root, csv_name, num_ccp, downsample=None, interact=False, fields="zt, rout, rin, curr_rate, curr_q, elasticity2", rows=None, cols=None, **kwargs):
experiment_root = os.path.abspath(os.path.expanduser(experiment_root))
experiment_name = os.path.basename(experiment_root)
tomls = glob.glob(os.path.join(experiment_root, '*.toml'))
assert len(tomls) == 1, f"there should be exactly 1 .toml (config) in the experiment directory: {experiment_root} -> {tomls}"
with open(tomls[0], 'r') as f:
config = f.read()
wrap_str = rows
grid = []
if rows:
rows = 'rows=vars({})'.format(rows)
grid.append(rows)
if cols:
cols = 'cols=vars({})'.format(cols)
grid.append(cols)
grid_str = ','.join(grid)
if interact:
interact_str = ""
static_str = "#"
else:
interact_str = "#"
static_str = ""
def format_title(experiment_name):
return experiment_name
mm_plt_fmt = """
**{title}**
```{{r mm{i}, fig.width=15, fig.align='center', echo=FALSE}}
df_m_{i} <- read.csv("{path}", sep=" ") # header=FALSE, col.names=c("t", "total", "delay","bundle", "cross"))
df_m_{i} <- df_m_{i} %>% gather("measurement", "value", total, delay, bundle, cross)
{remove}df_switch_{i} <- read.csv("{switch_path}", sep=",")
plt_m_{i} <- ggplot(df_m_{i}, aes(x=t, y=value, color=measurement)) +
geom_line() +
{remove}geom_rect(data=df_switch_{i}, inherit.aes=FALSE, aes(xmin=xmin,xmax=xmax,ymin=0,ymax=max(df_m_{i}$value),fill="xtcp"), alpha=0.2) +
scale_fill_manual('Mode', values="black", labels=c("xtcp"))
{interact_str}ggplotly(plt_m_{i})
{static_str}plt_m_{i}
```"""
g = glob.glob(experiment_root + '/**/mm-graph.tmp', recursive=True)
mm_plots = []
for (i,path) in enumerate(g):
switch_path = "/".join(path.split("/")[:-1])+"/ccp_switch.parsed"
try:
with open(switch_path) as f:
if sum(1 for _ in f) < 2:
raise Exception("") # goto except
remove = ""
except:
remove = "#"
mm_plots.append(
mm_plt_fmt.format(
i=i,
path=path,
title=format_title(path.split(experiment_name)[1]),
switch_path=switch_path,
remove=remove,
interact_str=interact_str,
static_str=static_str,
)
)
mm_plots_str = "\n".join(mm_plots)
if num_ccp == 0:
nimbus_plots = ""
else:
if len(g) < 3:
nimbus_fig_height = len(g) * 4
elif len(g) < 10:
nimbus_fig_height = len(g) * 2
elif len(g) < 50:
nimbus_fig_height = len(g) * 1
else:
nimbus_fig_height = 30
nimbus_fig_height = max(nimbus_fig_height, 15)
nimbus_plots = """
#### Nimbus
```{{r plot1, fig.width=15, fig.height={fig_height}, fig.align='center', echo=FALSE}}
df <- read.csv("{csv}", sep=",", na.strings=c("","none"))
if (nrow(df) == 0) {{
print("no ccp output")
}} else {{
df <- df %>% gather("measurement", "value", {fields})
plt <- ggplot(df, aes(x=elapsed, y=value, color=measurement)) +
geom_line() +
facet_wrap(~interaction(sch, alg, rate, rtt, bundle, cross, seed), labeller = labeller(.default=label_both, .multi_line=FALSE), nrow={nrow}, ncol=1) +
scale_x_continuous(breaks=seq(0, max(df$elapsed), by=5))
{interact_str}ggplotly(plt)
{static_str}plt
}}
```
""".format(
csv = os.path.join(experiment_root, csv_name),
fields = fields,
wrap_str_check = "1" if wrap_str is not None else "0",
wrap_str = wrap_str,
nrow = len(g),
fig_height = nimbus_fig_height,
interact_str=interact_str,
static_str=static_str,
)
fct_path = os.path.join(experiment_root, 'fcts.data')
if os.path.isfile(fct_path):
fct_plots = """
#### Flow Completion Times
```{{r fcts, fig.width=15, fig.height=6, fig.align='center', echo=FALSE}}
df_fct <- read.csv("{csv}", sep=" ")
df_fct$Duration <- df_fct$Duration.usec. / 1e6
bw <- 12e6 # TODO make this configurable
df_fct$ofct <- (df_fct$Size / bw) + 0.05
df_fct$NormFct <- df_fct$Duration / df_fct$ofct
df_fct$scheme <- paste(df_fct$sch, "_", df_fct$alg, sep="")
fct_plt <- ggplot(df_fct, aes(x=NormFct, colour=scheme)) + stat_ecdf() + scale_x_log10()
fct_plt
```""".format(
csv = fct_path,
)
else:
fct_plots = ""
contents = """
---
title: "{title}"
output: html_document
---
<style type="text/css">
.main-container {{
max-width: 1400px;
margin-left: auto;
margin-right: auto;
}}
</style>
```{{r, echo=FALSE}}
suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(plotly)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(tidyr)))
```
### Overall
{fct_plots}
### Per-Experiment
{nimbus_plots}
#### Mahimahi
{mm_plots}
### Config
```{{r config, eval=FALSE}}
{config}
```
""".format(
title = experiment_name,
config = config,
grid_str = grid_str,
nimbus_plots = nimbus_plots,
fct_plots = fct_plots,
mm_plots = mm_plots_str,
)
rmd = os.path.join(experiment_root, 'exp.Rmd')
html = os.path.join(experiment_root, 'index.html')
with open(rmd, 'w') as f:
f.write(contents)
agenda.task("Rendering Rmd as HTML...")
try:
out = subprocess.check_output("R -e rmarkdown::render\"('{}', output_file='{}')\"".format(
rmd,
html
), shell=True)
except subprocess.CalledProcessError as e:
agenda.failure("Failed to render Rmd as HTML:")
print(e.output.decode())