From 81ee3fded6d7b7bee859caaf44c7cd61d9ac4e88 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Wed, 10 Sep 2025 17:23:04 +0200 Subject: [PATCH] ex2: benchmark job --- exercise2/Makefile | 4 ++++ exercise2/bench.py | 24 ++++++++++++++++++++++++ flake.nix | 1 + 3 files changed, 29 insertions(+) create mode 100755 exercise2/bench.py diff --git a/exercise2/Makefile b/exercise2/Makefile index 40678c5..40ce328 100644 --- a/exercise2/Makefile +++ b/exercise2/Makefile @@ -4,6 +4,7 @@ SRC := mandel_mpi.c TARGET := $(SRC:.c=) ARGS := 1 OUTDIR := out +NBENCH := 1 .PHONY: all clean run @@ -18,3 +19,6 @@ run: $(TARGET) clean: rm -rf $(OUTDIR) + +time: $(TARGET) + python3 bench.py '$(OUTDIR)/$(TARGET) 0' $(NBENCH) diff --git a/exercise2/bench.py b/exercise2/bench.py new file mode 100755 index 0000000..7127c40 --- /dev/null +++ b/exercise2/bench.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +from statistics import mean +from subprocess import DEVNULL, run +from sys import argv, exit +from time import perf_counter + + +def benchmark(cmd, N=10): + times = [] + for _ in range(N): + start = perf_counter() + run(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL) + times.append(perf_counter() - start) + return mean(times) + + +if __name__ == "__main__": + if len(argv) < 2: + print("usage: python3 bench.py 'cmd' [N]") + exit(1) + cmd = argv[1] + N = int(argv[2]) if len(argv) > 2 else 10 + print(f"{benchmark(cmd, N):.4f}") diff --git a/flake.nix b/flake.nix index 816c361..1d6ecbc 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,7 @@ gnuplot ffmpeg mpi + python3 ]; shellHook = '' echo welcome!