aboutsummaryrefslogtreecommitdiffstats
path: root/benchmark_throughput.py
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2025-05-14 12:25:52 +0300
committerJan Tuomi <jan@jantuomi.fi>2025-05-14 12:25:52 +0300
commit4b42039d22008dd65a1bb176d1b80e167b4d76a2 (patch)
treecc7ac8af3ee3e49836724feb496c8382c7b48e56 /benchmark_throughput.py
parent7fdc3a0403fb542616fca1626657eb826ac5b2f6 (diff)
Add and improve benchmark scripts
Diffstat (limited to 'benchmark_throughput.py')
-rw-r--r--benchmark_throughput.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/benchmark_throughput.py b/benchmark_throughput.py
new file mode 100644
index 0000000..5bfa4d5
--- /dev/null
+++ b/benchmark_throughput.py
@@ -0,0 +1,56 @@
+import os
+import time
+import tempfile
+import shutil
+import argparse
+from random import randint
+from femtoqueue import FemtoQueue
+
+def run_benchmark(duration_seconds: int, payload_size: int = 100):
+ print(f"Running throughput benchmark for {duration_seconds} sec. See -h for help.")
+ tmpdir = tempfile.mkdtemp()
+ q = FemtoQueue(data_dir=tmpdir, node_name="node1")
+ payload = b"x" * payload_size
+
+ total_count = 0
+ count_per_second = 0
+ start_time = last_report_time = time.time()
+
+ try:
+ while True:
+ now = time.time()
+ if now - start_time >= duration_seconds:
+ break
+
+ # Push, pop, done, and delete task.
+ # NOTE: one cycle must take way less than one second for the math to be accurate.
+ n_pushed_tasks = randint(0, 50)
+ for _ in range(n_pushed_tasks):
+ q.push(payload)
+
+ while task := q.pop():
+ q.done(task)
+ total_count += 1
+ count_per_second += 1
+
+ # Print per-second throughput
+ if now - last_report_time >= 1.0:
+ print(f"{int(now - start_time)}s: {count_per_second} tasks/sec")
+ count_per_second = 0
+ last_report_time = now
+
+ finally:
+ elapsed = time.time() - start_time
+ print("Cleaning up...")
+ shutil.rmtree(tmpdir)
+
+ print(f"\nRan for {elapsed:.2f} seconds")
+ print(f"Total tasks processed: {total_count}")
+ print(f"Overall throughput: {total_count / elapsed:.2f} tasks/sec")
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser(description="FemtoQueue throughput benchmark")
+ parser.add_argument("duration", type=int, help="Duration to run the benchmark (in seconds)", default=10)
+ args = parser.parse_args()
+
+ run_benchmark(args.duration)