2 # Copyright (C) 2019 - Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from statistics
import mean
23 from operator
import add
25 import matplotlib
.pyplot
as plt
26 from matplotlib
.backends
.backend_pdf
import PdfPages
27 from matplotlib
.ticker
import PercentFormatter
33 from minio
import Minio
34 from minio
.error
import NoSuchKey
35 from minio
.error
import ResponseError
38 BENCHMARK_TYPES
= ["dummy", "text"]
39 DEFAULT_BUCKET
= "lava"
42 "ec9a9794af488a9accce7708a8b0d8188b498789", # Does not build
43 "8c99128c640cbce71fb8a6caa15e4c672252b662", # Block on configure
44 "f3847c753f1b4f12353c38d97b0577d9993d19fb", # Does not build
45 "e0111295f17ddfcc33ec771a8deac505473a06ad", # Does not build
46 "d0d4e0ed487ea23aaf0d023513c0a4d86901b79b", # Does not build
47 "c24f7ab4dd9edeb5e50b0070fd9d9e8691057dde", # Does not build
48 "ce67f5614a4db3b2de4d887eca52135b439b4937", # Does not build
49 "80aff5efc66679fd934cef433c0e698694748385", # Does not build
50 "f4f11e84942d36fcc8a597d226928bce2ccac4b3", # Does not build
53 def json_type(string
):
55 Argpase type for json args.
56 We expect a base dictionary.
58 passed_json
= json
.loads(string
)
59 if not isinstance(passed_json
, dict):
60 msg
= "%r is not a dict" % string
61 raise argparse
.ArgumentTypeError(msg
)
64 def graph_get_color(branch
):
66 Get the color matching the branch.
68 color
= {"stable-1.5": "red", "stable-2.0": "green", "master": "blue"}
72 def graph_get_title(branch
, benchmark_type
):
74 Get title for graph based on benchmark type.
76 string
= {"dummy": "Dummy output", "text": "Text output"}
77 return "{} - {}".format(branch
, string
[benchmark_type
])
82 Return minio client configured.
85 "obj.internal.efficios.com", access_key
="jenkins", secret_key
="echo123456"
89 def get_file(client
, prefix
, file_name
, workdir_name
):
91 Return the path of the downloaded file.
94 destination
= os
.path
.join(workdir_name
, file_name
)
95 object_name
= "{}/{}".format(prefix
, file_name
)
97 client
.fget_object(DEFAULT_BUCKET
, object_name
, destination
)
104 def delete_file(client
, prefix
, file_name
):
106 Delete the file on remote.
108 object_name
= "{}/{}".format(prefix
, file_name
)
110 client
.remove_object(DEFAULT_BUCKET
, object_name
)
111 except ResponseError
as err
:
117 def get_git_log(bt_version
, cutoff
, repo_path
):
119 Return an ordered (older to newer) list of commits for the bt_version and
120 cutoff. WARNING: This changes the git repo HEAD.
122 repo
= git
.Repo(repo_path
)
125 "{}..origin/{}".format(cutoff
, bt_version
), "--pretty=format:%H", "--reverse"
129 def parse_result(result_path
):
131 Parse the result file. Return a dataset of User time + System time.
133 with
open(result_path
) as result
:
134 parsed_result
= json
.load(result
)
138 parsed_result
["User time (seconds)"],
139 parsed_result
["System time (seconds)"],
144 def get_benchmark_results(client
, commit
, workdir
):
146 Fetch the benchmark result from a certain commit across all benchmark type.
149 benchmark_valid
= True
150 for b_type
in BENCHMARK_TYPES
:
151 prefix
= "/results/benchmarks/babeltrace/{}".format(b_type
)
152 result_file
= get_file(client
, prefix
, commit
, workdir
)
155 Benchmark is either corrupted or not complete.
157 return None, benchmark_valid
158 results
[b_type
] = parse_result(result_file
)
159 if all(i
== 0.0 for i
in results
[b_type
]):
160 benchmark_valid
= False
161 print("Invalid benchmark for {}/{}/{}".format(prefix
, b_type
, commit
))
162 # The dataset is valid return immediately.
163 return results
, benchmark_valid
166 def plot_raw_value(branch
, benchmark_type
, x_data
, y_data
, labels
, latest_values
):
168 Plot the graph using the raw value.
174 for pos
in range(len(x_data
)):
176 valid_points
, outliers
= sanitize_dataset(y_data
[pos
])
177 for y
in valid_points
:
178 point_x_data
.append(x
)
179 point_y_data
.append(y
)
181 outlier_x_data
.append(x
)
182 outlier_y_data
.append(y
)
185 point_x_data
, point_y_data
, "o", label
=branch
, color
=graph_get_color(branch
)
187 plt
.plot(outlier_x_data
, outlier_y_data
, "+", label
="outlier", color
="black")
191 ymin
= 0.8 * min([item
for sublist
in y_data
for item
in sublist
])
192 ymax
= 1.2 * max([item
for sublist
in y_data
for item
in sublist
])
193 # Put latest of other branches for reference as horizontal line.
194 for l_branch
, l_result
in latest_values
.items():
195 if not l_result
or l_branch
== branch
:
199 label
="Latest {}".format(l_branch
),
200 color
=graph_get_color(l_branch
),
203 ymax
= 1.2 * l_result
205 plt
.ylim(ymin
=0, ymax
=ymax
)
206 plt
.xticks(x_data
, labels
, rotation
=90, family
="monospace")
207 plt
.title(graph_get_title(branch
, benchmark_type
), fontweight
="bold")
208 plt
.ylabel("User + system time (s)")
209 plt
.xlabel("Latest commits")
213 # Put tick on the right side
214 ax
.tick_params(labeltop
=False, labelright
=True)
220 def plot_delta_between_point(
221 branch
, benchmark_type
, x_data
, y_data
, labels
, latest_values
224 Plot the graph of delta between each sequential commit.
228 # Transform y_data to a list of for which the reference is the first
231 for pos
, y
in enumerate(y_data
):
233 local_y_data
.append(0.0)
235 local_y_data
.append(y
- y_data
[pos
- 1])
237 plt
.plot(x_data
, local_y_data
, "o", label
=branch
, color
=graph_get_color(branch
))
239 # Get max absolute value to align the y axis with zero in the middle.
241 local_abs_max
= abs(max(local_y_data
, key
=abs)) * 1.3
243 plt
.ylim(ymin
=local_abs_max
* -1, ymax
=local_abs_max
)
246 plt
.xticks(x_data
, labels
, rotation
=90, family
="monospace")
248 graph_get_title(branch
, benchmark_type
) + " Delta to previous commit",
251 plt
.ylabel("Seconds")
252 plt
.xlabel("Latest commits")
256 # Put tick on the right side
257 ax
.tick_params(labeltop
=False, labelright
=True)
263 def plot_ratio(branch
, benchmark_type
, x_data
, y_data
, labels
, latest_values
):
265 Plot the graph using a ratio using first point as reference (0%).
271 reference
= y_data
[0]
273 # Transform y_data to a list of ratio for which the reference is the first
275 local_y_data
= list(map(lambda y
: ((y
/ reference
) - 1.0) * 100, y_data
))
277 plt
.plot(x_data
, local_y_data
, "o", label
=branch
, color
=graph_get_color(branch
))
279 # Put latest of other branches for reference as horizontal line.
280 for l_branch
, l_result
in latest_values
.items():
281 if not l_result
or l_branch
== branch
:
283 ratio_l_result
= ((l_result
/ reference
) - 1.0) * 100.0
285 "branch {} branch {} value {} l_result {} reference {}".format(
286 branch
, l_branch
, ratio_l_result
, l_result
, reference
291 label
="Latest {}".format(l_branch
),
292 color
=graph_get_color(l_branch
),
295 # Draw the reference line.
296 plt
.axhline(y
=0, label
="Reference (leftmost point)", linestyle
="-", color
="Black")
298 # Get max absolute value to align the y axis with zero in the middle.
300 local_abs_max
= abs(max(local_y_data
, key
=abs)) * 1.3
302 y_abs_max
= local_abs_max
304 plt
.ylim(ymin
=y_abs_max
* -1, ymax
=y_abs_max
)
307 percent_formatter
= PercentFormatter()
308 ax
.yaxis
.set_major_formatter(percent_formatter
)
309 ax
.yaxis
.set_minor_formatter(percent_formatter
)
310 plt
.xticks(x_data
, labels
, rotation
=90, family
="monospace")
311 plt
.title(graph_get_title(branch
, benchmark_type
), fontweight
="bold")
313 plt
.xlabel("Latest commits")
317 # Put tick on the right side
318 ax
.tick_params(labeltop
=False, labelright
=True)
324 def generate_graph(branches
, report_name
, git_path
):
327 pdf_pages
= PdfPages(report_name
)
329 client
= get_client()
330 branch_results
= dict()
332 # Fetch the results for each branch.
333 for branch
, cutoff
in branches
.items():
334 commits
= get_git_log(branch
, cutoff
, git_path
)
336 with tempfile
.TemporaryDirectory() as workdir
:
337 for commit
in commits
:
338 b_results
, valid
= get_benchmark_results(client
, commit
, workdir
)
339 if not b_results
or not valid
:
341 results
.append((commit
, b_results
))
342 branch_results
[branch
] = results
344 for b_type
in BENCHMARK_TYPES
:
348 # Find the maximum size for a series inside our series dataset.
349 # This is used later to compute the size of the actual plot (pdf).
350 # While there gather the comparison value used to draw comparison line
352 for branch
, results
in branch_results
.items():
353 max_len
= max([max_len
, len(results
)])
355 latest_values
[branch
] = mean(
356 sanitize_dataset(results
[-1][1][b_type
])[0]
359 latest_values
[branch
] = None
361 for branch
, results
in branch_results
.items():
362 # Create a figure instance
363 if max_len
and max_len
> 10:
364 width
= 0.16 * max_len
368 x_data
= list(range(len(results
)))
369 y_data
= [c
[1][b_type
] for c
in results
]
370 labels
= [c
[0][:8] for c
in results
]
372 fig
= plt
.figure(figsize
=(width
, 8.27), dpi
=100)
373 plot_raw_value(branch
, b_type
, x_data
, y_data
, labels
, latest_values
)
374 pdf_pages
.savefig(fig
)
376 # Use the mean of each sanitize dataset here, we do not care for
377 # variance for ratio. At least not yet.
378 y_data
= [mean(sanitize_dataset(c
[1][b_type
])[0]) for c
in results
]
379 fig
= plt
.figure(figsize
=(width
, 8.27), dpi
=100)
380 plot_ratio(branch
, b_type
, x_data
, y_data
, labels
, latest_values
)
381 pdf_pages
.savefig(fig
)
383 fig
= plt
.figure(figsize
=(width
, 8.27), dpi
=100)
384 plot_delta_between_point(
385 branch
, b_type
, x_data
, y_data
, labels
, latest_values
387 pdf_pages
.savefig(fig
)
392 def launch_jobs(branches
, git_path
, wait_for_completion
, debug
, force
):
394 Lauch jobs for all missing results.
396 client
= get_client()
397 for branch
, cutoff
in branches
.items():
398 commits
= get_git_log(branch
, cutoff
, git_path
)
400 with tempfile
.TemporaryDirectory() as workdir
:
401 for commit
in commits
:
402 if commit
in invalid_commits
:
404 b_results
= get_benchmark_results(client
, commit
, workdir
)[0]
405 if b_results
and not force
:
408 commit
, wait_for_completion
=wait_for_completion
, debug
=debug
414 Parse arguments and execute as needed.
417 "master": "31976fe2d70a8b6b7f8b31b9e0b3bc004d415575",
418 "stable-2.0": "07f585356018b4ddfbd0e09c49a14e38977c6973",
419 "stable-1.5": "49e98b837a5667130e0d1e062a6bd7985c7c4582",
422 parser
= argparse
.ArgumentParser(description
="Babeltrace benchmark utility")
424 "--generate-jobs", action
="store_true", help="Generate and send jobs"
427 "--force-jobs", action
="store_true", help="Force the queueing of jobs to lava"
430 "--do-not-wait-on-completion",
433 help="Wait for the completion of each jobs sent. This is useful"
434 "for the ci. Otherwise we could end up spaming the lava instance.",
439 help="Generate graphs and save them to pdf",
442 "--report-name", default
="report.pdf", help="The name of the pdf report."
445 "--debug", action
="store_true", default
=False, help="Do not send jobs to lava."
448 "--repo-path", help="The location of the git repo to use.", required
=True
451 "--overwrite-branches-cutoff",
452 help="A dictionary of the form {"
453 "'branch_name': 'commit_hash_cutoff',...}. Allow custom graphing and"
455 required
=False, type=json_type
458 args
= parser
.parse_args()
460 if args
.overwrite_branches_cutoff
:
461 bt_branches
= args
.overwrite_branches_cutoff
463 if not os
.path
.exists(args
.repo_path
):
464 print("Repository location does not exists.")
467 if args
.generate_jobs
:
468 print("Launching jobs for:")
470 for branch
, cutoff
in bt_branches
.items():
471 print("\t Branch {} with cutoff {}".format(branch
, cutoff
))
476 not args
.do_not_wait_on_completion
,
481 if args
.generate_report
:
482 print("Generating pdf report ({}) for:".format(args
.report_name
))
483 for branch
, cutoff
in bt_branches
.items():
484 print("\t Branch {} with cutoff {}".format(branch
, cutoff
))
485 generate_graph(bt_branches
, args
.report_name
, args
.repo_path
)
490 def sanitize_dataset(dataset
):
492 Use IRQ 1.5 [1] to remove outlier from the dataset. This is useful to get a
493 representative mean without outlier in it.
494 [1] https://en.wikipedia.org/wiki/Interquartile_range#Outliers
496 sorted_data
= sorted(dataset
)
497 q1
, q3
= numpy
.percentile(sorted_data
, [25, 75])
499 lower_bound
= q1
- (1.5 * iqr
)
500 upper_bound
= q3
+ (1.5 * iqr
)
504 if lower_bound
<= i
<= upper_bound
:
505 new_dataset
.append(i
)
508 return new_dataset
, outliers
511 if __name__
== "__main__":