Files
golib/socket/config/performance_test.go
nabbar e89b03987d Package Socket:
- Update config: add tags to json, yml, ... serialization
- Update config: extract TLS struct as independant struct to simplify
- Update config/test: fix following unify TLS config
- Update server/test: fix following unify TLS config
2025-12-22 13:05:28 +01:00

344 lines
10 KiB
Go

/*
* MIT License
*
* Copyright (c) 2025 Nicolas JUHEL
*
* 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.
*
*
*/
// Performance Tests - Socket Configuration Package
//
// This file contains performance benchmarks using gmeasure to measure the
// efficiency of configuration operations.
//
// Test Coverage:
// - Validation performance: TCP, UDP validation latency
// - Creation performance: Struct initialization overhead
// - Copy performance: Structure copy efficiency
// - TLS methods performance: GetTLS() call overhead
// - Protocol comparison: Relative performance across protocols
// - Client vs Server: Comparative validation costs
// - Memory performance: Allocation efficiency and structure size
//
// Performance Goals:
// - Validation: < 1ms average for network protocols
// - Creation: < 100µs for struct initialization
// - Copy: < 10µs for value copy operations
// - TLS methods: < 50µs for GetTLS() calls
//
// Measurement Methodology:
// Uses gmeasure.Experiment for statistical analysis with multiple samples.
// Reports include median, mean, and standard deviation for accurate performance
// characterization. Tests are run with N=500-10000 samples for reliability.
package config_test
import (
"time"
libptc "github.com/nabbar/golib/network/protocol"
"github.com/nabbar/golib/socket/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gmeasure"
)
var _ = Describe("Client Performance", func() {
Context("Validation performance", func() {
It("should validate TCP clients efficiently", func() {
exp := gmeasure.NewExperiment("TCP Client Validation")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
c := config.Client{
Network: libptc.NetworkTCP,
Address: "localhost:8080",
}
exp.MeasureDuration("validation", func() {
_ = c.Validate()
})
}, gmeasure.SamplingConfig{N: 1000})
stats := exp.GetStats("validation")
AddReportEntry("Validation Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 1*time.Millisecond))
})
It("should validate UDP clients efficiently", func() {
exp := gmeasure.NewExperiment("UDP Client Validation")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
c := config.Client{
Network: libptc.NetworkUDP,
Address: "localhost:9000",
}
exp.MeasureDuration("validation", func() {
_ = c.Validate()
})
}, gmeasure.SamplingConfig{N: 1000})
stats := exp.GetStats("validation")
AddReportEntry("Validation Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 1*time.Millisecond))
})
})
Context("Creation performance", func() {
It("should create clients efficiently", func() {
exp := gmeasure.NewExperiment("Client Creation")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
exp.MeasureDuration("creation", func() {
_ = config.Client{
Network: libptc.NetworkTCP,
Address: "localhost:8080",
}
})
}, gmeasure.SamplingConfig{N: 10000})
stats := exp.GetStats("creation")
AddReportEntry("Creation Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 100*time.Microsecond))
})
})
Context("Copy performance", func() {
It("should copy clients efficiently", func() {
original := config.Client{
Network: libptc.NetworkTCP,
Address: "localhost:8080",
}
exp := gmeasure.NewExperiment("Client Copy")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
exp.MeasureDuration("copy", func() {
_ = original
})
}, gmeasure.SamplingConfig{N: 10000})
stats := exp.GetStats("copy")
AddReportEntry("Copy Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 10*time.Microsecond))
})
})
})
var _ = Describe("Server Performance", func() {
Context("Validation performance", func() {
It("should validate TCP servers efficiently", func() {
exp := gmeasure.NewExperiment("TCP Server Validation")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
s := config.Server{
Network: libptc.NetworkTCP,
Address: ":8080",
}
exp.MeasureDuration("validation", func() {
_ = s.Validate()
})
}, gmeasure.SamplingConfig{N: 1000})
stats := exp.GetStats("validation")
AddReportEntry("Validation Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 1*time.Millisecond))
})
It("should validate UDP servers efficiently", func() {
exp := gmeasure.NewExperiment("UDP Server Validation")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
s := config.Server{
Network: libptc.NetworkUDP,
Address: ":9000",
}
exp.MeasureDuration("validation", func() {
_ = s.Validate()
})
}, gmeasure.SamplingConfig{N: 1000})
stats := exp.GetStats("validation")
AddReportEntry("Validation Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 1*time.Millisecond))
})
})
Context("Creation performance", func() {
It("should create servers efficiently", func() {
exp := gmeasure.NewExperiment("Server Creation")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
exp.MeasureDuration("creation", func() {
_ = config.Server{
Network: libptc.NetworkTCP,
Address: ":8080",
}
})
}, gmeasure.SamplingConfig{N: 10000})
stats := exp.GetStats("creation")
AddReportEntry("Creation Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 100*time.Microsecond))
})
})
Context("TLS methods performance", func() {
It("should call GetTLS efficiently", func() {
s := config.Server{
Network: libptc.NetworkTCP,
Address: ":8080",
}
s.TLS.Enabled = false
exp := gmeasure.NewExperiment("GetTLS Call")
AddReportEntry(exp.Name, exp)
exp.Sample(func(idx int) {
exp.MeasureDuration("getTLS", func() {
_, _ = s.GetTLS()
})
}, gmeasure.SamplingConfig{N: 10000})
stats := exp.GetStats("getTLS")
AddReportEntry("GetTLS Stats", stats)
Expect(stats.DurationFor(gmeasure.StatMean)).To(BeNumerically("<", 50*time.Microsecond))
})
})
})
var _ = Describe("Comparative Performance", func() {
Context("Protocol comparison", func() {
It("should compare validation performance across protocols", func() {
protocols := []struct {
name string
proto libptc.NetworkProtocol
addr string
}{
{"TCP", libptc.NetworkTCP, "localhost:8080"},
{"TCP4", libptc.NetworkTCP4, "127.0.0.1:8080"},
{"TCP6", libptc.NetworkTCP6, "[::1]:8080"},
{"UDP", libptc.NetworkUDP, "localhost:9000"},
{"UDP4", libptc.NetworkUDP4, "127.0.0.1:9000"},
{"UDP6", libptc.NetworkUDP6, "[::1]:9000"},
}
exp := gmeasure.NewExperiment("Protocol Validation Comparison")
AddReportEntry(exp.Name, exp)
for _, p := range protocols {
exp.Sample(func(idx int) {
c := config.Client{
Network: p.proto,
Address: p.addr,
}
exp.MeasureDuration(p.name, func() {
_ = c.Validate()
})
}, gmeasure.SamplingConfig{N: 500})
}
for _, p := range protocols {
stats := exp.GetStats(p.name)
AddReportEntry(p.name+" Stats", stats)
}
})
})
Context("Client vs Server", func() {
It("should compare client and server validation performance", func() {
exp := gmeasure.NewExperiment("Client vs Server Validation")
AddReportEntry(exp.Name, exp)
// Client validation
exp.Sample(func(idx int) {
c := config.Client{
Network: libptc.NetworkTCP,
Address: "localhost:8080",
}
exp.MeasureDuration("client", func() {
_ = c.Validate()
})
}, gmeasure.SamplingConfig{N: 1000})
// Server validation
exp.Sample(func(idx int) {
s := config.Server{
Network: libptc.NetworkTCP,
Address: ":8080",
}
exp.MeasureDuration("server", func() {
_ = s.Validate()
})
}, gmeasure.SamplingConfig{N: 1000})
clientStats := exp.GetStats("client")
serverStats := exp.GetStats("server")
AddReportEntry("Client Stats", clientStats)
AddReportEntry("Server Stats", serverStats)
})
})
})
var _ = Describe("Memory Performance", func() {
Context("Allocation efficiency", func() {
It("should minimize allocations during validation", func() {
c := config.Client{
Network: libptc.NetworkTCP,
Address: "localhost:8080",
}
// Warm up
_ = c.Validate()
// The validation should not cause excessive allocations
// This is a qualitative test - actual measurement would require runtime profiling
for i := 0; i < 1000; i++ {
_ = c.Validate()
}
Succeed()
})
})
Context("Structure size", func() {
It("should have reasonable client structure size", func() {
c := config.Client{}
// Client should be small enough to copy efficiently
// Actual size check would require unsafe.Sizeof
Expect(&c).NotTo(BeNil())
})
It("should have reasonable server structure size", func() {
s := config.Server{}
// Server should be small enough to copy efficiently
// Actual size check would require unsafe.Sizeof
Expect(&s).NotTo(BeNil())
})
})
})