@@ -8,8 +8,10 @@ import (
8
8
"errors"
9
9
"net"
10
10
"os"
11
+ "strconv"
11
12
"sync"
12
13
"testing"
14
+ "time"
13
15
14
16
"github.com/emiago/sipgo/sip"
15
17
"github.com/stretchr/testify/assert"
@@ -238,6 +240,27 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
238
240
t .Log ("Server ready" )
239
241
}
240
242
243
+ var maxInvitesPerSec chan struct {}
244
+ if v := os .Getenv ("MAX_REQUESTS" ); v != "" {
245
+ t .Logf ("Limiting number of requests: %s req/s" , v )
246
+ maxInvites , _ := strconv .Atoi (v )
247
+ maxInvitesPerSec = make (chan struct {}, maxInvites )
248
+ ctx , cancel := context .WithCancel (context .Background ())
249
+ defer cancel ()
250
+ go func () {
251
+ for {
252
+ select {
253
+ case <- ctx .Done ():
254
+ return
255
+ case <- time .After (1 * time .Second ):
256
+ for i := 0 ; i < maxInvites ; i ++ {
257
+ <- maxInvitesPerSec
258
+ }
259
+ }
260
+ }
261
+ }()
262
+ }
263
+
241
264
for _ , tc := range testCases {
242
265
t .Run (tc .transport , func (t * testing.B ) {
243
266
proto := "sip"
@@ -252,7 +275,12 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
252
275
ua , _ := NewUA (WithUserAgenTLSConfig (clientTLS ))
253
276
client , err := NewClient (ua )
254
277
require .NoError (t , err )
278
+
255
279
for p .Next () {
280
+ // If we are running in limit mode
281
+ if maxInvitesPerSec != nil {
282
+ maxInvitesPerSec <- struct {}{}
283
+ }
256
284
req , _ , _ := createTestInvite (t , proto + ":bob@" + tc .serverAddr , tc .transport , client .ip .String ())
257
285
tx , err := client .TransactionRequest (ctx , req )
258
286
require .NoError (t , err )
0 commit comments