mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
SmartPtr: Support load test for source by srs-bench. v6.0.130 (#4097)
1. Add live benchmark support in srs-bench, which only connects and disconnects without any media transport, to test source creation and disposal and verify source memory leaks. 2. SmartPtr: Support cleanup of HTTP-FLV stream. Unregister the HTTP-FLV handler for the pattern and clean up the objects and resources. 3. Support benchmarking RTMP/SRT with srs-bench by integrating the gosrt and oryx RTMP libraries. 4. Refine SRT and RTC sources by using a timer to clean up the sources, following the same strategy as the Live source. --------- Co-authored-by: Haibo Chen <495810242@qq.com> Co-authored-by: Jacob Su <suzp1984@gmail.com>
This commit is contained in:
parent
e3d74fb045
commit
1f9309ae25
508 changed files with 6805 additions and 3299 deletions
195
trunk/3rdparty/srs-bench/live/live.go
vendored
Normal file
195
trunk/3rdparty/srs-bench/live/live.go
vendored
Normal file
|
@ -0,0 +1,195 @@
|
|||
// The MIT License (MIT)
|
||||
//
|
||||
// # Copyright (c) 2021 Winlin
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
package live
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ossrs/go-oryx-lib/errors"
|
||||
"github.com/ossrs/go-oryx-lib/logger"
|
||||
)
|
||||
|
||||
var closeAfterPublished bool
|
||||
|
||||
var pr string
|
||||
|
||||
var streams, delay int
|
||||
|
||||
var statListen string
|
||||
|
||||
func Parse(ctx context.Context) {
|
||||
fl := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||
|
||||
var sfu string
|
||||
fl.StringVar(&sfu, "sfu", "srs", "The SFU server, srs or gb28181 or janus")
|
||||
|
||||
fl.BoolVar(&closeAfterPublished, "cap", false, "")
|
||||
|
||||
fl.StringVar(&pr, "pr", "", "")
|
||||
|
||||
fl.IntVar(&streams, "sn", 1, "")
|
||||
fl.IntVar(&delay, "delay", 10, "")
|
||||
|
||||
fl.StringVar(&statListen, "stat", "", "")
|
||||
|
||||
fl.Usage = func() {
|
||||
fmt.Println(fmt.Sprintf("Usage: %v [Options]", os.Args[0]))
|
||||
fmt.Println(fmt.Sprintf("Options:"))
|
||||
fmt.Println(fmt.Sprintf(" -sfu The target server that can be rtc, live, janus, or gb28181. Default: rtc"))
|
||||
fmt.Println(fmt.Sprintf(" rtc/srs: SRS WebRTC SFU server, for WebRTC/WHIP/WHEP."))
|
||||
fmt.Println(fmt.Sprintf(" live: SRS live streaming server, for RTMP/HTTP-FLV/HLS."))
|
||||
fmt.Println(fmt.Sprintf(" janus: Janus WebRTC SFU server, for janus private protocol."))
|
||||
fmt.Println(fmt.Sprintf(" -sn The number of streams to simulate. Variable: %%d. Default: 1"))
|
||||
fmt.Println(fmt.Sprintf(" -delay The start delay in ms for each client or stream to simulate. Default: 50"))
|
||||
fmt.Println(fmt.Sprintf(" -stat [Optional] The stat server API listen port."))
|
||||
fmt.Println(fmt.Sprintf("Publisher:"))
|
||||
fmt.Println(fmt.Sprintf(" -pr The url to publish. If sn exceed 1, auto append variable %%d."))
|
||||
fmt.Println(fmt.Sprintf(" -cap Whether to close connection after publish. Default: false"))
|
||||
fmt.Println(fmt.Sprintf("\n例如,1个推流,无媒体传输:"))
|
||||
fmt.Println(fmt.Sprintf(" %v -pr=rtmp://localhost/live/livestream -cap=true", os.Args[0]))
|
||||
fmt.Println(fmt.Sprintf("\n例如,2个推流,无媒体传输:"))
|
||||
fmt.Println(fmt.Sprintf(" %v -pr=rtmp://localhost/live/livestream_%%d -sn=2 -cap=true", os.Args[0]))
|
||||
fmt.Println()
|
||||
}
|
||||
_ = fl.Parse(os.Args[1:])
|
||||
|
||||
showHelp := streams <= 0
|
||||
if pr == "" {
|
||||
showHelp = true
|
||||
}
|
||||
if showHelp {
|
||||
fl.Usage()
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
if statListen != "" && !strings.Contains(statListen, ":") {
|
||||
statListen = ":" + statListen
|
||||
}
|
||||
|
||||
summaryDesc := fmt.Sprintf("streams=%v", streams)
|
||||
if pr != "" {
|
||||
summaryDesc = fmt.Sprintf("%v, publish=(url=%v,cap=%v)",
|
||||
summaryDesc, pr, closeAfterPublished)
|
||||
}
|
||||
logger.Tf(ctx, "Run benchmark with %v", summaryDesc)
|
||||
}
|
||||
|
||||
func Run(ctx context.Context) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// Run tasks.
|
||||
var wg sync.WaitGroup
|
||||
defer wg.Wait()
|
||||
|
||||
// Run STAT API server.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
if statListen == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var lc net.ListenConfig
|
||||
ln, err := lc.Listen(ctx, "tcp", statListen)
|
||||
if err != nil {
|
||||
logger.Ef(ctx, "stat listen err+%v", err)
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
handleStat(ctx, mux, statListen)
|
||||
|
||||
srv := &http.Server{
|
||||
Handler: mux,
|
||||
BaseContext: func(listener net.Listener) context.Context {
|
||||
return ctx
|
||||
},
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
srv.Shutdown(ctx)
|
||||
}()
|
||||
|
||||
logger.Tf(ctx, "Stat listen at %v", statListen)
|
||||
if err := srv.Serve(ln); err != nil {
|
||||
if ctx.Err() == nil {
|
||||
logger.Ef(ctx, "stat serve err+%v", err)
|
||||
cancel()
|
||||
}
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// Run all publishers.
|
||||
publisherStartedCtx, publisherStartedCancel := context.WithCancel(ctx)
|
||||
defer publisherStartedCancel()
|
||||
for i := 0; pr != "" && i < streams && ctx.Err() == nil; i++ {
|
||||
r_auto := pr
|
||||
if streams > 1 && !strings.Contains(r_auto, "%") {
|
||||
r_auto += "%d"
|
||||
}
|
||||
|
||||
r2 := r_auto
|
||||
if strings.Contains(r2, "%") {
|
||||
r2 = fmt.Sprintf(r2, i)
|
||||
}
|
||||
|
||||
gStatLive.Publishers.Expect++
|
||||
gStatLive.Publishers.Alive++
|
||||
|
||||
wg.Add(1)
|
||||
go func(pr string) {
|
||||
defer wg.Done()
|
||||
defer func() {
|
||||
gStatLive.Publishers.Alive--
|
||||
logger.Tf(ctx, "Publisher %v done, alive=%v", pr, gStatLive.Publishers.Alive)
|
||||
|
||||
<- publisherStartedCtx.Done()
|
||||
if gStatLive.Publishers.Alive == 0 {
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := startPublish(ctx, pr, closeAfterPublished); err != nil {
|
||||
if errors.Cause(err) != context.Canceled {
|
||||
logger.Wf(ctx, "Run err %+v", err)
|
||||
}
|
||||
}
|
||||
}(r2)
|
||||
|
||||
if delay > 0 {
|
||||
time.Sleep(time.Duration(delay) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
210
trunk/3rdparty/srs-bench/live/publisher.go
vendored
Normal file
210
trunk/3rdparty/srs-bench/live/publisher.go
vendored
Normal file
|
@ -0,0 +1,210 @@
|
|||
// The MIT License (MIT)
|
||||
//
|
||||
// # Copyright (c) 2021 Winlin
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
package live
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/haivision/srtgo"
|
||||
"github.com/ossrs/go-oryx-lib/amf0"
|
||||
"github.com/ossrs/go-oryx-lib/errors"
|
||||
"github.com/ossrs/go-oryx-lib/logger"
|
||||
"github.com/ossrs/go-oryx-lib/rtmp"
|
||||
)
|
||||
|
||||
func startPublish(ctx context.Context, r string, closeAfterPublished bool) error {
|
||||
ctx = logger.WithContext(ctx)
|
||||
logger.Tf(ctx, "Run publish url=%v, cap=%v", r, closeAfterPublished)
|
||||
|
||||
u, err := url.Parse(r)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "parse %v", r)
|
||||
}
|
||||
|
||||
if u.Scheme == "rtmp" {
|
||||
return startPublishRTMP(ctx, u, closeAfterPublished)
|
||||
} else if u.Scheme == "srt" {
|
||||
return startPublishSRT(ctx, u, closeAfterPublished)
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid schema %v of %v", u.Scheme, r)
|
||||
}
|
||||
|
||||
func startPublishSRT(ctx context.Context, u *url.URL, closeAfterPublished bool) (err error) {
|
||||
// Parse host and port.
|
||||
port := 1935
|
||||
if u.Port() != "" {
|
||||
if port, err = strconv.Atoi(u.Port()); err != nil {
|
||||
return errors.Wrapf(err, "parse port %v", u.Port())
|
||||
}
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(u.Hostname())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "lookup %v", u.Hostname())
|
||||
}
|
||||
if len(ips) == 0 {
|
||||
return errors.Errorf("no ips for %v", u.Hostname())
|
||||
}
|
||||
logger.Tf(ctx, "Parse url %v to host=%v, ip=%v, port=%v",
|
||||
u.String(), u.Hostname(), ips[0], port)
|
||||
|
||||
// Setup libsrt.
|
||||
client := srtgo.NewSrtSocket(ips[0].To4().String(), uint16(port),
|
||||
map[string]string{
|
||||
"transtype": "live",
|
||||
"tsbpdmode": "false",
|
||||
"tlpktdrop": "false",
|
||||
"latency": "0",
|
||||
"streamid": fmt.Sprintf("#%v", u.Fragment),
|
||||
},
|
||||
)
|
||||
defer client.Close()
|
||||
|
||||
if err := client.Connect(); err != nil {
|
||||
return errors.Wrapf(err, "SRT connect to %v:%v", u.Hostname(), port)
|
||||
}
|
||||
logger.Tf(ctx, "Connect to SRT server %v:%v success", u.Hostname(), port)
|
||||
|
||||
// We should wait for a while after connected to SRT server before quit. Because SRT server use timeout
|
||||
// to detect UDP connection status, so we should never reconnect very fast.
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(3 * time.Second):
|
||||
logger.Tf(ctx, "SRT publish stream success, stream=%v", u.Fragment)
|
||||
}
|
||||
|
||||
if closeAfterPublished {
|
||||
logger.Tf(ctx, "Close connection after published")
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func startPublishRTMP(ctx context.Context, u *url.URL, closeAfterPublished bool) (err error) {
|
||||
parts := strings.Split(u.Path, "/")
|
||||
if len(parts) == 0 {
|
||||
return errors.Errorf("invalid path %v", u.Path)
|
||||
}
|
||||
app, stream := strings.Join(parts[:len(parts)-1], "/"), parts[len(parts)-1]
|
||||
|
||||
// Parse host and port.
|
||||
port := 1935
|
||||
if u.Port() != "" {
|
||||
if port, err = strconv.Atoi(u.Port()); err != nil {
|
||||
return errors.Wrapf(err, "parse port %v", u.Port())
|
||||
}
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(u.Hostname())
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "lookup %v", u.Hostname())
|
||||
}
|
||||
if len(ips) == 0 {
|
||||
return errors.Errorf("no ips for %v", u.Hostname())
|
||||
}
|
||||
logger.Tf(ctx, "Parse url %v to host=%v, ip=%v, port=%v, app=%v, stream=%v",
|
||||
u.String(), u.Hostname(), ips[0], port, app, stream)
|
||||
|
||||
// Connect via TCP client.
|
||||
c, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ips[0], Port: port})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "dial %v %v", u.Hostname(), u.Port())
|
||||
}
|
||||
defer c.Close()
|
||||
logger.Tf(ctx, "Connect to RTMP server %v:%v success", u.Hostname(), port)
|
||||
|
||||
// RTMP Handshake.
|
||||
rd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
hs := rtmp.NewHandshake(rd)
|
||||
|
||||
if err := hs.WriteC0S0(c); err != nil {
|
||||
return errors.Wrap(err, "write c0")
|
||||
}
|
||||
if err := hs.WriteC1S1(c); err != nil {
|
||||
return errors.Wrap(err, "write c1")
|
||||
}
|
||||
|
||||
if _, err = hs.ReadC0S0(c); err != nil {
|
||||
return errors.Wrap(err, "read s1")
|
||||
}
|
||||
s1, err := hs.ReadC1S1(c)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "read s1")
|
||||
}
|
||||
if _, err = hs.ReadC2S2(c); err != nil {
|
||||
return errors.Wrap(err, "read s2")
|
||||
}
|
||||
|
||||
if err := hs.WriteC2S2(c, s1); err != nil {
|
||||
return errors.Wrap(err, "write c2")
|
||||
}
|
||||
logger.Tf(ctx, "RTMP handshake with %v:%v success", ips[0], port)
|
||||
|
||||
// Do connect and publish.
|
||||
client := rtmp.NewProtocol(c)
|
||||
|
||||
connectApp := rtmp.NewConnectAppPacket()
|
||||
tcURL := fmt.Sprintf("rtmp://%v%v", u.Hostname(), app)
|
||||
connectApp.CommandObject.Set("tcUrl", amf0.NewString(tcURL))
|
||||
if err = client.WritePacket(connectApp, 1); err != nil {
|
||||
return errors.Wrap(err, "write connect app")
|
||||
}
|
||||
|
||||
var connectAppRes *rtmp.ConnectAppResPacket
|
||||
if _, err = client.ExpectPacket(&connectAppRes); err != nil {
|
||||
return errors.Wrap(err, "expect connect app res")
|
||||
}
|
||||
logger.Tf(ctx, "RTMP connect app success, tcUrl=%v", tcURL)
|
||||
|
||||
createStream := rtmp.NewCreateStreamPacket()
|
||||
if err = client.WritePacket(createStream, 1); err != nil {
|
||||
return errors.Wrap(err, "write create stream")
|
||||
}
|
||||
|
||||
var createStreamRes *rtmp.CreateStreamResPacket
|
||||
if _, err = client.ExpectPacket(&createStreamRes); err != nil {
|
||||
return errors.Wrap(err, "expect create stream res")
|
||||
}
|
||||
logger.Tf(ctx, "RTMP create stream success")
|
||||
|
||||
publish := rtmp.NewPublishPacket()
|
||||
publish.StreamName = *amf0.NewString(stream)
|
||||
if err = client.WritePacket(publish, 1); err != nil {
|
||||
return errors.Wrap(err, "write publish")
|
||||
}
|
||||
logger.Tf(ctx, "RTMP publish stream success, stream=%v", stream)
|
||||
|
||||
if closeAfterPublished {
|
||||
logger.Tf(ctx, "Close connection after published")
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
68
trunk/3rdparty/srs-bench/live/stat.go
vendored
Normal file
68
trunk/3rdparty/srs-bench/live/stat.go
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
// The MIT License (MIT)
|
||||
//
|
||||
// # Copyright (c) 2021 Winlin
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
package live
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/ossrs/go-oryx-lib/logger"
|
||||
)
|
||||
|
||||
type statLive struct {
|
||||
Publishers struct {
|
||||
Expect int `json:"expect"`
|
||||
Alive int `json:"alive"`
|
||||
} `json:"publishers"`
|
||||
Subscribers struct {
|
||||
Expect int `json:"expect"`
|
||||
Alive int `json:"alive"`
|
||||
} `json:"subscribers"`
|
||||
PeerConnection interface{} `json:"random-pc"`
|
||||
}
|
||||
|
||||
var gStatLive statLive
|
||||
|
||||
func handleStat(ctx context.Context, mux *http.ServeMux, l string) {
|
||||
if strings.HasPrefix(l, ":") {
|
||||
l = "127.0.0.1" + l
|
||||
}
|
||||
|
||||
logger.Tf(ctx, "Handle http://%v/api/v1/sb/live", l)
|
||||
mux.HandleFunc("/api/v1/sb/live", func(w http.ResponseWriter, r *http.Request) {
|
||||
res := &struct {
|
||||
Code int `json:"code"`
|
||||
Data interface{} `json:"data"`
|
||||
}{
|
||||
0, &gStatLive,
|
||||
}
|
||||
|
||||
b, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
logger.Wf(ctx, "marshal %v err %+v", res, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue