-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
427 lines (354 loc) · 14.3 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import argparse
import glob
import json
import os
import shutil
import warnings
from datetime import datetime, timedelta
from multiprocessing import Pool
from os.path import abspath, basename, dirname, exists, join
# turn off all warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
from tqdm import tqdm
from RCAEval.benchmark.evaluation import Evaluator
from RCAEval.classes.graph import Node
from RCAEval.io.time_series import drop_constant, drop_time, preprocess
from RCAEval.utility import (
dump_json,
is_py38,
is_py310,
load_json,
download_online_boutique_dataset,
download_sock_shop_1_dataset,
download_sock_shop_2_dataset,
download_train_ticket_dataset,
download_re1_dataset,
download_re2_dataset,
download_re3_dataset,
)
if is_py310():
from RCAEval.e2e import (
baro,
causalrca,
circa,
cloudranger,
cmlp_pagerank,
dummy,
e_diagnosis,
easyrca,
fci_pagerank,
fci_randomwalk,
ges_pagerank,
granger_pagerank,
granger_randomwalk,
lingam_pagerank,
lingam_randomwalk,
micro_diag,
microcause,
microrank,
mscred,
nsigma,
ntlr_pagerank,
ntlr_randomwalk,
pc_pagerank,
pc_randomwalk,
run,
tracerca,
)
elif is_py38():
from RCAEval.e2e import dummy, e_diagnosis, ht, rcd, mmrcd
else:
print("Please use Python 3.8 or 3.10")
exit(1)
try:
import torch
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from RCAEval.e2e.causalrca import causalrca
except ImportError:
pass
def parse_args():
parser = argparse.ArgumentParser(description="RCAEval evaluation")
parser.add_argument("--method", type=str, help="Choose a method.")
parser.add_argument("--dataset", type=str, help="Choose a dataset.", choices=[
"online-boutique", "sock-shop-1", "sock-shop-2", "train-ticket",
"re1-ob", "re1-ss", "re1-tt", "re2-ob", "re2-ss", "re2-tt", "re3-ob", "re3-ss", "re3-tt"
])
parser.add_argument("--length", type=int, default=20, help="Time series length (RQ4)")
parser.add_argument("--tdelta", type=int, default=0, help="Specify $t_delta$ to simulate delay in anomaly detection")
parser.add_argument("--test", action="store_true", help="Perform smoke test on certain methods without fully run on all data")
args = parser.parse_args()
if args.method not in globals():
raise ValueError(f"{args.method=} not defined. Please check imported methods.")
return args
args = parse_args()
# download dataset
if "online-boutique" in args.dataset or "re1-ob" in args.dataset:
download_online_boutique_dataset()
elif "sock-shop-1" in args.dataset:
download_sock_shop_1_dataset()
elif "sock-shop-2" in args.dataset or "re1-ss" in args.dataset:
download_sock_shop_2_dataset()
elif "train-ticket" in args.dataset or "re1-tt" in args.dataset:
download_train_ticket_dataset()
elif "re2" in args.dataset:
download_re2_dataset()
elif "re3" in args.dataset:
download_re3_dataset()
else:
raise Exception(f"{args.dataset} is not defined!")
DATASET_MAP = {
"online-boutique": "data/online-boutique",
"sock-shop-1": "data/sock-shop-1",
"sock-shop-2": "data/sock-shop-2",
"train-ticket": "data/train-ticket",
"re1-ob": "data/online-boutique",
"re1-ss": "data/sock-shop-2",
"re1_tt": "data/train-ticket",
"re2-ob": "data/RE2/RE2-OB",
"re2-ss": "data/RE2/RE2-SS",
"re2-tt": "data/RE2/RE2-TT",
"re3-ob": "data/RE3/RE3-OB",
"re3-ss": "data/RE3/RE3-SS",
"re3-tt": "data/RE3/RE3-TT"
}
dataset = DATASET_MAP[args.dataset]
# prepare input paths
data_paths = list(glob.glob(os.path.join(dataset, "**/data.csv"), recursive=True))
if not data_paths:
data_paths = list(glob.glob(os.path.join(dataset, "**/simple_metrics.csv"), recursive=True))
# new_data_paths = []
# for p in data_paths:
# if os.path.exists(p.replace("data.csv", "simple_data.csv")):
# new_data_paths.append(p.replace("data.csv", "simple_data.csv"))
# elif os.path.exists(p.replace("data.csv", "simple_metrics.csv")):
# new_data_paths.append(p.replace("data.csv", "simple_metrics.csv"))
# else:
# new_data_paths.append(p)
# data_paths = new_data_paths
if args.test is True:
data_paths = data_paths[:2]
# prepare output paths
from tempfile import TemporaryDirectory
# output_path = TemporaryDirectory().name
output_path = "output"
report_path = join(output_path, f"report.xlsx")
result_path = join(output_path, "results")
os.makedirs(result_path, exist_ok=True)
def process(data_path):
run_args = argparse.Namespace()
run_args.root_path = os.getcwd()
run_args.data_path = data_path
# convert length from minutes to seconds
if args.length is None:
args.length = 10
data_length = args.length * 60 // 2
data_dir = dirname(data_path)
service, metric = basename(dirname(dirname(data_path))).split("_")
case = basename(dirname(data_path))
rp = join(result_path, f"{service}_{metric}_{case}.json")
# == Load and Preprocess data ==
data = pd.read_csv(data_path)
# remove lat-50, only selecte lat-90
data = data.loc[:, ~data.columns.str.endswith("_latency-50")]
if "mm-tt" in data_path:
time_col = data["time"]
data = data.loc[:, data.columns.str.startswith("ts-")]
data["time"] = time_col
# handle inf
data = data.replace([np.inf, -np.inf], np.nan)
# handle na
data = data.fillna(method="ffill")
data = data.fillna(0)
with open(join(data_dir, "inject_time.txt")) as f:
inject_time = int(f.readlines()[0].strip()) + args.tdelta
# for metrics, minutes -> seconds // 2
normal_df = data[data["time"] < inject_time].tail(args.length * 60 // 2)
anomal_df = data[data["time"] >= inject_time].head(args.length * 60 // 2)
data = pd.concat([normal_df, anomal_df], ignore_index=True)
# num column, exclude time
num_node = len(data.columns) - 1
# rename latency
data = data.rename(
columns={
c: c.replace("_latency-90", "_latency")
for c in data.columns
if c.endswith("_latency-90")
}
)
# == Get SLI ===
sli = None
if "my-sock-shop" in data_path or "fse-ss" in data_path:
sli = "front-end_cpu"
if f"{service}_latency" in data:
sli = f"{service}_latency"
elif "sock-shop" in data_path:
sli = "front-end_cpu"
if f"{service}_lat_90" in data:
sli = f"{service}_lat_90"
elif "train-ticket" in data_path or "fse-tt" in data_path or "RE2-TT" in data_path:
sli = "ts-ui-dashboard_latency"
if f"{service}_latency" in data:
sli = f"{service}_latency"
elif "online-boutique" in data_path or "fse-ob" in data_path or "RE2-OB" in data_path or "RE2-SS" in data_path:
sli = "frontend_latency"
if f"{service}_latency" in data:
sli = f"{service}_latency"
elif "frontend_1" in data:
sli = "frontend_1"
else:
raise ValueError("SLI not implemented")
# == PROCESS ==
func = globals()[args.method]
try:
st = datetime.now()
out = func(
data,
inject_time,
dataset=args.dataset,
anomalies=None,
dk_select_useful=False,
sli=sli,
verbose=False,
n_iter=num_node,
args=run_args,
)
root_causes = out.get("ranks")
# print("==============")
# print(f"{data_path=}")
# print(root_causes[:5])
dump_json(filename=rp, data={0: root_causes})
except Exception as e:
raise e
print(f"{args.method=} failed on {data_path=}")
print(e)
rp = join(result_path, f"{service}_{metric}_{case}_failed.json")
with open(rp, "w") as f:
json.dump({"error": str(e)}, f)
start_time = datetime.now()
for data_path in tqdm(sorted(data_paths)):
process(data_path)
end_time = datetime.now()
time_taken = end_time - start_time
avg_speed = round(time_taken.total_seconds() / len(data_paths), 2)
# ======== EVALUTION ===========
rps = glob.glob(join(result_path, "*.json"))
services = sorted(list(set([basename(x).split("_")[0] for x in rps])))
faults = sorted(list(set([basename(x).split("_")[1] for x in rps])))
eval_data = {
"service-fault": [],
"top_1_service": [],
"top_3_service": [],
"top_5_service": [],
"avg@5_service": [],
"top_1_metric": [],
"top_3_metric": [],
"top_5_metric": [],
"avg@5_metric": [],
}
s_evaluator_all = Evaluator()
f_evaluator_all = Evaluator()
s_evaluator_cpu = Evaluator()
f_evaluator_cpu = Evaluator()
s_evaluator_mem = Evaluator()
f_evaluator_mem = Evaluator()
s_evaluator_lat = Evaluator()
f_evaluator_lat = Evaluator()
s_evaluator_loss = Evaluator()
f_evaluator_loss = Evaluator()
s_evaluator_io = Evaluator()
f_evaluator_io = Evaluator()
s_evaluator_socket = Evaluator()
f_evaluator_socket = Evaluator()
for service in services:
for fault in faults:
s_evaluator = Evaluator()
f_evaluator = Evaluator()
for rp in rps:
s, m = basename(rp).split("_")[:2]
if s != service or m != fault:
continue # ignore
data = load_json(rp)
if "error" in data:
continue # ignore
for i, ranks in data.items():
s_ranks = [Node(x.split("_")[0].replace("-db", ""), "unknown") for x in ranks]
# remove duplication
old_s_ranks = s_ranks.copy()
s_ranks = (
[old_s_ranks[0]]
+ [
old_s_ranks[i]
for i in range(1, len(old_s_ranks))
if old_s_ranks[i] not in old_s_ranks[:i]
]
if old_s_ranks
else []
)
f_ranks = [Node(x.split("_")[0], x.split("_")[1]) for x in ranks]
s_evaluator.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator.add_case(ranks=f_ranks, answer=Node(service, fault))
if fault == "cpu":
s_evaluator_cpu.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_cpu.add_case(ranks=f_ranks, answer=Node(service, fault))
s_evaluator_all.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_all.add_case(ranks=f_ranks, answer=Node(service, fault))
elif fault == "mem":
s_evaluator_mem.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_mem.add_case(ranks=f_ranks, answer=Node(service, fault))
s_evaluator_all.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_all.add_case(ranks=f_ranks, answer=Node(service, fault))
elif fault == "delay":
s_evaluator_lat.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_lat.add_case(ranks=f_ranks, answer=Node(service, "latency"))
s_evaluator_all.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_all.add_case(ranks=f_ranks, answer=Node(service, "latency"))
elif fault == "loss":
s_evaluator_loss.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_loss.add_case(ranks=f_ranks, answer=Node(service, "latency"))
s_evaluator_all.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_all.add_case(ranks=f_ranks, answer=Node(service, "latency"))
elif fault == "disk":
s_evaluator_io.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_io.add_case(ranks=f_ranks, answer=Node(service, "diskio"))
s_evaluator_all.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_all.add_case(ranks=f_ranks, answer=Node(service, "diskio"))
elif fault == "socket":
s_evaluator_socket.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_socket.add_case(ranks=f_ranks, answer=Node(service, "socket"))
s_evaluator_all.add_case(ranks=s_ranks, answer=Node(service, "unknown"))
f_evaluator_all.add_case(ranks=f_ranks, answer=Node(service, "socket"))
eval_data["service-fault"].append(f"{service}_{fault}")
eval_data["top_1_service"].append(s_evaluator.accuracy(1))
eval_data["top_3_service"].append(s_evaluator.accuracy(3))
eval_data["top_5_service"].append(s_evaluator.accuracy(5))
eval_data["avg@5_service"].append(s_evaluator.average(5))
eval_data["top_1_metric"].append(f_evaluator.accuracy(1))
eval_data["top_3_metric"].append(f_evaluator.accuracy(3))
eval_data["top_5_metric"].append(f_evaluator.accuracy(5))
eval_data["avg@5_metric"].append(f_evaluator.average(5))
print("--- Evaluation results ---")
for name, s_evaluator, f_evaluator in [
("cpu", s_evaluator_cpu, f_evaluator_cpu),
("mem", s_evaluator_mem, f_evaluator_mem),
("io", s_evaluator_io, f_evaluator_io),
("socket", s_evaluator_socket, f_evaluator_socket),
("delay", s_evaluator_lat, f_evaluator_lat),
("loss", s_evaluator_loss, f_evaluator_loss),
]:
eval_data["service-fault"].append(f"overall_{name}")
eval_data["top_1_service"].append(s_evaluator.accuracy(1))
eval_data["top_3_service"].append(s_evaluator.accuracy(3))
eval_data["top_5_service"].append(s_evaluator.accuracy(5))
eval_data["avg@5_service"].append(s_evaluator.average(5))
eval_data["top_1_metric"].append(f_evaluator.accuracy(1))
eval_data["top_3_metric"].append(f_evaluator.accuracy(3))
eval_data["top_5_metric"].append(f_evaluator.accuracy(5))
eval_data["avg@5_metric"].append(f_evaluator.average(5))
if name == "io":
name = "disk"
if s_evaluator.average(5) is not None:
print( f"Avg@5-{name.upper()}:".ljust(12), round(s_evaluator.average(5), 2))
print("---")
print("Avg speed:", avg_speed)