forked from tphakala/go-m4a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
252 lines (230 loc) · 7.14 KB
/
Copy pathbench_test.go
File metadata and controls
252 lines (230 loc) · 7.14 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: MIT
package m4a
import (
"bytes"
"errors"
"io"
"testing"
)
// benchFrameCount is the number of AAC-LC access units each mux/demux benchmark
// processes. A few thousand frames is a realistic short clip (roughly a minute
// of 48 kHz audio at 1024 samples per frame) and is large enough that per-frame
// costs dominate the one-time setup and finalize work.
const benchFrameCount = 5000
// benchFrames builds n synthetic AAC-LC access units with realistic, varied
// sizes (roughly 100..800 bytes, the range of real AAC-LC frames). The bytes are
// arbitrary; the container never decodes them.
func benchFrames(n int) [][]byte {
frames := make([][]byte, n)
for i := range frames {
size := 100 + (i*97)%701 // 100..800 bytes, varied
au := make([]byte, size)
for j := range au {
au[j] = byte(i*31 + j)
}
frames[i] = au
}
return frames
}
// benchTotalPayload sums the access-unit lengths: the mdat payload byte count
// the mux and demux benchmarks move per iteration.
func benchTotalPayload(frames [][]byte) int64 {
var total int64
for _, f := range frames {
total += int64(len(f))
}
return total
}
// buildBenchFile muxes frames into an in-memory M4A and returns the bytes.
func buildBenchFile(b *testing.B, frames [][]byte) []byte {
b.Helper()
ws := &memWS{}
w, err := NewWriter(ws, WriterConfig{SampleRate: 48000, Channels: 1, ASC: ascMono48k})
if err != nil {
b.Fatal(err)
}
for _, au := range frames {
if err := w.WriteFrame(au); err != nil {
b.Fatal(err)
}
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
return ws.buf
}
// BenchmarkWriteFrames muxes benchFrameCount frames, including Close, into an
// in-memory io.WriteSeeker. The backing buffer is reused across iterations (only
// pos is reset) so the numbers isolate the Writer's own allocations from the
// sink's growth. Per-frame cost is (reported value / benchFrameCount).
func BenchmarkWriteFrames(b *testing.B) {
frames := benchFrames(benchFrameCount)
cfg := WriterConfig{SampleRate: 48000, Channels: 1, ASC: ascMono48k}
ws := &memWS{}
// Warm-up mux grows the in-memory sink to the final file size. memWS.Write
// reallocates and full-copies whenever the buffer must grow, so without this
// the first measured iteration would charge an O(N^2) sink-growth cost to the
// numbers. Pre-growing it makes later iterations reuse the backing buffer, so
// the measured cost is the Writer's, not the test sink's. b.Loop excludes this
// pre-loop work from the timer and allocation counters.
muxOnce(b, ws, cfg, frames)
b.SetBytes(benchTotalPayload(frames))
b.ReportAllocs()
for b.Loop() {
ws.pos = 0 // reuse the backing buffer; measure only the Writer's allocs
muxOnce(b, ws, cfg, frames)
}
}
// muxOnce writes all frames to ws with a fresh Writer and closes it.
func muxOnce(b *testing.B, ws *memWS, cfg WriterConfig, frames [][]byte) {
b.Helper()
w, err := NewWriter(ws, cfg)
if err != nil {
b.Fatal(err)
}
for _, au := range frames {
if err := w.WriteFrame(au); err != nil {
b.Fatal(err)
}
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
// BenchmarkReadFrame demuxes every frame via ReadFrame. The Reader is parsed
// once; each iteration only resets the cursor, so the numbers reflect the
// steady-state per-frame read cost (one make([]byte, size) per frame is the
// documented ReadFrame contract). Per-frame cost is (value / benchFrameCount).
func BenchmarkReadFrame(b *testing.B) {
frames := benchFrames(benchFrameCount)
data := buildBenchFile(b, frames)
rd, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
b.SetBytes(benchTotalPayload(frames))
b.ReportAllocs()
for b.Loop() {
rd.resetCursor()
for {
_, rerr := rd.ReadFrame()
if errors.Is(rerr, io.EOF) {
break
}
if rerr != nil {
b.Fatal(rerr)
}
}
}
}
// BenchmarkReadFrameInto demuxes every frame via ReadFrameInto into a single
// reused buffer sized to the largest frame. The Reader is parsed once and each
// iteration only resets the cursor, so the numbers reflect the steady-state
// per-frame read cost: zero allocations, since the buffer is reused rather than
// allocated per frame (unlike ReadFrame's documented one make per frame). Per-
// frame cost is (value / benchFrameCount).
func BenchmarkReadFrameInto(b *testing.B) {
frames := benchFrames(benchFrameCount)
data := buildBenchFile(b, frames)
rd, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
// Size the reused buffer to the largest frame so ReadFrameInto never returns
// io.ErrShortBuffer in the measured loop.
var maxSize int
for _, f := range frames {
if len(f) > maxSize {
maxSize = len(f)
}
}
dst := make([]byte, maxSize)
b.SetBytes(benchTotalPayload(frames))
b.ReportAllocs()
for b.Loop() {
rd.resetCursor()
for {
_, rerr := rd.ReadFrameInto(dst)
if errors.Is(rerr, io.EOF) {
break
}
if rerr != nil {
b.Fatal(rerr)
}
}
}
}
// BenchmarkRawStream drives the primary go-aac decode feed: io.Copy from a
// RawStream over the built file into io.Discard. The rawReader is reused with a
// pre-warmed scratch buffer so the numbers show the steady-state per-frame cost,
// which is expected to be zero allocations after warm-up.
func BenchmarkRawStream(b *testing.B) {
frames := benchFrames(benchFrameCount)
data := buildBenchFile(b, frames)
framed := benchTotalPayload(frames) + 2*int64(len(frames)) // + 2-byte length prefixes
rd, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
rr := &rawReader{rd: rd}
// Warm the scratch buffer to the largest frame so its one-time growth is not
// charged to the measured loop.
rd.resetCursor()
if _, err := io.Copy(io.Discard, rr); err != nil {
b.Fatal(err)
}
b.SetBytes(framed)
b.ReportAllocs()
for b.Loop() {
rd.resetCursor()
rr.buf = nil
rr.err = nil
if _, err := io.Copy(io.Discard, rr); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkRawStreamFresh is the realistic-usage variant: a fresh RawStream per
// iteration (as aacm4a.NewDecoder does once per file). It charges the one-time
// scratch growth to every iteration, so allocs/op reflect that growth amortized
// over all frames rather than the true per-frame cost.
func BenchmarkRawStreamFresh(b *testing.B) {
frames := benchFrames(benchFrameCount)
data := buildBenchFile(b, frames)
framed := benchTotalPayload(frames) + 2*int64(len(frames))
rd, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
b.SetBytes(framed)
b.ReportAllocs()
for b.Loop() {
rd.resetCursor()
if _, err := io.Copy(io.Discard, rd.RawStream()); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkBuildMoov measures Close's moov assembly for a large frame count,
// exercising the AppendContainer re-copies that carry the sample table up the
// box tree (stbl -> minf -> mdia -> trak -> moov).
func BenchmarkBuildMoov(b *testing.B) {
frames := benchFrames(benchFrameCount)
sizes := make([]uint32, len(frames))
for i, f := range frames {
sizes[i] = uint32(len(f))
}
w := &Writer{
trackMeta: trackMeta{
sampleRate: 48000,
channels: 1,
asc: ascMono48k,
},
payloadStart: 100,
sizes: sizes,
}
b.ReportAllocs()
for b.Loop() {
_ = w.buildMoov()
}
}