Files
go-caatsm/internal/app/processor_bench_test.go
T
windyboyandClaude Sonnet 4.5 7b6f6383ad Add AFTN protocol validation and serial reader health monitoring to enhance aviation telegram processing reliability and observability
Implement comprehensive AFTN/ICAO protocol compliance validation with configurable enforcement, enabling early detection of malformed telegrams and reducing downstream processing errors. Add real-time serial reader health monitoring to automatically detect message flow interruptions and sequence gaps, ensuring operational visibility into the telegram ingestion pipeline.

Key enhancements:
- AFTN validator validates priority indicators (FF/GG/QU/DD/SS/KK), ICAO addresses (4-char alphanumeric), and datetime formats (DDHHMM) with detailed error categorization
- Invalid telegrams automatically routed to DLQ with full context for offline review and correction
- Serial reader health monitoring tracks message gaps and sequence numbers to detect stalled readers or missing messages within configurable threshold (default: 2 minutes)
- Four new Prometheus metrics expose validation errors by type, message gaps, sequence gaps, and health status for operational alerting
- Pre-configured Prometheus alert rules for critical conditions (stalled reader, high error rates, consumer lag)
- Grafana dashboard provides real-time visibility into AFTN compliance and serial reader health
- Validation disabled by default for safe rollout with zero breaking changes to existing functionality

Implementation maintains clean architecture with validator in adapter layer, extends processor and consumer with health tracking, and ensures thread-safe concurrent access to tracking state. All changes fully tested with 48 validator tests, 10 processor tests, and 21 consumer tests passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-24 14:21:59 +08:00

164 lines
3.8 KiB
Go

package app
import (
"caatsm/internal/adapter/dto"
"caatsm/internal/adapter/parser"
"caatsm/internal/infra/config"
"caatsm/internal/infra/telemetry"
"context"
"testing"
"time"
"go.uber.org/zap"
)
// Mock implementations for benchmarking
type mockRepository struct{}
func (m *mockRepository) InsertOne(ctx context.Context, msg *dto.ParsedTelegram) error {
return nil
}
func (m *mockRepository) InsertBatch(ctx context.Context, msgs []*dto.ParsedTelegram) error {
return nil
}
func (m *mockRepository) InsertRaw(ctx context.Context, msg *dto.ParsedTelegram) error {
return nil
}
type mockPublisher struct{}
func (m *mockPublisher) Publish(message interface{}) error {
return nil
}
// Benchmark data
var (
benchARRRaw = []byte(`ZCZC TMQ2526 141605
FF ZBTJZPZX
141604 ZBACZQZX
(ARR-JAE7433/A0132-RKSI-ZBTJ1604)
NNNN`)
benchDEPRaw = []byte(`ZCZC DEP5678 120915
DD KLAXZPZX
120914 KSFOZQZX
(DEP-ABC5678-A1234-ZBTJ1440-ZGGG)
NNNN`)
benchFPLRaw = []byte(`ZCZC TMQ2617 142150
GG ZBTJZPZX
150551 ZBTJUOBK
(FPL-OKA2861-IS
-MA60/M-SHID/C
-ZBTJ0030
-K0420S0450 CG J1 FZ
-ZSYT0100 ZSQD ZYTL
-REG/B3710 SEL/ RMK/TCAS )
NNNN`)
)
// createBenchmarkProcessor creates a processor with mocks for benchmarking
func createBenchmarkProcessor() *MessageProcessor {
aviationParser := parser.ProvideParser()
mockRepo := &mockRepository{}
mockPub := &mockPublisher{}
logger := zap.NewNop()
recorder := telemetry.NewNoop()
cfg := &config.Config{
AFTN: config.AFTNConfig{
ValidationEnabled: false,
MessageGapThreshold: 2 * time.Minute,
EnableSequenceGapDetection: true,
},
}
return NewMessageProcessor(aviationParser, mockRepo, mockPub, recorder, logger, cfg)
}
// BenchmarkHandleARR benchmarks processing ARR messages end-to-end
func BenchmarkHandleARR(b *testing.B) {
processor := createBenchmarkProcessor()
ctx := context.Background()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = processor.Handle(ctx, benchARRRaw, "msg-arr-123")
}
}
// BenchmarkHandleDEP benchmarks processing DEP messages end-to-end
func BenchmarkHandleDEP(b *testing.B) {
processor := createBenchmarkProcessor()
ctx := context.Background()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = processor.Handle(ctx, benchDEPRaw, "msg-dep-123")
}
}
// BenchmarkHandleFPL benchmarks processing FPL messages end-to-end
func BenchmarkHandleFPL(b *testing.B) {
processor := createBenchmarkProcessor()
ctx := context.Background()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = processor.Handle(ctx, benchFPLRaw, "msg-fpl-123")
}
}
// BenchmarkHandleMixed benchmarks processing a mix of message types
func BenchmarkHandleMixed(b *testing.B) {
processor := createBenchmarkProcessor()
ctx := context.Background()
messages := [][]byte{benchARRRaw, benchDEPRaw, benchFPLRaw}
msgIDs := []string{"msg-arr", "msg-dep", "msg-fpl"}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
idx := i % len(messages)
_ = processor.Handle(ctx, messages[idx], msgIDs[idx])
}
}
// BenchmarkHandleParseOnly benchmarks parsing without persistence/publishing
// This isolates parser performance
func BenchmarkHandleParseOnly(b *testing.B) {
aviationParser := parser.ProvideParser()
// Use a repository that does nothing
mockRepo := &mockRepository{}
// Use a publisher that does nothing
mockPub := &mockPublisher{}
logger := zap.NewNop()
recorder := telemetry.NewNoop()
cfg := &config.Config{
AFTN: config.AFTNConfig{
ValidationEnabled: false,
MessageGapThreshold: 2 * time.Minute,
EnableSequenceGapDetection: true,
},
}
processor := NewMessageProcessor(aviationParser, mockRepo, mockPub, recorder, logger, cfg)
ctx := context.Background()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = processor.Handle(ctx, benchARRRaw, "msg-parse-only")
}
}