-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLatencyBenchmark.java
More file actions
69 lines (58 loc) · 2.68 KB
/
Copy pathLatencyBenchmark.java
File metadata and controls
69 lines (58 loc) · 2.68 KB
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
package com.jpyrust;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
public class LatencyBenchmark {
public static void main(String[] args) throws Exception {
System.out.println("Starting Benchmark: Subprocess vs JNI+Rust FFI (Shared Memory)");
System.out.println("Initializing JPyRustBridge...");
JPyRustBridge bridge = new JPyRustBridge("benchmark_cam");
bridge.initialize();
byte[] dummyData = Files.readAllBytes(Paths.get("sample.png"));
int length = dummyData.length;
int width = 100;
int height = 100;
int channels = 3;
ByteBuffer directBuffer = ByteBuffer.allocateDirect(length);
directBuffer.put(dummyData);
directBuffer.flip();
int warmup = 2;
int iterations = 10;
System.out.println("Warming up...");
for (int i = 0; i < warmup; i++) {
bridge.processImage(directBuffer, length, width, height, channels);
}
System.out.println("\n[1] Testing Native JNI + Rust FFI (Shared Memory)");
long jniTotal = 0;
for (int i = 0; i < iterations; i++) {
directBuffer.position(0);
long start = System.nanoTime();
bridge.processImage(directBuffer, length, width, height, channels);
long end = System.nanoTime();
long durationMs = (end - start) / 1000000;
jniTotal += durationMs;
System.out.printf(" Call %d: %d ms\n", (i+1), durationMs);
}
long jniAvg = jniTotal / iterations;
System.out.println("\n[2] Testing Legacy Subprocess (ProcessBuilder)");
long subTotal = 0;
for (int i = 0; i < iterations; i++) {
directBuffer.clear();
directBuffer.put(dummyData);
directBuffer.flip();
long start = System.nanoTime();
bridge.runPythonRaw(directBuffer, length, width, height, channels);
long end = System.nanoTime();
long durationMs = (end - start) / 1000000;
subTotal += durationMs;
System.out.printf(" Call %d: %d ms\n", (i+1), durationMs);
}
long subAvg = subTotal / iterations;
System.out.println("\n================ BENCHMARK RESULTS ================");
System.out.printf("Legacy Subprocess Average : %d ms\n", subAvg);
System.out.printf("JPyRust (JNI+SHMEM) Avg : %d ms\n", jniAvg);
System.out.printf("Performance Improvement : %.2fx faster\n", (double)subAvg / Math.max(1, jniAvg));
System.out.println("===================================================");
bridge.close();
}
}