Skip to content

Commit b625de0

Browse files
committed
add MAX_REQUESTS to limit number of invite request per second on benchmarking server
1 parent 1566e28 commit b625de0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

server_integration_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"errors"
99
"net"
1010
"os"
11+
"strconv"
1112
"sync"
1213
"testing"
14+
"time"
1315

1416
"github.com/emiago/sipgo/sip"
1517
"github.com/stretchr/testify/assert"
@@ -238,6 +240,27 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
238240
t.Log("Server ready")
239241
}
240242

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+
241264
for _, tc := range testCases {
242265
t.Run(tc.transport, func(t *testing.B) {
243266
proto := "sip"
@@ -252,7 +275,12 @@ func BenchmarkIntegrationClientServer(t *testing.B) {
252275
ua, _ := NewUA(WithUserAgenTLSConfig(clientTLS))
253276
client, err := NewClient(ua)
254277
require.NoError(t, err)
278+
255279
for p.Next() {
280+
// If we are running in limit mode
281+
if maxInvitesPerSec != nil {
282+
maxInvitesPerSec <- struct{}{}
283+
}
256284
req, _, _ := createTestInvite(t, proto+":bob@"+tc.serverAddr, tc.transport, client.ip.String())
257285
tx, err := client.TransactionRequest(ctx, req)
258286
require.NoError(t, err)

0 commit comments

Comments
 (0)