1
0
mirror of https://github.com/ossrs/srs.git synced 2026-10-24 09:59:20 +03:00
Files
srs/cmd/perf/rtmp.go
Winlin 7b737e64df Perf: Load and profile tools, and the first SRS and Go proxy fixes. v8.0.63 (#4785)
SRS gets performance tools that load and profile SRS in Docker for every
protocol: a lean load tool, an internal skill that runs an improvement
round, and a measurement method that holds up to a 10% verdict. The same
tools then measure the Go proxy. The first runs found one SRS fix and
three Go proxy fixes.

The load tool, a standalone Go module in cmd/perf (bin/srs-perf):

- Per-core worker processes shard the clients and report counters each
second. Lean clients for every protocol: RTMP publish and play,
HTTP-FLV, HTTP-TS, HLS, DASH, RTSP, SRT, WHIP and WHEP. No decode;
frames and lag come from timestamps.
- 17 scenarios: each protocol's fan-out, publishers (rtmp-pub,
whip-pub), many-streams, cross-protocol bridges (srt-rtmp, srt-whep,
whip-rtmp, whip-flv), and delay probes for the RTMP pause, forward and
edge.
- run ramps by doubling to a capacity, then holds at 50% and 90% of it.
compare checks that the setups match and reports capacity and CPU per
client at the 10% line.
- make perf-image builds an optimized SRS with symbols and frame
pointers, with perf, FlameGraph and FFmpeg. SRS runs on one pinned core
and is configured only by env files, one per scenario.

The measurement method:

- A flat CBR source, checked to ±3% per second, since SRS CPU follows
the bitrate.
- SRS CPU from its cgroup once a second. Holds have no profiler in the
measuring window; each count gets its own separate profile hold.
- perf-ab.sh builds two revisions and runs them side by side on separate
cpusets. compare gives the 95% CI of the paired per-second difference.
- perf-verify.sh checks every run: capacity, healthy steps, profiles and
symbols, noise, and what bound the run.

The internal-perf-for-srs skill:

- SKILL.md holds the rules, the shared tools and the round workflow. A
Target Router loads references/cpp-server.md for the C++ media server or
references/go-server.md for the Go proxy.
- Each reference covers its tools, topology, scenarios, measuring, how
to read its profiles (Linux perf, or Go pprof and trace), A/B, the
verified scenarios with their capacity and top bottleneck, and how to
start a round.
- srs-develop routes performance, benchmark, load test and profile tasks
to it. perf-skill-test.sh checks the text against the scenario registry.
- Reports are git-ignored, since their numbers hold only for the machine
that made them.
- The skills now name each server by its job: the C++ media server and
the Go proxy, with no "generation".

C++ media server, one core, Linux arm64 in Docker:

- Fix: an empty SrsSharedPtr holds no reference count, so each fan-out
copy of a message does one malloc instead of three. Covered by
srs_utest_ai05.cpp.
- Capacity: rtmp-rtmp, rtmp-flv, rtmp-hls, whip-rtmp and whip-flv
12,800; rtmp-dash and srt-rtmp 6,400; rtmp-ts and rtmp-rtsp 3,200;
whip-whep 2,000 (cap); rtmp-whep, srt-whep, srt-srt and rtmp-pub 1,280;
whip-pub 1,000 (cap); many-streams 640 streams.
- Top bottlenecks found, not fixed: the RTMP fan-out copy,
_st_epoll_dispatch with EAGAIN reads, 4 KB reads and writes for HLS and
DASH segments, one write per message for HTTP-TS and two per packet for
RTSP, the SRTP replay window in libsrtp, and libsrt for SRT.
- Delays: the paused RTMP player's 500 ms sleep, and the forward's and
edge publish's 500 ms receive timeout, each adding about 220 ms of lag.

The Go proxy, cmd/proxy, on one core in front of 1 to 4 SRS origins:

