From fc5b67939fa700e3556910bed41e2dc32738c4e0 Mon Sep 17 00:00:00 2001 From: Sebastian Thurm Date: Tue, 4 Jun 2024 21:16:45 +0200 Subject: [PATCH] #469 Add synthetic and cprofile benchmark with main entry --- benchmarking/cprofile_setup.py | 45 +++++++++++++++++++++++++++++ benchmarking/synthetic_benchmark.py | 36 +++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 benchmarking/cprofile_setup.py create mode 100644 benchmarking/synthetic_benchmark.py diff --git a/benchmarking/cprofile_setup.py b/benchmarking/cprofile_setup.py new file mode 100644 index 0000000..8ccc775 --- /dev/null +++ b/benchmarking/cprofile_setup.py @@ -0,0 +1,45 @@ +import cProfile +import io +import pstats +from pathlib import Path + +import numpy as np + +from lindemann.main import main + + +def profile_lindemann(args, number_of_runs=5): + + main(args) # Warmup + all_times = [] + for run_number in range(number_of_runs): + + profiler = cProfile.Profile() + + profiler.enable() + main(args) + profiler.disable() + + stats_stream = io.StringIO() + stats = pstats.Stats(profiler, stream=stats_stream).sort_stats("tottime") + stats.print_stats() + + stats_stream.seek(0) + + for line in stats_stream: + if "tottime" in line: + line = next(stats_stream) + time = float(line.split()[1]) + all_times.append(time) + print(f"Time for run {run_number + 1}: {time} seconds") + break + + all_times = np.array(all_times) + mean_time = np.mean(all_times) + std_time = np.std(all_times) + print(f"Mean time of {number_of_runs} runs: {mean_time:.6f} s ± {std_time:.6f} s") + + +if __name__ == "__main__": + args = [Path("tests/test_example/459_01.lammpstrj")] + profile_lindemann(args, number_of_runs=5) diff --git a/benchmarking/synthetic_benchmark.py b/benchmarking/synthetic_benchmark.py new file mode 100644 index 0000000..dc780aa --- /dev/null +++ b/benchmarking/synthetic_benchmark.py @@ -0,0 +1,36 @@ +import time + +import numpy as np + +from lindemann.index import per_atoms, per_frames, per_trj + + +def generate_test_data(num_frames, num_atoms): + rng = np.random.default_rng(seed=42) + return rng.random((num_frames, num_atoms, 3)).astype(np.float32) + + +def benchmark(function, positions, iterations=3): + index = function(positions) # Warm-up + times = [] + for _ in range(iterations): + start_time = time.time() + function(positions) + end_time = time.time() + times.append(end_time - start_time) + return np.mean(times), np.std(times), index + + +def main(): + num_frames = 5000 + num_atoms = 1103 + positions = generate_test_data(num_frames, num_atoms) + iterations = 1 + + mean_time, std_time, index = benchmark(per_trj.calculate, positions, iterations=iterations) + print(f"Mean time of {iterations} runs: {mean_time:.6f} s ± {std_time:.6f} s") + print(f"Index: {index}") + + +if __name__ == "__main__": + main()