-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuffer_mapping_bench_test.go
More file actions
132 lines (118 loc) · 3.29 KB
/
Copy pathbuffer_mapping_bench_test.go
File metadata and controls
132 lines (118 loc) · 3.29 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:build !rust && !(js && wasm)
// Copyright 2026 The GoGPU Authors
// SPDX-License-Identifier: MIT
package wgpu_test
import (
"context"
"testing"
"github.com/gogpu/wgpu"
)
// FEAT-WGPU-MAPPING-001 — zero-alloc benchmark gate for Buffer.Map.
//
// The primary blocking path must allocate 0 bytes per iteration after
// warm-up. The async escape-hatch path must also stay zero-alloc when
// driven via sync.Pool (MapPending). If either gate regresses, CI
// should fail.
func setupMapBench(b *testing.B) (*wgpu.Instance, *wgpu.Adapter, *wgpu.Device, *wgpu.Buffer) {
b.Helper()
instance, err := wgpu.CreateInstance(nil)
if err != nil {
b.Skipf("cannot create instance: %v", err)
}
adapter, err := instance.RequestAdapter(nil)
if err != nil {
instance.Release()
b.Skipf("cannot request adapter: %v", err)
}
device, err := adapter.RequestDevice(nil)
if err != nil {
adapter.Release()
instance.Release()
b.Skipf("cannot request device: %v", err)
}
if device.Queue() == nil {
device.Release()
adapter.Release()
instance.Release()
b.Skip("no GPU backend available")
}
buf, err := device.CreateBuffer(&wgpu.BufferDescriptor{
Label: "bench-map",
Size: 1024,
Usage: wgpu.BufferUsageMapRead | wgpu.BufferUsageCopyDst,
})
if err != nil {
device.Release()
adapter.Release()
instance.Release()
b.Fatalf("CreateBuffer: %v", err)
}
return instance, adapter, device, buf
}
// BenchmarkBufferMapReadPrimary measures the primary blocking Map path.
// Target: 0 allocs/op after warm-up.
func BenchmarkBufferMapReadPrimary(b *testing.B) {
instance, adapter, device, buf := setupMapBench(b)
defer instance.Release()
defer adapter.Release()
defer device.Release()
defer buf.Release()
const size = 1024
ctx := context.Background()
// Warm up the lazy state machine and the sync.Pool entries so the
// measurement reflects steady-state cost, not first-call init.
_ = buf.Map(ctx, wgpu.MapModeRead, 0, size)
rng, _ := buf.MappedRange(0, size)
_ = rng.Bytes()
_ = buf.Unmap()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := buf.Map(ctx, wgpu.MapModeRead, 0, size); err != nil {
b.Fatal(err)
}
rng, _ := buf.MappedRange(0, size)
_ = rng.Bytes()
rng.Release()
if err := buf.Unmap(); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkBufferMapAsyncEscapeHatch measures the MapAsync + Status path
// that game loops would use. Target: 0 allocs/op after warm-up thanks to
// MapPending's sync.Pool.
func BenchmarkBufferMapAsyncEscapeHatch(b *testing.B) {
instance, adapter, device, buf := setupMapBench(b)
defer instance.Release()
defer adapter.Release()
defer device.Release()
defer buf.Release()
const size = 1024
// Warm up.
pending, _ := buf.MapAsync(wgpu.MapModeRead, 0, size)
device.Poll(wgpu.PollWait)
_, _ = pending.Status()
rng, _ := buf.MappedRange(0, size)
_ = rng.Bytes()
_ = buf.Unmap()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
pending, err := buf.MapAsync(wgpu.MapModeRead, 0, size)
if err != nil {
b.Fatal(err)
}
device.Poll(wgpu.PollPoll)
if ready, werr := pending.Status(); !ready || werr != nil {
b.Fatalf("Status: ready=%v err=%v", ready, werr)
}
pending.Release()
rng, _ := buf.MappedRange(0, size)
_ = rng.Bytes()
rng.Release()
if err := buf.Unmap(); err != nil {
b.Fatal(err)
}
}
}