- perf-up.sh -target proxy, run -target proxy, which samples the proxy
and each origin, Go profiles in each profile hold (CPU, allocations,
mutex, block, trace, GC), and proxy A/B.
- Fix: HTTP-FLV, HTTP-TS and HLS relay the origin's error status,
headers and body, rather than a 500 with the origin's address.
- Fix: a WebRTC STUN binding routes by its ufrag, not a cached client
address. WHIP and WHEP failed from 160 clients on three origins.
- Capacity on three origins: rtmp-flv and rtmp-hls 12,800; rtmp-rtmp and
rtmp-ts 6,400; whip-whep 2,000 (cap); srt-srt and rtmp-pub 1,280;
whip-pub 1,000 (cap); many-streams 1,280 streams.

---------

Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 18:46:30 -04:00

260 lines
6.2 KiB
Go

package main
import (
"encoding/binary"
"errors"
"io"
"sync"
)
type rtmpMessage struct {
timestamp uint32
typeID byte
streamID uint32
payload []byte
}
func put24(b []byte, v uint32) { b[0], b[1], b[2] = byte(v>>16), byte(v>>8), byte(v) }
func get24(b []byte) uint32 { return uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2]) }
type rtmpWriter struct {
w io.Writer
chunkSize int
mu sync.Mutex
}
func newRTMPWriter(w io.Writer) *rtmpWriter { return &rtmpWriter{w: w, chunkSize: 128} }
func (w *rtmpWriter) write(csid int, m rtmpMessage) error {
if csid < 2 || csid > 63 || w.chunkSize <= 0 || len(m.payload) > 0xffffff {
return errors.New("invalid RTMP chunk")
}
w.mu.Lock()
defer w.mu.Unlock()
var h [16]byte
h[0] = byte(csid)
extended := m.timestamp >= 0xffffff
if extended {
put24(h[1:4], 0xffffff)
} else {
put24(h[1:4], m.timestamp)
}
put24(h[4:7], uint32(len(m.payload)))
h[7] = m.typeID
binary.LittleEndian.PutUint32(h[8:12], m.streamID)
n := 12
if extended {
binary.BigEndian.PutUint32(h[12:16], m.timestamp)
n = 16
}
if _, err := w.w.Write(h[:n]); err != nil {
return err
}
for pos := 0; pos < len(m.payload); {
end := pos + w.chunkSize
if end > len(m.payload) {
end = len(m.payload)
}
if _, err := w.w.Write(m.payload[pos:end]); err != nil {
return err
}
pos = end
if pos < len(m.payload) {
if _, err := w.w.Write([]byte{byte(0xc0 | csid)}); err != nil {
return err
}
if extended {
if _, err := w.w.Write(h[12:16]); err != nil {
return err
}
}
}
}
return nil
}
type chunkState struct {
message rtmpMessage
length int
delta uint32
extended bool
read int
}
type rtmpReader struct {
r io.Reader
chunkSize int
streams map[int]*chunkState
}
func newRTMPReader(r io.Reader) *rtmpReader {
return &rtmpReader{r: r, chunkSize: 128, streams: make(map[int]*chunkState)}
}
func (r *rtmpReader) read() (rtmpMessage, error) {
for {
var b [1]byte
if _, err := io.ReadFull(r.r, b[:]); err != nil {
return rtmpMessage{}, err
}
format, csid := int(b[0]>>6), int(b[0]&0x3f)
if csid < 2 {
var extra [2]byte
n := 1
if csid == 1 {
n = 2
}
if _, err := io.ReadFull(r.r, extra[:n]); err != nil {
return rtmpMessage{}, err
}
csid = 64 + int(extra[0])
if n == 2 {
csid += int(extra[1]) * 256
}
}
state := r.streams[csid]
if state == nil {
if format != 0 {
return rtmpMessage{}, errors.New("RTMP chunk without initial header")
}
state = &chunkState{}
r.streams[csid] = state
}
if format != 3 {
var h [11]byte
size := []int{11, 7, 3}[format]
if _, err := io.ReadFull(r.r, h[:size]); err != nil {
return rtmpMessage{}, err
}
ts := get24(h[:3])
state.extended = ts == 0xffffff
if state.extended {
var ext [4]byte
if _, err := io.ReadFull(r.r, ext[:]); err != nil {
return rtmpMessage{}, err
}
ts = binary.BigEndian.Uint32(ext[:])
}
if format == 0 {
state.message.timestamp = ts
state.message.streamID = binary.LittleEndian.Uint32(h[7:11])
} else {
state.message.timestamp += ts
state.delta = ts
}
if format < 2 {
state.length = int(get24(h[3:6]))
state.message.typeID = h[6]
}
if state.length > 16<<20 {
return rtmpMessage{}, errors.New("RTMP message too large")
}
state.read = 0
state.message.payload = make([]byte, state.length)
} else {
if state.extended {
var ext [4]byte
if _, err := io.ReadFull(r.r, ext[:]); err != nil {
return rtmpMessage{}, err
}
}
if state.read == state.length {
state.message.timestamp += state.delta
state.read = 0
state.message.payload = make([]byte, state.length)
}
}
if r.chunkSize <= 0 {
return rtmpMessage{}, errors.New("invalid RTMP chunk size")
}
count := state.length - state.read
if count > r.chunkSize {
count = r.chunkSize
}
if _, err := io.ReadFull(r.r, state.message.payload[state.read:state.read+count]); err != nil {
return rtmpMessage{}, err
}
state.read += count
if state.read == state.length {
m := state.message
if m.typeID == 1 && len(m.payload) == 4 {
size := binary.BigEndian.Uint32(m.payload)
if size == 0 || size > 1<<20 {
return rtmpMessage{}, errors.New("invalid RTMP peer chunk size")
}
r.chunkSize = int(size)
}
return m, nil
}
}
}
// readFLVHeader reads the FLV header and the first previous tag size.
func readFLVHeader(reader io.Reader) error {
var header [9]byte
if _, err := io.ReadFull(reader, header[:]); err != nil {
return err
}
if string(header[:3]) != "FLV" || binary.BigEndian.Uint32(header[5:9]) < 9 {
return errors.New("invalid FLV header")
}
_, err := io.CopyN(io.Discard, reader, int64(binary.BigEndian.Uint32(header[5:9])-9)+4)
return err
}
// readFLVTag reads one tag of any type with its previous tag size; io.EOF
// only when the stream ends between two tags.
func readFLVTag(reader io.Reader) (rtmpMessage, error) {
var h [11]byte
if _, err := io.ReadFull(reader, h[:]); err != nil {
return rtmpMessage{}, err
}
size := get24(h[1:4])
if size > 16<<20 {
return rtmpMessage{}, errors.New("FLV tag too large")
}
payload := make([]byte, size)
if _, err := io.ReadFull(reader, payload); err != nil {
return rtmpMessage{}, noEOF(err)
}
var previous [4]byte
if _, err := io.ReadFull(reader, previous[:]); err != nil {
return rtmpMessage{}, noEOF(err)
}
if binary.BigEndian.Uint32(previous[:]) != size+11 {
return rtmpMessage{}, errors.New("invalid FLV previous tag size")
}
return rtmpMessage{timestamp: get24(h[4:7]) | uint32(h[7])<<24, typeID: h[0], payload: payload}, nil
}
// noEOF turns an end inside a tag into io.ErrUnexpectedEOF.
func noEOF(err error) error {
if err == io.EOF {
return io.ErrUnexpectedEOF
}
return err
}
func readFLVTags(reader io.Reader) ([]rtmpMessage, error) {
if err := readFLVHeader(reader); err != nil {
return nil, err
}
var tags []rtmpMessage
for {
tag, err := readFLVTag(reader)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if tag.typeID == 8 || tag.typeID == 9 {
tags = append(tags, tag)
}
}
if len(tags) == 0 {
return nil, errors.New("FLV has no media tags")
}
return tags, nil
}