Merge pull request #1131 from strukturag/improve-sprintf-performance

Don't use fmt.Sprintf where not necessary.
This commit is contained in:
Joachim Bauch 2025-11-24 14:07:10 +01:00 committed by GitHub
commit 0e835c8130
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 138 additions and 6 deletions

View file

@ -62,6 +62,26 @@ jobs:
run: |
make test TIMEOUT=120s
benchmark:
env:
MAXMIND_GEOLITE2_LICENSE: ${{ secrets.MAXMIND_GEOLITE2_LICENSE }}
USE_DB_IP_GEOIP_DATABASE: "1"
strategy:
matrix:
go-version:
- "1.24"
- "1.25"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
- name: Run benchmarks
run: |
make benchmark
coverage:
env:
MAXMIND_GEOLITE2_LICENSE: ${{ secrets.MAXMIND_GEOLITE2_LICENSE }}

View file

@ -51,6 +51,10 @@ ifeq ($(TIMEOUT),)
TIMEOUT := 60s
endif
ifeq ($(BENCHMARK),)
BENCHMARK := .
endif
ifneq ($(TEST),)
TESTARGS := $(TESTARGS) -run "$(TEST)"
endif
@ -111,6 +115,9 @@ vet:
test: vet
GOEXPERIMENT=synctest $(GO) test -timeout $(TIMEOUT) $(TESTARGS) ./...
benchmark:
GOEXPERIMENT=synctest $(GO) test -bench=$(BENCHMARK) -benchmem -run=^$$ -timeout $(TIMEOUT) $(TESTARGS) ./...
checklocks: $(GOPATHBIN)/checklocks
GOEXPERIMENT=synctest go vet -vettool=$(GOPATHBIN)/checklocks ./...

View file

@ -23,7 +23,6 @@ package signaling
import (
"context"
"fmt"
"sync"
"time"
@ -55,7 +54,7 @@ func GetSubjectForUserId(userId string, backend *Backend) string {
}
func GetSubjectForSessionId(sessionId PublicSessionId, backend *Backend) string {
return fmt.Sprintf("session.%s", sessionId)
return string("session." + sessionId)
}
type asyncSubscriberNats struct {

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)
}
}

8
hub.go
View file

@ -663,7 +663,7 @@ func (h *Hub) decodePrivateSessionId(id PrivateSessionId) *SessionIdData {
return nil
}
cache_key := fmt.Sprintf("%s|%s", id, privateSessionName)
cache_key := string(id + "|" + privateSessionName)
cache := h.getDecodeCache(cache_key)
if result := cache.Get(cache_key); result != nil {
return result
@ -683,7 +683,7 @@ func (h *Hub) decodePublicSessionId(id PublicSessionId) *SessionIdData {
return nil
}
cache_key := fmt.Sprintf("%s|%s", id, publicSessionName)
cache_key := string(id + "|" + publicSessionName)
cache := h.getDecodeCache(cache_key)
if result := cache.Get(cache_key); result != nil {
return result
@ -2072,7 +2072,7 @@ func (h *Hub) processMessageMsg(sess Session, message *ClientMessage) {
if h.mcu != nil {
// Maybe this is a message to be processed by the MCU.
var data MessageClientMessageData
if err := json.Unmarshal(msg.Data, &data); err == nil {
if err := data.UnmarshalJSON(msg.Data); err == nil {
if err := data.CheckValid(); err != nil {
h.logger.Printf("Invalid message %+v from client %s: %v", message, session.PublicId(), err)
if err, ok := err.(*Error); ok {
@ -2189,7 +2189,7 @@ func (h *Hub) processMessageMsg(sess Session, message *ClientMessage) {
if h.mcu != nil {
var data MessageClientMessageData
if err := json.Unmarshal(msg.Data, &data); err == nil {
if err := data.UnmarshalJSON(msg.Data); err == nil {
if err := data.CheckValid(); err != nil {
h.logger.Printf("Invalid message %+v from client %s: %v", message, session.PublicId(), err)
if err, ok := err.(*Error); ok {

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)