diff options
-rw-r--r-- | meson.build | 2 | ||||
-rwxr-xr-x | scripts/benchmark.py | 14 | ||||
-rw-r--r-- | scripts/meson.build | 27 | ||||
-rwxr-xr-x | scripts/plot.py | 139 |
4 files changed, 108 insertions, 74 deletions
diff --git a/meson.build b/meson.build index 8cfa24a..8805474 100644 --- a/meson.build +++ b/meson.build @@ -279,6 +279,8 @@ if not get_option('benchmarks').disabled() endif endif +subdir('scripts') + if not meson.is_subproject() summary('Benchmarks', build_benchmarks, bool_yn: true) summary('Tests', not get_option('tests').disabled(), bool_yn: true) diff --git a/scripts/benchmark.py b/scripts/benchmark.py index 2249fd1..c160445 100755 --- a/scripts/benchmark.py +++ b/scripts/benchmark.py @@ -1,6 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -# Copyright 2011-2021 David Robillard <d@drobilla.net> +# Copyright 2011-2022 David Robillard <d@drobilla.net> # SPDX-License-Identifier: ISC """ @@ -30,18 +30,20 @@ subprocess.call( FILENAME = "gibberish.txt" CHARACTERS = string.ascii_letters + string.digits + " '." -WORDS = open("/usr/share/dict/words", "r").readlines() SCHEMES = ["http", "https"] DOMAINS = ["example.org", "drobilla.net", "lv2plug.in", "gitlab.com"] NAMES = ["ns", "foo", "bar", "stuff", "things"] +with open("/usr/share/dict/words", "r", encoding="utf-8") as words_file: + WORDS = words_file.readlines() + def random_word(): """Return a random word with ASCII letters and digits""" if random.randint(0, 1): # Generate a URI - result = "%s://%s/" % (random.choice(SCHEMES), random.choice(DOMAINS)) + result = f"{random.choice(SCHEMES)}://{random.choice(DOMAINS)}/" for _ in range(random.randrange(1, 6)): result += random.choice(NAMES) + "/" else: @@ -55,9 +57,9 @@ def random_word(): if not os.path.exists(FILENAME): - print("Generating random text %s" % FILENAME) + print(f"Generating random text {FILENAME}") - with open(FILENAME, "w") as out: + with open(FILENAME, "w", encoding="utf-8") as out: for i in range(1 << 20): out.write(random_word()) out.write("\n") diff --git a/scripts/meson.build b/scripts/meson.build new file mode 100644 index 0000000..c303ae4 --- /dev/null +++ b/scripts/meson.build @@ -0,0 +1,27 @@ +# Copyright 2020-2022 David Robillard <d@drobilla.net> +# SPDX-License-Identifier: CC0-1.0 OR ISC + +if get_option('strict') and not meson.is_subproject() + flake8 = find_program('flake8', required: get_option('tests')) + pylint = find_program('pylint', required: get_option('tests')) + black = find_program('black', required: get_option('tests')) + + # Scripts that pass with everything including pylint + scripts = files( + 'benchmark.py', + 'plot.py', + ) + + if is_variable('black') and black.found() + black_opts = ['-l', '79', '-q', '--check'] + test('black', black, args: black_opts + scripts, suite: 'scripts') + endif + + if is_variable('flake8') and flake8.found() + test('flake8', flake8, args: scripts, suite: 'scripts') + endif + + if is_variable('pylint') and pylint.found() + test('pylint', pylint, args: scripts, suite: 'scripts') + endif +endif diff --git a/scripts/plot.py b/scripts/plot.py index 9056df4..dbf79de 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -1,8 +1,12 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -# Copyright 2011-2020 David Robillard <d@drobilla.net> +# Copyright 2011-2022 David Robillard <d@drobilla.net> # SPDX-License-Identifier: ISC +""" +Plot a benchmark result. +""" + import math import os import sys @@ -20,7 +24,7 @@ matplotlib.rc( "serif": "Times", "sans-serif": "Helvetica", "monospace": "Courier", - } + }, ) pyplot.subplots_adjust(wspace=0.2, hspace=0.2) @@ -34,11 +38,9 @@ class SensibleScalarFormatter(matplotlib.ticker.ScalarFormatter): self.set_powerlimits([-6, 6]) self.set_scientific(True) - def _set_orderOfMagnitude(self, value_range): + def _set_order_of_magnitude(self): # Calculate "best" order in the usual way - matplotlib.ticker.ScalarFormatter._set_orderOfMagnitude( - self, value_range - ) + super()._set_order_of_magnitude() # Round down to sensible (millions, billions, etc) order self.orderOfMagnitude = self.orderOfMagnitude - ( @@ -48,67 +50,68 @@ class SensibleScalarFormatter(matplotlib.ticker.ScalarFormatter): self.set_scientific(True) -file_prefix = os.path.commonprefix(sys.argv[1:]) -n_plots = len(sys.argv) - 2 -for i in range(n_plots): - filename = sys.argv[i + 2] - file = open(filename, "r") - - ax = pyplot.subplot( - math.ceil(math.sqrt(n_plots)), math.ceil(math.sqrt(n_plots)), i + 1 - ) - - ax.xaxis.set_major_formatter(SensibleScalarFormatter()) - ax.yaxis.set_major_formatter(SensibleScalarFormatter()) - for a in ["x", "y"]: - ax.grid( - which="major", - axis=a, - zorder=1, - linewidth=0.5, - linestyle=":", - color="0", - dashes=[0.5, 8.0], +if __name__ == "__main__": + file_prefix = os.path.commonprefix(sys.argv[1:]) + N_PLOTS = len(sys.argv) - 2 + for i in range(N_PLOTS): + filename = sys.argv[i + 2] + + with open(filename, "r", encoding="utf-8") as in_file: + ax = pyplot.subplot( + math.ceil(math.sqrt(N_PLOTS)), + math.ceil(math.sqrt(N_PLOTS)), + i + 1, + ) + + ax.xaxis.set_major_formatter(SensibleScalarFormatter()) + ax.yaxis.set_major_formatter(SensibleScalarFormatter()) + for a in ["x", "y"]: + ax.grid( + which="major", + axis=a, + zorder=1, + linewidth=0.5, + linestyle=":", + color="0", + dashes=[0.5, 8.0], + ) + + header = in_file.readline() + columns = header[1:].split() + + pyplot.xlabel("Elements") + pyplot.ylabel("Time (s)") + + times = [] + for i in columns: + times.append([]) + + for line in in_file: + if line[0] == "#": + continue + + fields = line.split() + for index, field in enumerate(fields): + times[index].append([float(field)]) + + for i in range(len(times) - 1): + matplotlib.pyplot.plot( + times[0], times[i + 1], "-o", label=columns[i + 1] + ) + + pyplot.legend( + loc="upper left", + handletextpad=0.15, + borderpad=0.20, + borderaxespad=0, + labelspacing=0.10, + columnspacing=0, + framealpha=0.90, ) - header = file.readline() - columns = header[1:].split() - - pyplot.xlabel("Elements") - pyplot.ylabel("Time (s)") - - times = [] - for i in columns: - times.append([]) - for line in file: - if line[0] == "#": - continue - - fields = line.split() - num = 0 - for i in fields: - times[num].append([float(i)]) - num += 1 - - file.close() - - for i in range(len(times) - 1): - matplotlib.pyplot.plot( - times[0], times[i + 1], "-o", label=columns[i + 1] - ) + file_prefix_len = len(file_prefix) + pyplot.title(os.path.splitext(filename[file_prefix_len:])[0].title()) - pyplot.legend( - loc="upper left", - handletextpad=0.15, - borderpad=0.20, - borderaxespad=0, - labelspacing=0.10, - columnspacing=0, - framealpha=0.90, - ) - - pyplot.title(os.path.splitext(filename[len(file_prefix) :])[0].title()) - -print("Writing %s" % sys.argv[1]) -matplotlib.pyplot.tight_layout() -matplotlib.pyplot.savefig(sys.argv[1]) + print(f"Writing {sys.argv[1]}") + matplotlib.pyplot.tight_layout() + matplotlib.pyplot.savefig(sys.argv[1]) |