aboutsummaryrefslogtreecommitdiffstats
path: root/benchmark_throughput.py
blob: 5bfa4d55d67cf650803eea4ef33466f17d5da5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)