style(audio): fix formatting and add missing newlines

- Fix indentation and alignment in performance tests
- Add missing newlines at end of files
- Clean up error message formatting for consistency
This commit is contained in:
Alex P 2025-08-27 20:54:50 +00:00
parent 9dda569523
commit e3e7b898b5
4 changed files with 34 additions and 34 deletions

View File

@ -320,4 +320,4 @@ func (als *AudioLoggerStandards) WithSubComponent(subComponent string) *AudioLog
logger: als.logger.With().Str("sub_component", subComponent).Logger(), logger: als.logger.With().Str("sub_component", subComponent).Logger(),
component: als.component + "." + subComponent, component: als.component + "." + subComponent,
} }
} }

View File

@ -45,7 +45,7 @@ func TestPerformanceCriticalPaths(t *testing.T) {
// This is the most critical path that must not interfere with KVM // This is the most critical path that must not interfere with KVM
func testAudioFrameProcessingLatency(t *testing.T) { func testAudioFrameProcessingLatency(t *testing.T) {
const ( const (
frameCount = 1000 frameCount = 1000
maxLatencyPerFrame = 100 * time.Microsecond // Very strict requirement maxLatencyPerFrame = 100 * time.Microsecond // Very strict requirement
) )
@ -61,7 +61,7 @@ func testAudioFrameProcessingLatency(t *testing.T) {
// Simulate the critical path: validation + metrics update // Simulate the critical path: validation + metrics update
err := ValidateAudioFrameFast(frameData) err := ValidateAudioFrameFast(frameData)
require.NoError(t, err) require.NoError(t, err)
// Record frame received (atomic operation) // Record frame received (atomic operation)
RecordFrameReceived(len(frameData)) RecordFrameReceived(len(frameData))
} }
@ -69,22 +69,22 @@ func testAudioFrameProcessingLatency(t *testing.T) {
avgLatencyPerFrame := elapsed / frameCount avgLatencyPerFrame := elapsed / frameCount
t.Logf("Average frame processing latency: %v", avgLatencyPerFrame) t.Logf("Average frame processing latency: %v", avgLatencyPerFrame)
// Ensure frame processing is fast enough to not interfere with KVM // Ensure frame processing is fast enough to not interfere with KVM
assert.Less(t, avgLatencyPerFrame, maxLatencyPerFrame, assert.Less(t, avgLatencyPerFrame, maxLatencyPerFrame,
"Frame processing latency %v exceeds maximum %v - may interfere with KVM", "Frame processing latency %v exceeds maximum %v - may interfere with KVM",
avgLatencyPerFrame, maxLatencyPerFrame) avgLatencyPerFrame, maxLatencyPerFrame)
// Ensure total processing time is reasonable // Ensure total processing time is reasonable
maxTotalTime := 50 * time.Millisecond maxTotalTime := 50 * time.Millisecond
assert.Less(t, elapsed, maxTotalTime, assert.Less(t, elapsed, maxTotalTime,
"Total processing time %v exceeds maximum %v", elapsed, maxTotalTime) "Total processing time %v exceeds maximum %v", elapsed, maxTotalTime)
} }
// testMetricsUpdateOverhead tests the overhead of metrics updates // testMetricsUpdateOverhead tests the overhead of metrics updates
func testMetricsUpdateOverhead(t *testing.T) { func testMetricsUpdateOverhead(t *testing.T) {
const iterations = 10000 const iterations = 10000
// Test RecordFrameReceived performance // Test RecordFrameReceived performance
start := time.Now() start := time.Now()
for i := 0; i < iterations; i++ { for i := 0; i < iterations; i++ {
@ -202,7 +202,7 @@ func testMemoryAllocationPatterns(t *testing.T) {
// testConcurrentAccessPerformance tests performance under concurrent access // testConcurrentAccessPerformance tests performance under concurrent access
func testConcurrentAccessPerformance(t *testing.T) { func testConcurrentAccessPerformance(t *testing.T) {
const ( const (
numGoroutines = 10 numGoroutines = 10
operationsPerGoroutine = 1000 operationsPerGoroutine = 1000
) )
@ -215,7 +215,7 @@ func testConcurrentAccessPerformance(t *testing.T) {
go func() { go func() {
defer wg.Done() defer wg.Done()
frameData := make([]byte, 1920) frameData := make([]byte, 1920)
for j := 0; j < operationsPerGoroutine; j++ { for j := 0; j < operationsPerGoroutine; j++ {
// Simulate concurrent audio processing // Simulate concurrent audio processing
_ = ValidateAudioFrameFast(frameData) _ = ValidateAudioFrameFast(frameData)
@ -232,7 +232,7 @@ func testConcurrentAccessPerformance(t *testing.T) {
totalOperations := numGoroutines * operationsPerGoroutine * 4 // 4 operations per iteration totalOperations := numGoroutines * operationsPerGoroutine * 4 // 4 operations per iteration
avgLatency := elapsed / time.Duration(totalOperations) avgLatency := elapsed / time.Duration(totalOperations)
t.Logf("Concurrent access: %d operations in %v (avg: %v per operation)", t.Logf("Concurrent access: %d operations in %v (avg: %v per operation)",
totalOperations, elapsed, avgLatency) totalOperations, elapsed, avgLatency)
// Concurrent access should not significantly degrade performance // Concurrent access should not significantly degrade performance
@ -306,7 +306,7 @@ func TestRegressionDetection(t *testing.T) {
start := time.Now() start := time.Now()
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
_ = ValidateAudioFrameFast(frameData) _ = ValidateAudioFrameFast(frameData)
RecordFrameReceived(len(frameData)) RecordFrameReceived(len(frameData))
} }
frameProcessingTime := time.Since(start) / 100 frameProcessingTime := time.Since(start) / 100
@ -335,13 +335,13 @@ func TestRegressionDetection(t *testing.T) {
// - ARM Cortex-A7 @ 1GHz single core // - ARM Cortex-A7 @ 1GHz single core
// - 256MB DDR3L RAM // - 256MB DDR3L RAM
// - Must not interfere with primary KVM functionality // - Must not interfere with primary KVM functionality
assert.Less(t, frameProcessingTime, baselines["frame_processing"], assert.Less(t, frameProcessingTime, baselines["frame_processing"],
"Frame processing regression: %v > %v", frameProcessingTime, baselines["frame_processing"]) "Frame processing regression: %v > %v", frameProcessingTime, baselines["frame_processing"])
assert.Less(t, metricsUpdateTime, 100*time.Microsecond, assert.Less(t, metricsUpdateTime, 100*time.Microsecond,
"Metrics update regression: %v > 100μs", metricsUpdateTime) "Metrics update regression: %v > 100μs", metricsUpdateTime)
assert.Less(t, configAccessTime, 10*time.Microsecond, assert.Less(t, configAccessTime, 10*time.Microsecond,
"Config access regression: %v > 10μs", configAccessTime) "Config access regression: %v > 10μs", configAccessTime)
assert.Less(t, validationTime, 10*time.Microsecond, assert.Less(t, validationTime, 10*time.Microsecond,
"Validation regression: %v > 10μs", validationTime) "Validation regression: %v > 10μs", validationTime)
t.Logf("Performance results:") t.Logf("Performance results:")
@ -384,6 +384,6 @@ func TestMemoryLeakDetection(t *testing.T) {
t.Logf("Memory growth after 10,000 operations: %d bytes", memoryGrowth) t.Logf("Memory growth after 10,000 operations: %d bytes", memoryGrowth)
// Memory growth should be minimal (less than 1MB) // Memory growth should be minimal (less than 1MB)
assert.Less(t, memoryGrowth, int64(1024*1024), assert.Less(t, memoryGrowth, int64(1024*1024),
"Excessive memory growth detected: %d bytes", memoryGrowth) "Excessive memory growth detected: %d bytes", memoryGrowth)
} }

View File

@ -29,7 +29,7 @@ var (
func ValidateAudioQuality(quality AudioQuality) error { func ValidateAudioQuality(quality AudioQuality) error {
// Validate enum range // Validate enum range
if quality < AudioQualityLow || quality > AudioQualityUltra { if quality < AudioQualityLow || quality > AudioQualityUltra {
return fmt.Errorf("%w: quality value %d outside valid range [%d, %d]", return fmt.Errorf("%w: quality value %d outside valid range [%d, %d]",
ErrInvalidAudioQuality, int(quality), int(AudioQualityLow), int(AudioQualityUltra)) ErrInvalidAudioQuality, int(quality), int(AudioQualityLow), int(AudioQualityUltra))
} }
return nil return nil
@ -43,21 +43,21 @@ func ValidateFrameData(data []byte) error {
if len(data) == 0 { if len(data) == 0 {
return fmt.Errorf("%w: frame data is empty", ErrInvalidFrameData) return fmt.Errorf("%w: frame data is empty", ErrInvalidFrameData)
} }
config := GetConfig() config := GetConfig()
// Check minimum frame size // Check minimum frame size
if len(data) < config.MinFrameSize { if len(data) < config.MinFrameSize {
return fmt.Errorf("%w: frame size %d below minimum %d", return fmt.Errorf("%w: frame size %d below minimum %d",
ErrInvalidFrameSize, len(data), config.MinFrameSize) ErrInvalidFrameSize, len(data), config.MinFrameSize)
} }
// Check maximum frame size // Check maximum frame size
if len(data) > config.MaxAudioFrameSize { if len(data) > config.MaxAudioFrameSize {
return fmt.Errorf("%w: frame size %d exceeds maximum %d", return fmt.Errorf("%w: frame size %d exceeds maximum %d",
ErrInvalidFrameSize, len(data), config.MaxAudioFrameSize) ErrInvalidFrameSize, len(data), config.MaxAudioFrameSize)
} }
// Validate frame alignment for audio samples (must be even for 16-bit samples) // Validate frame alignment for audio samples (must be even for 16-bit samples)
if len(data)%2 != 0 { if len(data)%2 != 0 {
return fmt.Errorf("%w: frame size %d not aligned for 16-bit samples", return fmt.Errorf("%w: frame size %d not aligned for 16-bit samples",
ErrInvalidFrameSize, len(data)) ErrInvalidFrameSize, len(data))
} }
return nil return nil
@ -89,7 +89,7 @@ func ValidateBufferSize(size int) error {
// Use SocketMaxBuffer as the upper limit for general buffer validation // Use SocketMaxBuffer as the upper limit for general buffer validation
// This allows for socket buffers while still preventing extremely large allocations // This allows for socket buffers while still preventing extremely large allocations
if size > config.SocketMaxBuffer { if size > config.SocketMaxBuffer {
return fmt.Errorf("%w: buffer size %d exceeds maximum %d", return fmt.Errorf("%w: buffer size %d exceeds maximum %d",
ErrInvalidBufferSize, size, config.SocketMaxBuffer) ErrInvalidBufferSize, size, config.SocketMaxBuffer)
} }
return nil return nil
@ -99,7 +99,7 @@ func ValidateBufferSize(size int) error {
func ValidateThreadPriority(priority int) error { func ValidateThreadPriority(priority int) error {
const minPriority, maxPriority = -20, 19 const minPriority, maxPriority = -20, 19
if priority < minPriority || priority > maxPriority { if priority < minPriority || priority > maxPriority {
return fmt.Errorf("%w: priority %d outside valid range [%d, %d]", return fmt.Errorf("%w: priority %d outside valid range [%d, %d]",
ErrInvalidPriority, priority, minPriority, maxPriority) ErrInvalidPriority, priority, minPriority, maxPriority)
} }
return nil return nil
@ -113,11 +113,11 @@ func ValidateLatency(latency time.Duration) error {
config := GetConfig() config := GetConfig()
minLatency := time.Millisecond // Minimum reasonable latency minLatency := time.Millisecond // Minimum reasonable latency
if latency > 0 && latency < minLatency { if latency > 0 && latency < minLatency {
return fmt.Errorf("%w: latency %v below minimum %v", return fmt.Errorf("%w: latency %v below minimum %v",
ErrInvalidLatency, latency, minLatency) ErrInvalidLatency, latency, minLatency)
} }
if latency > config.MaxLatency { if latency > config.MaxLatency {
return fmt.Errorf("%w: latency %v exceeds maximum %v", return fmt.Errorf("%w: latency %v exceeds maximum %v",
ErrInvalidLatency, latency, config.MaxLatency) ErrInvalidLatency, latency, config.MaxLatency)
} }
return nil return nil
@ -233,7 +233,7 @@ func ValidateSampleRate(sampleRate int) error {
return nil return nil
} }
} }
return fmt.Errorf("%w: sample rate %d not in supported rates %v", return fmt.Errorf("%w: sample rate %d not in supported rates %v",
ErrInvalidSampleRate, sampleRate, validRates) ErrInvalidSampleRate, sampleRate, validRates)
} }
@ -244,7 +244,7 @@ func ValidateChannelCount(channels int) error {
} }
config := GetConfig() config := GetConfig()
if channels > config.MaxChannels { if channels > config.MaxChannels {
return fmt.Errorf("%w: channel count %d exceeds maximum %d", return fmt.Errorf("%w: channel count %d exceeds maximum %d",
ErrInvalidChannels, channels, config.MaxChannels) ErrInvalidChannels, channels, config.MaxChannels)
} }
return nil return nil
@ -259,11 +259,11 @@ func ValidateBitrate(bitrate int) error {
// Convert kbps to bps for comparison with config limits // Convert kbps to bps for comparison with config limits
bitrateInBps := bitrate * 1000 bitrateInBps := bitrate * 1000
if bitrateInBps < config.MinOpusBitrate { if bitrateInBps < config.MinOpusBitrate {
return fmt.Errorf("%w: bitrate %d kbps (%d bps) below minimum %d bps", return fmt.Errorf("%w: bitrate %d kbps (%d bps) below minimum %d bps",
ErrInvalidBitrate, bitrate, bitrateInBps, config.MinOpusBitrate) ErrInvalidBitrate, bitrate, bitrateInBps, config.MinOpusBitrate)
} }
if bitrateInBps > config.MaxOpusBitrate { if bitrateInBps > config.MaxOpusBitrate {
return fmt.Errorf("%w: bitrate %d kbps (%d bps) exceeds maximum %d bps", return fmt.Errorf("%w: bitrate %d kbps (%d bps) exceeds maximum %d bps",
ErrInvalidBitrate, bitrate, bitrateInBps, config.MaxOpusBitrate) ErrInvalidBitrate, bitrate, bitrateInBps, config.MaxOpusBitrate)
} }
return nil return nil
@ -276,11 +276,11 @@ func ValidateFrameDuration(duration time.Duration) error {
} }
config := GetConfig() config := GetConfig()
if duration < config.MinFrameDuration { if duration < config.MinFrameDuration {
return fmt.Errorf("%w: frame duration %v below minimum %v", return fmt.Errorf("%w: frame duration %v below minimum %v",
ErrInvalidFrameDuration, duration, config.MinFrameDuration) ErrInvalidFrameDuration, duration, config.MinFrameDuration)
} }
if duration > config.MaxFrameDuration { if duration > config.MaxFrameDuration {
return fmt.Errorf("%w: frame duration %v exceeds maximum %v", return fmt.Errorf("%w: frame duration %v exceeds maximum %v",
ErrInvalidFrameDuration, duration, config.MaxFrameDuration) ErrInvalidFrameDuration, duration, config.MaxFrameDuration)
} }
return nil return nil

View File

@ -553,4 +553,4 @@ func TestValidationPerformance(t *testing.T) {
// Audio processing must not interfere with primary KVM functionality // Audio processing must not interfere with primary KVM functionality
assert.Less(t, perIteration, 200*time.Microsecond, "Validation should not impact KVM performance") assert.Less(t, perIteration, 200*time.Microsecond, "Validation should not impact KVM performance")
t.Logf("Validation performance: %v per iteration", perIteration) t.Logf("Validation performance: %v per iteration", perIteration)
} }