blob: a07d5ecc69417c45148e8b2452e4d4bcb6e7ee84 [file] [log] [blame]
Rafael Espindola6dc954a2017-11-14 16:40:301#!/usr/bin/env python
2#
Chandler Carruth2946cd72019-01-19 08:50:563# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rafael Espindola6dc954a2017-11-14 16:40:306#
7# ==------------------------------------------------------------------------==#
8
9import os
10import glob
11import re
12import subprocess
13import json
14import datetime
15import argparse
Tobias Hietaf98ee402023-05-17 14:59:4116
Serge Gueltona1aa7a42019-03-20 07:42:1317try:
18 from urllib.parse import urlencode
19 from urllib.request import urlopen, Request
20except ImportError:
21 from urllib import urlencode
22 from urllib2 import urlopen, Request
23
Rafael Espindola6dc954a2017-11-14 16:40:3024
25parser = argparse.ArgumentParser()
Tobias Hietaf98ee402023-05-17 14:59:4126parser.add_argument("benchmark_directory")
27parser.add_argument("--runs", type=int, default=10)
28parser.add_argument("--wrapper", default="")
29parser.add_argument("--machine", required=True)
30parser.add_argument("--revision", required=True)
31parser.add_argument("--threads", action="store_true")
32parser.add_argument(
33 "--url",
34 help="The lnt server url to send the results to",
35 default="http://localhost:8000/db_default/v4/link/submitRun",
36)
Rafael Espindola6dc954a2017-11-14 16:40:3037args = parser.parse_args()
38
Tobias Hietaf98ee402023-05-17 14:59:4139
Rafael Espindola6dc954a2017-11-14 16:40:3040class Bench:
41 def __init__(self, directory, variant):
42 self.directory = directory
43 self.variant = variant
Tobias Hietaf98ee402023-05-17 14:59:4144
Rafael Espindola6dc954a2017-11-14 16:40:3045 def __str__(self):
46 if not self.variant:
47 return self.directory
Tobias Hietaf98ee402023-05-17 14:59:4148 return "%s-%s" % (self.directory, self.variant)
49
Rafael Espindola6dc954a2017-11-14 16:40:3050
51def getBenchmarks():
52 ret = []
Tobias Hietaf98ee402023-05-17 14:59:4153 for i in glob.glob("*/response*.txt"):
54 m = re.match("response-(.*)\.txt", os.path.basename(i))
Rafael Espindola6dc954a2017-11-14 16:40:3055 variant = m.groups()[0] if m else None
56 ret.append(Bench(os.path.dirname(i), variant))
57 return ret
58
Tobias Hietaf98ee402023-05-17 14:59:4159
Rafael Espindola6dc954a2017-11-14 16:40:3060def parsePerfNum(num):
Tobias Hietaf98ee402023-05-17 14:59:4161 num = num.replace(b",", b"")
Rafael Espindola6dc954a2017-11-14 16:40:3062 try:
63 return int(num)
64 except ValueError:
65 return float(num)
66
Tobias Hietaf98ee402023-05-17 14:59:4167
Rafael Espindola6dc954a2017-11-14 16:40:3068def parsePerfLine(line):
69 ret = {}
Tobias Hietaf98ee402023-05-17 14:59:4170 line = line.split(b"#")[0].strip()
Rafael Espindola6dc954a2017-11-14 16:40:3071 if len(line) != 0:
72 p = line.split()
Tobias Hietaf98ee402023-05-17 14:59:4173 ret[p[1].strip().decode("ascii")] = parsePerfNum(p[0])
Rafael Espindola6dc954a2017-11-14 16:40:3074 return ret
75
Tobias Hietaf98ee402023-05-17 14:59:4176
Rafael Espindola6dc954a2017-11-14 16:40:3077def parsePerf(output):
78 ret = {}
Tobias Hietaf98ee402023-05-17 14:59:4179 lines = [x.strip() for x in output.split(b"\n")]
Rafael Espindola6dc954a2017-11-14 16:40:3080
Tobias Hietaf98ee402023-05-17 14:59:4181 seconds = [x for x in lines if b"seconds time elapsed" in x][0]
Rafael Espindola6dc954a2017-11-14 16:40:3082 seconds = seconds.strip().split()[0].strip()
Tobias Hietaf98ee402023-05-17 14:59:4183 ret["seconds-elapsed"] = parsePerfNum(seconds)
Rafael Espindola6dc954a2017-11-14 16:40:3084
Tobias Hietaf98ee402023-05-17 14:59:4185 measurement_lines = [x for x in lines if b"#" in x]
Rafael Espindola6dc954a2017-11-14 16:40:3086 for l in measurement_lines:
87 ret.update(parsePerfLine(l))
88 return ret
89
Tobias Hietaf98ee402023-05-17 14:59:4190
Rafael Espindola6dc954a2017-11-14 16:40:3091def run(cmd):
92 try:
93 return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
94 except subprocess.CalledProcessError as e:
95 print(e.output)
96 raise e
97
Tobias Hietaf98ee402023-05-17 14:59:4198
Rafael Espindola6dc954a2017-11-14 16:40:3099def combinePerfRun(acc, d):
Tobias Hietaf98ee402023-05-17 14:59:41100 for k, v in d.items():
Rafael Espindola6dc954a2017-11-14 16:40:30101 a = acc.get(k, [])
102 a.append(v)
103 acc[k] = a
104
Tobias Hietaf98ee402023-05-17 14:59:41105
Rafael Espindola6dc954a2017-11-14 16:40:30106def perf(cmd):
107 # Discard the first run to warm up any system cache.
108 run(cmd)
109
110 ret = {}
Tobias Hietaf98ee402023-05-17 14:59:41111 wrapper_args = [x for x in args.wrapper.split(",") if x]
Rafael Espindola6dc954a2017-11-14 16:40:30112 for i in range(args.runs):
Tobias Hietaf98ee402023-05-17 14:59:41113 os.unlink("t")
114 out = run(wrapper_args + ["perf", "stat"] + cmd)
Rafael Espindola6dc954a2017-11-14 16:40:30115 r = parsePerf(out)
116 combinePerfRun(ret, r)
Tobias Hietaf98ee402023-05-17 14:59:41117 os.unlink("t")
Rafael Espindola6dc954a2017-11-14 16:40:30118 return ret
119
Tobias Hietaf98ee402023-05-17 14:59:41120
Rafael Espindola6dc954a2017-11-14 16:40:30121def runBench(bench):
Tobias Hietaf98ee402023-05-17 14:59:41122 thread_arg = [] if args.threads else ["--no-threads"]
Rafael Espindola6dc954a2017-11-14 16:40:30123 os.chdir(bench.directory)
Tobias Hietaf98ee402023-05-17 14:59:41124 suffix = "-%s" % bench.variant if bench.variant else ""
125 response = "response" + suffix + ".txt"
126 ret = perf(["../ld.lld", "@" + response, "-o", "t"] + thread_arg)
127 ret["name"] = str(bench)
128 os.chdir("..")
Rafael Espindola6dc954a2017-11-14 16:40:30129 return ret
130
Tobias Hietaf98ee402023-05-17 14:59:41131
Rafael Espindola6dc954a2017-11-14 16:40:30132def buildLntJson(benchmarks):
133 start = datetime.datetime.utcnow().isoformat()
134 tests = [runBench(b) for b in benchmarks]
135 end = datetime.datetime.utcnow().isoformat()
136 ret = {
Tobias Hietaf98ee402023-05-17 14:59:41137 "format_version": 2,
138 "machine": {"name": args.machine},
139 "run": {
140 "end_time": start,
141 "start_time": end,
142 "llvm_project_revision": args.revision,
Rafael Espindola6dc954a2017-11-14 16:40:30143 },
Tobias Hietaf98ee402023-05-17 14:59:41144 "tests": tests,
Rafael Espindola6dc954a2017-11-14 16:40:30145 }
146 return json.dumps(ret, sort_keys=True, indent=4)
147
Tobias Hietaf98ee402023-05-17 14:59:41148
Rafael Espindola6dc954a2017-11-14 16:40:30149def submitToServer(data):
Tobias Hietaf98ee402023-05-17 14:59:41150 data2 = urlencode({"input_data": data}).encode("ascii")
Serge Gueltona1aa7a42019-03-20 07:42:13151 urlopen(Request(args.url, data2))
Rafael Espindola6dc954a2017-11-14 16:40:30152
Tobias Hietaf98ee402023-05-17 14:59:41153
Rafael Espindola6dc954a2017-11-14 16:40:30154os.chdir(args.benchmark_directory)
155data = buildLntJson(getBenchmarks())
156submitToServer(data)