Repository navigation
Expand file tree
/
Copy pathExpVirtualThreadsBenchmark.java
More file actions
77 lines (63 loc) · 3.89 KB
/
Copy pathExpVirtualThreadsBenchmark.java
File metadata and controls
77 lines (63 loc) · 3.89 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
70
71
72
73
74
75
76
77
import java.util.concurrent.*;
public class ExpVirtualThreadsBenchmark {
static final int TASKS = 10_000;
static final int IO_SLEEP = 10; // ms, simula I/O bloqueante
static final int POOL_SIZE = 200; // hilos de plataforma
public static void main(String[] args) throws Exception {
long platformMs = benchmarkPlatform();
long virtualMs = benchmarkVirtual();
System.out.println();
System.out.println("┌──────────────────────────────┬────────────────┬───────────────┬──────────────────┐");
System.out.println("│ Estrategia │ Hilos activos │ Tiempo (ms) │ Throughput │");
System.out.println("├──────────────────────────────┼────────────────┼───────────────┼──────────────────┤");
System.out.printf( "│ %-28s │ %-14s │ %-13d │ %-16s │%n",
"Pool fijo (" + POOL_SIZE + " plataforma)", "" + POOL_SIZE,
platformMs, throughput(TASKS, platformMs));
System.out.printf( "│ %-28s │ %-14s │ %-13d │ %-16s │%n",
"Virtual thread per task", "" + TASKS,
virtualMs, throughput(TASKS, virtualMs));
System.out.println("└──────────────────────────────┴────────────────┴───────────────┴──────────────────┘");
System.out.println();
System.out.println("Pool: " + TASKS + " tareas / " + POOL_SIZE + " hilos → "
+ (TASKS / POOL_SIZE) + " batches × " + IO_SLEEP + " ms ≈ "
+ ((TASKS / POOL_SIZE) * IO_SLEEP) + " ms esperados");
System.out.println("Virtual: todas corren a la vez → ~" + IO_SLEEP + " ms esperados");
}
static long benchmarkPlatform() throws Exception {
System.out.println("Ejecutando con pool de " + POOL_SIZE + " hilos de plataforma...");
ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE);
long t0 = System.currentTimeMillis();
CompletableFuture<?>[] cfs = new CompletableFuture[TASKS];
for (int i = 0; i < TASKS; i++) {
cfs[i] = CompletableFuture.runAsync(ExpVirtualThreadsBenchmark::ioTask, pool);
}
CompletableFuture.allOf(cfs).join();
long elapsed = System.currentTimeMillis() - t0;
pool.shutdown();
System.out.println(" Plataforma terminó en " + elapsed + " ms");
return elapsed;
}
static long benchmarkVirtual() throws Exception {
System.out.println("Ejecutando con virtual thread per task...");
// newVirtualThreadPerTaskExecutor: lanza un virtual thread por cada tarea enviada
long t0 = System.currentTimeMillis();
try (ExecutorService exec = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<?>[] cfs = new CompletableFuture[TASKS];
for (int i = 0; i < TASKS; i++) {
cfs[i] = CompletableFuture.runAsync(ExpVirtualThreadsBenchmark::ioTask, exec);
}
CompletableFuture.allOf(cfs).join();
}
long elapsed = System.currentTimeMillis() - t0;
System.out.println(" Virtual terminó en " + elapsed + " ms");
return elapsed;
}
static void ioTask() {
try { Thread.sleep(IO_SLEEP); } catch (InterruptedException ignored) {}
}
static String throughput(int tasks, long ms) {
if (ms == 0) return "∞ t/s";
long tps = tasks * 1000L / ms;
return tps + " t/s";
}
}