summaryrefslogtreecommitdiff
path: root/src/main/java/bench/SingleBench.java
blob: 2ce39eb785eb37ec72e1db5bbf0e3270bbd73445 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package bench;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

public class SingleBench {

    public static void main(String[] args) throws Exception {
        if (args.length < 5) {
            System.err.println("Usage: SingleBench <configName> <threads> <durationSec> <warmupSec> <msgSize>");
            System.exit(1);
        }

        String configName = args[0];
        int threadCount = Integer.parseInt(args[1]);
        long durationSec = Long.parseLong(args[2]);
        long warmupSec = Long.parseLong(args[3]);
        int msgSize = Integer.parseInt(args[4]);

        String message = "X".repeat(msgSize);
        int messageBytes = message.getBytes().length;

        Logger logger = LogManager.getLogger("bench");

        CountDownLatch startLatch = new CountDownLatch(1);
        AtomicBoolean running = new AtomicBoolean(true);
        AtomicLong eventCounter = new AtomicLong(0);

        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < threadCount; i++) {
            Thread t = new Thread(() -> {
                try {
                    startLatch.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
                while (running.get()) {
                    logger.info(message);
                    eventCounter.incrementAndGet();
                }
            });
            t.start();
            threads.add(t);
        }

        // Warmup phase
        startLatch.countDown();
        if (warmupSec > 0) {
            Thread.sleep(warmupSec * 1000);
            eventCounter.set(0);
        }

        long startTime = System.nanoTime();
        Thread.sleep(durationSec * 1000);
        running.set(false);
        long endTime = System.nanoTime();

        for (Thread t : threads) {
            t.join(5000);
        }

        long events = eventCounter.get();
        double duration = (endTime - startTime) / 1_000_000_000.0;
        double eventsPerSec = events / duration;
        double mbPerSec = (events * messageBytes) / duration / (1024 * 1024);

        // Output CSV line: config,threads,events,duration,events_per_sec,mb_per_sec
        System.out.printf("%s,%d,%d,%.2f,%.0f,%.2f%n",
            configName, threadCount, events, duration, eventsPerSec, mbPerSec);

        // Only flush/shutdown if requested (default: no flush for max perf)
        boolean doFlush = Boolean.getBoolean("bench.flush");
        if (doFlush) {
            LogManager.shutdown();
        }
    }
}