Repository navigation
Expand file tree
/
Copy pathworkload_test.go
More file actions
213 lines (205 loc) · 7.31 KB
/
Copy pathworkload_test.go
File metadata and controls
213 lines (205 loc) · 7.31 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
package libcodec_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/thesyncim/goav"
"github.com/thesyncim/goav/av"
"github.com/thesyncim/goav/bundle"
"github.com/thesyncim/goav/codec"
"github.com/thesyncim/goav/component"
"github.com/thesyncim/goav/pipeline"
)
// TestFileLadderWorkload consumes independently encoded clips, including
// audio and delayed H.264 pictures. The opt-in driver records the input
// hashes, encoder settings, output quality, CPU and RSS alongside this log.
func TestFileLadderWorkload(t *testing.T) {
clips := os.Getenv("GOAV_WORKLOAD_CLIPS")
if clips == "" {
t.Skip("set GOAV_WORKLOAD_CLIPS or run scripts/bench/workloads.sh")
}
backend, option := libH264(t)
for _, size := range []string{"720p", "1080p"} {
t.Run(size, func(t *testing.T) {
data, err := os.ReadFile(filepath.Join(clips, size+".mp4"))
if err != nil {
t.Fatal(err)
}
inputVideo, inputAudio := demuxed(t, data)
if len(inputVideo) < 30 || len(inputAudio) == 0 {
t.Fatal("workload needs video GOPs and simultaneous audio")
}
in := goav.FileInput(size+".mp4", bytes.NewReader(data))
meter := &ladderMeter{starts: make(map[time.Duration]ladderStart), latencies: make(map[string]*ladderLatencies)}
inputMeter := component.PacketFunc("packet-latency-start", func(_ context.Context, packet *av.Packet, emit component.Emit) error {
pts, _ := packet.PTS.ToDuration()
meter.mu.Lock()
meter.starts[pts] = ladderStart{input: time.Now()}
meter.mu.Unlock()
return emit.Packet(packet)
})
video := in.Video().Do(inputMeter).Decode(codec.Backend(backend)).Do(meter)
rt := bundle.MustNew(option)
var hd, sd bytes.Buffer
hdOut := goav.Write("hd.mp4", &hd, goav.Format(av.FormatMP4))
sdOut := goav.Write("sd.mp4", &sd, goav.Format(av.FormatMP4))
rungs := []struct {
name string
width, height, bitrate int
destination goav.Destination
data *bytes.Buffer
}{
{"hd", 1280, 720, 2_000_000, hdOut, &hd},
{"sd", 640, 360, 800_000, sdOut, &sd},
}
var outputs []goav.Output
for _, rung := range rungs {
meter.latencies[rung.name] = &ladderLatencies{}
encoded := video.Resize(rung.width, rung.height).Encode(codec.H264(codec.Backend(backend), codec.Bitrate(rung.bitrate), codec.KeyframeInterval(30)))
outputs = append(outputs, encoded.To(rung.destination), in.Audio().To(rung.destination), encoded.To(goav.Sink(&ladderLatencySink{name: rung.name, meter: meter})))
}
before := runtime.NumGoroutine()
started := time.Now()
task, err := rt.Build(t.Context(), outputs...)
if err != nil {
t.Fatal(err)
}
defer task.Close()
decoders := 0
for _, node := range task.Spec().Nodes {
if strings.Contains(node.Name, "decode") {
decoders++
}
}
if decoders != 1 {
t.Fatalf("ladder has %d decoders, want shared decode", decoders)
}
if err := task.Run(t.Context()); err != nil {
t.Fatal(err)
}
if err := task.Close(); err != nil {
t.Fatal(err)
}
elapsed := time.Since(started)
if meter.frames != len(inputVideo) {
t.Fatalf("decoded %d frames for %d input packets", meter.frames, len(inputVideo))
}
if stats := task.Stats(); stats.PendingCleanup != 0 || stats.Dropped != 0 {
t.Fatalf("unclean workload: %+v", stats)
}
var drift time.Duration
for _, rung := range rungs {
v, a := demuxed(t, rung.data.Bytes())
if len(v) != len(inputVideo) || len(a) != len(inputAudio) {
t.Fatalf("%s: video=%d audio=%d, want %d/%d", rung.name, len(v), len(a), len(inputVideo), len(inputAudio))
}
endVideo, _ := v[len(v)-1].PTS.ToDuration()
endAudio, _ := a[len(a)-1].PTS.ToDuration()
delta := endVideo - endAudio
if delta < 0 {
delta = -delta
}
drift = max(drift, delta)
if delta > 100*time.Millisecond {
t.Fatalf("%s A/V tail drift %s", rung.name, delta)
}
if outputDir := os.Getenv("GOAV_WORKLOAD_OUTPUT"); outputDir != "" {
if err := os.WriteFile(filepath.Join(outputDir, size+"-"+rung.name+".mp4"), rung.data.Bytes(), 0600); err != nil {
t.Fatal(err)
}
}
}
if len(meter.starts) != 0 {
t.Fatalf("%d inputs have incomplete latency measurements", len(meter.starts))
}
var renditions []map[string]any
for _, rung := range rungs {
measure := meter.latencies[rung.name]
if len(measure.input) != len(inputVideo) {
t.Fatalf("%s latency samples=%d", rung.name, len(measure.input))
}
slices.Sort(measure.input)
slices.Sort(measure.decoded)
q := func(samples []time.Duration, p int) int64 { return samples[(len(samples)-1)*p/100].Nanoseconds() }
renditions = append(renditions, map[string]any{
"rendition": rung.name, "samples": len(measure.input),
"packet_input_to_encoded_p50_ns": q(measure.input, 50), "packet_input_to_encoded_p95_ns": q(measure.input, 95), "packet_input_to_encoded_p99_ns": q(measure.input, 99),
"post_decode_to_encoded_p50_ns": q(measure.decoded, 50), "post_decode_to_encoded_p95_ns": q(measure.decoded, 95), "post_decode_to_encoded_p99_ns": q(measure.decoded, 99),
})
}
var memory runtime.MemStats
runtime.ReadMemStats(&memory)
report, _ := json.Marshal(map[string]any{
"workload": "file_ladder", "size": size, "backend": backend,
"frames": meter.frames, "elapsed_ns": elapsed.Nanoseconds(), "input_fps": float64(meter.frames) / elapsed.Seconds(),
"renditions": renditions,
"av_tail_drift_ns": drift.Nanoseconds(), "goroutines_before": before, "goroutines_after": runtime.NumGoroutine(),
"heap_alloc_bytes": memory.HeapAlloc, "hd_bitrate": 2_000_000, "sd_bitrate": 800_000,
})
t.Log(string(report))
})
}
}
type ladderMeter struct {
mu sync.Mutex
starts map[time.Duration]ladderStart
frames int
latencies map[string]*ladderLatencies
}
type ladderStart struct {
input, decoded time.Time
outputs int
}
type ladderLatencies struct{ input, decoded []time.Duration }
func (*ladderMeter) Name() string { return "latency-start" }
func (m *ladderMeter) Handle(ctx context.Context, msg *pipeline.Message, emitter pipeline.Emitter) error {
if msg.Frame != nil {
pts, _ := msg.Frame.PTS.ToDuration()
m.mu.Lock()
start := m.starts[pts]
start.decoded = time.Now()
m.starts[pts] = start
m.frames++
m.mu.Unlock()
}
return emitter.Emit(ctx, msg)
}
func (*ladderMeter) Close() error { return nil }
type ladderLatencySink struct {
name string
meter *ladderMeter
}
func (s *ladderLatencySink) Name() string { return "latency-" + s.name }
func (s *ladderLatencySink) Handle(_ context.Context, msg *pipeline.Message) error {
if msg.Packet == nil {
return nil
}
pts, _ := msg.Packet.PTS.ToDuration()
s.meter.mu.Lock()
defer s.meter.mu.Unlock()
start, ok := s.meter.starts[pts]
if !ok || start.input.IsZero() || start.decoded.IsZero() {
return fmt.Errorf("encoded PTS %s lacks an input or decoded frame", pts)
}
now := time.Now()
measure := s.meter.latencies[s.name]
measure.input = append(measure.input, now.Sub(start.input))
measure.decoded = append(measure.decoded, now.Sub(start.decoded))
start.outputs++
if start.outputs == len(s.meter.latencies) {
delete(s.meter.starts, pts)
} else {
s.meter.starts[pts] = start
}
return nil
}
func (*ladderLatencySink) Close() error { return nil }