-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
72 lines (60 loc) · 1.35 KB
/
benchmark_test.go
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
package nodejs_test
import (
"context"
"testing"
"time"
"github.com/KarpelesLab/nodejs"
)
func BenchmarkEval(b *testing.B) {
// Check NodeJS availability
_, err := nodejs.New()
if err != nil {
b.Fatalf("NodeJS not available: %s", err)
}
// Setup
f, _ := nodejs.New()
proc, err := f.New()
if err != nil {
b.Fatalf("failed to create process: %s", err)
}
defer proc.Close()
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Run the benchmark
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := proc.Eval(ctx, "1+1", nil)
if err != nil {
b.Fatalf("eval failed: %s", err)
}
}
}
func BenchmarkPooledEval(b *testing.B) {
// Check NodeJS availability
f, err := nodejs.New()
if err != nil {
b.Fatalf("NodeJS not available: %s", err)
}
// Create a pool
pool := f.NewPool(4, 8)
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Wait for pool initialization
time.Sleep(1 * time.Second)
// Run the benchmark
b.ResetTimer()
for i := 0; i < b.N; i++ {
proc, err := pool.Take(ctx)
if err != nil {
b.Fatalf("failed to get process from pool: %s", err)
}
_, err = proc.Eval(ctx, "1+1", nil)
if err != nil {
proc.Close()
b.Fatalf("eval failed: %s", err)
}
proc.Close()
}
}