diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-22 23:51:18 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-22 23:51:18 +0300 |
| commit | 849951be1d1a7ee9f9302006ccb187bf5b4e36f3 (patch) | |
| tree | 496c924a03a9ea6212e29bb4699e268066ebad81 /internal/regex/bench_test.go | |
| parent | bf78b3abffee6d49c08ca2980156afc455994969 (diff) | |
feat: DTail fork — server/client feature development
Squashed development of the snonux/dtail fork's product code (internal/, cmd/)
since diverging from mimecast/dtail. Major areas:
- Read/output path: the former "turbo" channel-less path is now the single,
default server-side read/output path for cat/grep/tail and MapReduce; the old
channel-based path and its config/env toggles were removed.
- MapReduce: single aggregate implementation (server + serverless) fed directly
by a processor pipeline, with input-exhausted finalization via the shutdown
coordinator; high-concurrency and data-race fixes.
- Journal source reads (journal:unit.service) via journalctl, Linux-gated behind
a journal-v1 capability.
- Auth-key fast reconnect: in-memory per-user public-key cache with TTL/max-keys,
registered over an authenticated session (AUTHKEY), checked before
authorized_keys.
- Interactive query reload (--interactive-query) with SESSION START/UPDATE
generation boundaries and capability negotiation.
- Client-side deadlines: --timeout / --shutdownAfter as context deadlines;
follow shutdown handling.
- Client logging: diagnostics-only daily log by default, opt-in payload tee via
--log-payload.
- Numerous correctness fixes (buffer-pool double-recycle races, EOF-sentinel
leaks, glob-expansion cap, TOCTOU in CSV parsing) with accompanying unit tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/regex/bench_test.go')
| -rw-r--r-- | internal/regex/bench_test.go | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/internal/regex/bench_test.go b/internal/regex/bench_test.go new file mode 100644 index 0000000..16fd98e --- /dev/null +++ b/internal/regex/bench_test.go @@ -0,0 +1,111 @@ +package regex + +import ( + "bytes" + "testing" +) + +func BenchmarkLiteralVsRegex(b *testing.B) { + // Test data - typical log lines + testLines := [][]byte{ + []byte("2024-01-01 10:00:00 INFO Starting application"), + []byte("2024-01-01 10:00:01 DEBUG Loading configuration"), + []byte("2024-01-01 10:00:02 ERROR Failed to connect to database"), + []byte("2024-01-01 10:00:03 WARN Retrying connection"), + []byte("2024-01-01 10:00:04 INFO Connection established"), + []byte("2024-01-01 10:00:05 ERROR Timeout while processing request"), + []byte("2024-01-01 10:00:06 DEBUG Processing request ID: 12345"), + []byte("2024-01-01 10:00:07 INFO Request processed successfully"), + []byte("2024-01-01 10:00:08 ERROR Invalid input parameters"), + []byte("2024-01-01 10:00:09 WARN High memory usage detected"), + } + + // Benchmark literal pattern matching (our optimization) + b.Run("Literal_ERROR", func(b *testing.B) { + r, _ := New("ERROR", Default) + if !r.isLiteral { + b.Fatal("Pattern should be detected as literal") + } + + b.ResetTimer() + matches := 0 + for i := 0; i < b.N; i++ { + for _, line := range testLines { + if r.Match(line) { + matches++ + } + } + } + _ = matches + }) + + // Force regex pattern matching for comparison + b.Run("Regex_ERROR", func(b *testing.B) { + // Add a harmless regex operator to force regex compilation + r, _ := New("(?:ERROR)", Default) + if r.isLiteral { + b.Fatal("Pattern should not be detected as literal") + } + + b.ResetTimer() + matches := 0 + for i := 0; i < b.N; i++ { + for _, line := range testLines { + if r.Match(line) { + matches++ + } + } + } + _ = matches + }) + + // Direct bytes.Contains for reference + b.Run("BytesContains_ERROR", func(b *testing.B) { + pattern := []byte("ERROR") + + b.ResetTimer() + matches := 0 + for i := 0; i < b.N; i++ { + for _, line := range testLines { + if bytes.Contains(line, pattern) { + matches++ + } + } + } + _ = matches + }) +} + +func BenchmarkComplexPatterns(b *testing.B) { + testLine := []byte("2024-01-01 10:00:00 ERROR Failed to connect to database server at 192.168.1.100:5432") + + patterns := []struct { + name string + pattern string + }{ + {"Simple_ERROR", "ERROR"}, + {"Simple_database", "database"}, + {"Regex_ERROR.*database", "ERROR.*database"}, + {"Regex_\\d+\\.\\d+\\.\\d+\\.\\d+", `\d+\.\d+\.\d+\.\d+`}, // IP address pattern + {"Regex_^2024", "^2024"}, + {"Regex_5432$", "5432$"}, + } + + for _, p := range patterns { + b.Run(p.name, func(b *testing.B) { + r, err := New(p.pattern, Default) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + matches := 0 + for i := 0; i < b.N; i++ { + if r.Match(testLine) { + matches++ + } + } + _ = matches + }) + } +}
\ No newline at end of file |
