mirror of
https://github.com/ossrs/srs.git
synced 2026-10-24 09:59:20 +03:00
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>
175 lines
4.8 KiB
Go
175 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
)
|
|
|
|
// difference is new minus old with its two-sided 95% confidence interval.
|
|
// OldMean is the old side's mean over the samples used, for the relative change.
|
|
type difference struct {
|
|
Method string
|
|
N int
|
|
DF float64
|
|
Mean float64
|
|
OldMean float64
|
|
Low float64
|
|
High float64
|
|
}
|
|
|
|
func (d difference) containsZero() bool {
|
|
return d.Low <= 0 && d.High >= 0
|
|
}
|
|
|
|
func meanAndVariance(values []float64) (float64, float64) {
|
|
s := statOf(values)
|
|
return s.Mean, s.SD * s.SD
|
|
}
|
|
|
|
// pairedDifference is the mean of the per-second differences new[i] - old[i],
|
|
// for an A/B run whose two sides were sampled in the same seconds, so the noise
|
|
// both servers share cancels.
|
|
func pairedDifference(old, newer []float64) (difference, error) {
|
|
if len(old) != len(newer) {
|
|
return difference{}, errors.New("paired series differ in length")
|
|
}
|
|
if len(old) < 2 {
|
|
return difference{}, errors.New("a difference needs at least 2 samples")
|
|
}
|
|
diffs := make([]float64, len(old))
|
|
for i := range old {
|
|
diffs[i] = newer[i] - old[i]
|
|
}
|
|
mean, variance := meanAndVariance(diffs)
|
|
oldMean, _ := meanAndVariance(old)
|
|
n := float64(len(diffs))
|
|
half := studentT975(n-1) * math.Sqrt(variance/n)
|
|
return difference{Method: "paired", N: len(diffs), DF: n - 1, Mean: mean, OldMean: oldMean, Low: mean - half, High: mean + half}, nil
|
|
}
|
|
|
|
// welchDifference is the difference of the means of two windows measured at
|
|
// different times, with Welch's degrees of freedom, which do not assume equal
|
|
// variances.
|
|
func welchDifference(old, newer []float64) (difference, error) {
|
|
if len(old) < 2 || len(newer) < 2 {
|
|
return difference{}, errors.New("a difference needs at least 2 samples on each side")
|
|
}
|
|
oldMean, oldVar := meanAndVariance(old)
|
|
newMean, newVar := meanAndVariance(newer)
|
|
a, b := oldVar/float64(len(old)), newVar/float64(len(newer))
|
|
mean := newMean - oldMean
|
|
se := math.Sqrt(a + b)
|
|
df := float64(len(old) + len(newer) - 2)
|
|
if a+b > 0 {
|
|
df = (a + b) * (a + b) / (a*a/float64(len(old)-1) + b*b/float64(len(newer)-1))
|
|
}
|
|
half := studentT975(df) * se
|
|
return difference{Method: "unpaired", N: len(old) + len(newer), DF: df, Mean: mean, OldMean: oldMean, Low: mean - half, High: mean + half}, nil
|
|
}
|
|
|
|
// pairBySecond pairs the SRS CPU of two A/B sides by wall-clock time: each old
|
|
// sample takes the nearest unused new sample within half a second. Each side
|
|
// samples on its own one-second ticker, so their seconds are offset by less
|
|
// than a second; a second with no partner is left out.
|
|
func pairBySecond(old, newer []sample) ([]float64, []float64) {
|
|
var a, b []float64
|
|
used := make([]bool, len(newer))
|
|
for _, o := range old {
|
|
best := -1
|
|
var bestGap int64
|
|
for i, n := range newer {
|
|
gap := n.UnixMillis - o.UnixMillis
|
|
if gap < 0 {
|
|
gap = -gap
|
|
}
|
|
if used[i] || gap >= 500 || (best >= 0 && gap >= bestGap) {
|
|
continue
|
|
}
|
|
best, bestGap = i, gap
|
|
}
|
|
if best < 0 {
|
|
continue
|
|
}
|
|
used[best] = true
|
|
a = append(a, o.SRSCPUPercent)
|
|
b = append(b, newer[best].SRSCPUPercent)
|
|
}
|
|
return a, b
|
|
}
|
|
|
|
// studentT975 is the 0.975 quantile of Student's t with df degrees of freedom,
|
|
// the half-width factor of a two-sided 95% CI. df need not be an integer, for
|
|
// Welch's df.
|
|
func studentT975(df float64) float64 {
|
|
low, high := 0.0, 1.0
|
|
for studentTCDF(high, df) < 0.975 {
|
|
high *= 2
|
|
}
|
|
for i := 0; i < 200; i++ {
|
|
mid := (low + high) / 2
|
|
if studentTCDF(mid, df) < 0.975 {
|
|
low = mid
|
|
} else {
|
|
high = mid
|
|
}
|
|
}
|
|
return (low + high) / 2
|
|
}
|
|
|
|
// studentTCDF is P(T <= t) for t >= 0: 1 - I_x(df/2, 1/2) / 2, x = df/(df+t^2).
|
|
func studentTCDF(t, df float64) float64 {
|
|
return 1 - regularizedBeta(df/(df+t*t), df/2, 0.5)/2
|
|
}
|
|
|
|
// regularizedBeta is I_x(a, b), by the continued fraction of Numerical Recipes'
|
|
// betai, using the symmetry I_x(a, b) = 1 - I_{1-x}(b, a) where it converges
|
|
// faster.
|
|
func regularizedBeta(x, a, b float64) float64 {
|
|
if x <= 0 {
|
|
return 0
|
|
}
|
|
if x >= 1 {
|
|
return 1
|
|
}
|
|
la, _ := math.Lgamma(a + b)
|
|
lb, _ := math.Lgamma(a)
|
|
lc, _ := math.Lgamma(b)
|
|
front := math.Exp(la - lb - lc + a*math.Log(x) + b*math.Log(1-x))
|
|
if x < (a+1)/(a+b+2) {
|
|
return front * betaFraction(x, a, b) / a
|
|
}
|
|
return 1 - front*betaFraction(1-x, b, a)/b
|
|
}
|
|
|
|
// betaFraction evaluates the continued fraction by the modified Lentz method.
|
|
func betaFraction(x, a, b float64) float64 {
|
|
const tiny = 1e-300
|
|
c, d := 1.0, 1-(a+b)*x/(a+1)
|
|
if math.Abs(d) < tiny {
|
|
d = tiny
|
|
}
|
|
d = 1 / d
|
|
h := d
|
|
for m := 1; m <= 1000; m++ {
|
|
fm := float64(m)
|
|
aa := fm * (b - fm) * x / ((a + 2*fm - 1) * (a + 2*fm))
|
|
for i := 0; i < 2; i++ {
|
|
d = 1 + aa*d
|
|
if math.Abs(d) < tiny {
|
|
d = tiny
|
|
}
|
|
c = 1 + aa/c
|
|
if math.Abs(c) < tiny {
|
|
c = tiny
|
|
}
|
|
d = 1 / d
|
|
h *= d * c
|
|
aa = -(a + fm) * (a + b + fm) * x / ((a + 2*fm) * (a + 2*fm + 1))
|
|
}
|
|
if math.Abs(d*c-1) < 1e-15 {
|
|
break
|
|
}
|
|
}
|
|
return h
|
|
}
|