Repository navigation
Expand file tree
/
Copy patharray_buffer_allocator_floor_bench_internal_test.go
More file actions
111 lines (104 loc) · 3.33 KB
/
Copy patharray_buffer_allocator_floor_bench_internal_test.go
File metadata and controls
111 lines (104 loc) · 3.33 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//go:build windows && amd64
package gov8
import (
"sync/atomic"
"syscall"
"testing"
)
// BenchmarkArrayBufferAllocatorBackingStoreNativeFloor retains the exact host
// allocator and its two Go callbacks while keeping the short-lived store in
// native handles. It excludes the public BackingStore wrapper allocation and
// the extra allocator reference that permits a public store to outlive its
// isolate. The benchmark is diagnostic; public ownership semantics are not
// weakened.
func BenchmarkArrayBufferAllocatorBackingStoreNativeFloor(b *testing.B) {
var allocations atomic.Uint64
var frees atomic.Uint64
allocator, err := NewArrayBufferAllocator(ArrayBufferAllocatorCallbacks{
Allocate: func(int) bool {
allocations.Add(1)
return true
},
Free: func(int, byte) { frees.Add(1) },
})
if err != nil {
b.Fatal(err)
}
params := NewCreateParams()
if err := params.SetArrayBufferAllocator(allocator); err != nil {
b.Fatal(err)
}
iso, err := NewIsolateWithParams(params)
if err != nil {
b.Fatal(err)
}
b.Cleanup(func() {
_ = iso.Close()
_ = allocator.Close()
})
ensureBackingStoreHotProcs()
probe, _, _ := syscall.Syscall(backingStoreNewAddr, 2, iso.handle, 64, 0)
if probe == 0 {
b.Fatal(shimError("native-floor.NewBackingStore", 0))
}
status, _, _ := syscall.Syscall(backingStoreDisposeAddr, 1, probe, 0, 0)
if int64(status) < 0 {
b.Fatal(shimError("native-floor.BackingStore.Close", status))
}
if allocations.Load() != 1 || frees.Load() != 1 {
b.Fatalf("probe callbacks allocations=%d frees=%d", allocations.Load(), frees.Load())
}
beforeAllocations := allocations.Load()
beforeFrees := frees.Load()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handle, _, _ := syscall.Syscall(backingStoreNewAddr, 2, iso.handle, 64, 0)
if handle == 0 {
b.Fatal(shimError("native-floor.NewBackingStore", 0))
}
status, _, _ := syscall.Syscall(backingStoreDisposeAddr, 1, handle, 0, 0)
if int64(status) < 0 {
b.Fatal(shimError("native-floor.BackingStore.Close", status))
}
}
b.StopTimer()
if allocations.Load()-beforeAllocations != uint64(b.N) || frees.Load()-beforeFrees != uint64(b.N) {
b.Fatalf("callback deltas allocations=%d frees=%d iterations=%d",
allocations.Load()-beforeAllocations, frees.Load()-beforeFrees, b.N)
}
}
// BenchmarkDefaultArrayBufferAllocatorBackingStoreNativeFloor removes both
// Go callbacks and public wrapper/reference bookkeeping. It measures the two
// required native create/dispose crossings and V8's allocation work.
func BenchmarkDefaultArrayBufferAllocatorBackingStoreNativeFloor(b *testing.B) {
allocator, err := NewDefaultArrayBufferAllocator()
if err != nil {
b.Fatal(err)
}
params := NewCreateParams()
if err := params.SetArrayBufferAllocator(allocator); err != nil {
b.Fatal(err)
}
iso, err := NewIsolateWithParams(params)
if err != nil {
b.Fatal(err)
}
b.Cleanup(func() {
_ = iso.Close()
_ = allocator.Close()
})
ensureBackingStoreHotProcs()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
handle, _, _ := syscall.Syscall(backingStoreNewAddr, 2, iso.handle, 64, 0)
if handle == 0 {
b.Fatal(shimError("default-native-floor.NewBackingStore", 0))
}
status, _, _ := syscall.Syscall(backingStoreDisposeAddr, 1, handle, 0, 0)
if int64(status) < 0 {
b.Fatal(shimError("default-native-floor.BackingStore.Close", status))
}
}
}