Add benchmarks for string concatenation.

This commit is contained in:
Joachim Bauch 2025-11-24 12:31:21 +01:00
commit 4250d912ae
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 106 additions and 0 deletions

47
async_events_nats_test.go Normal file
View file

@ -0,0 +1,47 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2025 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package signaling
import (
"testing"
"google.golang.org/protobuf/types/known/timestamppb"
)
func Benchmark_GetSubjectForSessionId(b *testing.B) {
backend := &Backend{
id: "compat",
}
data := &SessionIdData{
Sid: 1,
Created: timestamppb.Now(),
BackendId: backend.Id(),
}
codec := NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
sid, err := codec.EncodePublic(data)
if err != nil {
b.Fatalf("could not create session id: %s", err)
}
for b.Loop() {
GetSubjectForSessionId(sid, backend)
}
}

View file

@ -51,6 +51,7 @@ import (
"github.com/nats-io/nats-server/v2/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/strukturag/nextcloud-spreed-signaling/api"
)
@ -827,6 +828,64 @@ func performHousekeeping(hub *Hub, now time.Time) *sync.WaitGroup {
return &wg
}
func Benchmark_DecodePrivateSessionId(b *testing.B) {
decodeCaches := make([]*LruCache[*SessionIdData], 0, numDecodeCaches)
for range numDecodeCaches {
decodeCaches = append(decodeCaches, NewLruCache[*SessionIdData](decodeCacheSize))
}
backend := &Backend{
id: "compat",
}
data := &SessionIdData{
Sid: 1,
Created: timestamppb.Now(),
BackendId: backend.Id(),
}
codec := NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
sid, err := codec.EncodePrivate(data)
if err != nil {
b.Fatalf("could not create session id: %s", err)
}
hub := &Hub{
cookie: codec,
decodeCaches: decodeCaches,
}
// Decode once to populate cache.
hub.decodePrivateSessionId(sid)
for b.Loop() {
hub.decodePrivateSessionId(sid)
}
}
func Benchmark_DecodePublicSessionId(b *testing.B) {
decodeCaches := make([]*LruCache[*SessionIdData], 0, numDecodeCaches)
for range numDecodeCaches {
decodeCaches = append(decodeCaches, NewLruCache[*SessionIdData](decodeCacheSize))
}
backend := &Backend{
id: "compat",
}
data := &SessionIdData{
Sid: 1,
Created: timestamppb.Now(),
BackendId: backend.Id(),
}
codec := NewSessionIdCodec([]byte("12345678901234567890123456789012"), []byte("09876543210987654321098765432109"))
sid, err := codec.EncodePublic(data)
if err != nil {
b.Fatalf("could not create session id: %s", err)
}
hub := &Hub{
cookie: codec,
decodeCaches: decodeCaches,
}
// Decode once to populate cache.
hub.decodePublicSessionId(sid)
for b.Loop() {
hub.decodePublicSessionId(sid)
}
}
func TestWebsocketFeatures(t *testing.T) {
t.Parallel()
require := require.New(t)