summaryrefslogtreecommitdiff
path: root/internal/tools/benchmark/benchmark.go
blob: b72832914418add32e15a860c2c19a451ea40863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package benchmark

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"github.com/mimecast/dtail/internal/tools/common"
)

// Config holds benchmark configuration
type Config struct {
	Mode         string
	BaselineDir  string
	Tag          string
	Quick        bool
	Memory       bool
	OutputFile   string
	Verbose      bool
	Iterations   string
	BaselinePath string
}

// Run executes the benchmark command
func Run() error {
	cfg := parseFlags()

	// Create baseline directory if needed
	if err := common.EnsureDirectory(cfg.BaselineDir); err != nil {
		return fmt.Errorf("failed to create baseline directory: %w", err)
	}

	switch cfg.Mode {
	case "run":
		return runBenchmarks(cfg)
	case "baseline":
		return createBaseline(cfg)
	case "compare":
		return compareWithBaseline(cfg)
	case "list":
		return listBaselines(cfg)
	case "clean":
		return cleanBaselines(cfg)
	default:
		return fmt.Errorf("unknown benchmark mode: %s", cfg.Mode)
	}
}

func parseFlags() *Config {
	cfg := &Config{
		BaselineDir: "benchmarks/baselines",
		Iterations:  "1x",
	}

	flag.StringVar(&cfg.Mode, "mode", "run", "Benchmark mode: run, baseline, compare, list, clean")
	flag.StringVar(&cfg.BaselineDir, "dir", cfg.BaselineDir, "Baseline directory")
	flag.StringVar(&cfg.Tag, "tag", "", "Tag for baseline (e.g., 'before-optimization')")
	flag.BoolVar(&cfg.Quick, "quick", false, "Run only quick benchmarks")
	flag.BoolVar(&cfg.Memory, "memory", false, "Include memory profiling")
	flag.StringVar(&cfg.OutputFile, "output", "", "Output file for results")
	flag.BoolVar(&cfg.Verbose, "verbose", false, "Verbose output")
	flag.StringVar(&cfg.Iterations, "iterations", cfg.Iterations, "Benchmark iterations (e.g., 3x)")
	flag.StringVar(&cfg.BaselinePath, "baseline", "", "Baseline file for comparison")

	flag.Parse()

	// Handle positional arguments for compare mode
	if cfg.Mode == "compare" && cfg.BaselinePath == "" {
		args := flag.Args()
		if len(args) > 0 {
			cfg.BaselinePath = args[0]
		}
	}

	return cfg
}

func runBenchmarks(cfg *Config) error {
	common.PrintSection("Running DTail Benchmarks")

	// Build binaries
	common.PrintInfo("Building binaries...\n")
	if err := common.BuildCommands("dcat", "dgrep", "dmap", "dtail", "dserver"); err != nil {
		return fmt.Errorf("failed to build binaries: %w", err)
	}

	// Prepare benchmark command
	args := []string{"test", "-bench=."}
	if cfg.Quick {
		args = append(args, "-bench=BenchmarkQuick")
	}
	if cfg.Memory {
		args = append(args, "-benchmem")
	}
	if cfg.Iterations != "1x" {
		args = append(args, fmt.Sprintf("-benchtime=%s", cfg.Iterations))
	}
	if cfg.Verbose {
		args = append(args, "-v")
	}
	args = append(args, "./benchmarks")

	// Run benchmarks
	cmd := exec.Command("go", args...)
	
	var output []byte
	var err error
	
	if cfg.OutputFile != "" {
		// Capture output for file
		output, err = cmd.CombinedOutput()
		if err != nil {
			return fmt.Errorf("benchmark failed: %w\n%s", err, string(output))
		}
		
		// Write to file
		if err := os.WriteFile(cfg.OutputFile, output, 0644); err != nil {
			return fmt.Errorf("failed to write output file: %w", err)
		}
		
		// Also print to stdout
		fmt.Print(string(output))
		common.PrintSuccess("\nResults saved to: %s\n", cfg.OutputFile)
	} else {
		// Direct output to stdout
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("benchmark failed: %w", err)
		}
	}

	return nil
}

func createBaseline(cfg *Config) error {
	if cfg.Tag == "" {
		return fmt.Errorf("baseline tag is required (use -tag)")
	}

	common.PrintSection("Creating Benchmark Baseline")

	// Generate filename
	timestamp := time.Now().Format("20060102_150405")
	safeTag := strings.ReplaceAll(cfg.Tag, " ", "_")
	safeTag = strings.Map(func(r rune) rune {
		if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || 
		   (r >= '0' && r <= '9') || r == '.' || r == '_' || r == '-' {
			return r
		}
		return '_'
	}, safeTag)
	
	filename := filepath.Join(cfg.BaselineDir, 
		fmt.Sprintf("baseline_%s_%s.txt", timestamp, safeTag))

	// Create baseline file with metadata
	file, err := os.Create(filename)
	if err != nil {
		return fmt.Errorf("failed to create baseline file: %w", err)
	}
	defer file.Close()

	// Write metadata
	fmt.Fprintf(file, "Git commit: %s\n", common.GetGitCommit())
	fmt.Fprintf(file, "Date: %s\n", time.Now().Format(time.RFC3339))
	fmt.Fprintf(file, "Tag: %s\n", cfg.Tag)
	fmt.Fprintf(file, "----------------------------------------\n")

	// Run benchmarks and capture output
	args := []string{"test", "-bench=.", "-benchmem"}
	if cfg.Quick {
		args = append(args, "-bench=BenchmarkQuick")
	}
	if cfg.Iterations != "1x" && cfg.Iterations != "" {
		args = append(args, fmt.Sprintf("-benchtime=%s", cfg.Iterations))
	}
	args = append(args, "./benchmarks")

	cmd := exec.Command("go", args...)
	cmd.Stdout = io.MultiWriter(file, os.Stdout)
	cmd.Stderr = os.Stderr

	common.PrintInfo("Running benchmarks for baseline...\n")
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("benchmark failed: %w", err)
	}

	common.PrintSuccess("\nBaseline saved to: %s\n", filename)
	return nil
}

func compareWithBaseline(cfg *Config) error {
	if cfg.BaselinePath == "" {
		return fmt.Errorf("baseline file required (use -baseline or specify as argument)")
	}

	if !common.FileExists(cfg.BaselinePath) {
		return fmt.Errorf("baseline file not found: %s", cfg.BaselinePath)
	}

	common.PrintSection("Comparing with Baseline")
	fmt.Printf("Baseline: %s\n\n", cfg.BaselinePath)

	// Run current benchmarks
	currentFile := filepath.Join(cfg.BaselineDir, "current.txt")
	args := []string{"test", "-bench=.", "-benchmem"}
	
	// Check if baseline is quick mode
	baselineContent, err := os.ReadFile(cfg.BaselinePath)
	if err != nil {
		return fmt.Errorf("failed to read baseline: %w", err)
	}
	if strings.Contains(string(baselineContent), "BenchmarkQuick") {
		args = append(args, "-bench=BenchmarkQuick")
	}
	
	args = append(args, "./benchmarks")

	cmd := exec.Command("go", args...)
	output, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("benchmark failed: %w\n%s", err, string(output))
	}

	// Save current results
	if err := os.WriteFile(currentFile, output, 0644); err != nil {
		return fmt.Errorf("failed to write current results: %w", err)
	}

	// Print current results
	fmt.Println("Current benchmark results:")
	fmt.Println(string(output))
	
	common.PrintSection("Comparison Report")

	// Try benchstat first
	if err := runBenchstat(cfg.BaselinePath, currentFile); err != nil {
		// Fall back to simple diff
		common.PrintInfo("benchstat not found, showing simple diff:\n\n")
		if err := showSimpleDiff(cfg.BaselinePath, currentFile); err != nil {
			return fmt.Errorf("failed to show diff: %w", err)
		}
	}

	// Save comparison report
	reportFile := filepath.Join(cfg.BaselineDir, 
		fmt.Sprintf("comparison_%s.txt", time.Now().Format("20060102_150405")))
	
	report := fmt.Sprintf("Comparison Report\n"+
		"Generated: %s\n"+
		"Baseline: %s\n"+
		"Current: %s\n"+
		"================================================================================\n\n",
		time.Now().Format(time.RFC3339),
		cfg.BaselinePath,
		currentFile)
	
	if err := os.WriteFile(reportFile, []byte(report), 0644); err != nil {
		common.PrintError("Failed to save comparison report: %v\n", err)
	} else {
		common.PrintInfo("\nComparison report saved to: %s\n", reportFile)
	}

	return nil
}

func listBaselines(cfg *Config) error {
	common.PrintSection("Available Baselines")

	pattern := filepath.Join(cfg.BaselineDir, "baseline_*.txt")
	files, err := filepath.Glob(pattern)
	if err != nil {
		return fmt.Errorf("failed to list baselines: %w", err)
	}

	if len(files) == 0 {
		fmt.Printf("No baselines found in %s\n", cfg.BaselineDir)
		return nil
	}

	// Sort by modification time (newest first)
	sort.Slice(files, func(i, j int) bool {
		fi, _ := os.Stat(files[i])
		fj, _ := os.Stat(files[j])
		return fi.ModTime().After(fj.ModTime())
	})

	// Display baselines
	for _, file := range files {
		info, err := os.Stat(file)
		if err != nil {
			continue
		}

		// Try to extract tag from file
		tag := extractTagFromBaseline(file)
		
		fmt.Printf("  %s  %8s  %-40s %s\n",
			info.ModTime().Format("2006-01-02 15:04:05"),
			common.FormatSize(info.Size()),
			filepath.Base(file),
			tag)
	}

	fmt.Printf("\nTotal: %d baselines\n", len(files))
	fmt.Printf("\nUsage: dtail-tools benchmark -mode compare <baseline_file>\n")
	
	return nil
}

func cleanBaselines(cfg *Config) error {
	common.PrintSection("Cleaning Old Baselines")

	pattern := filepath.Join(cfg.BaselineDir, "baseline_*.txt")
	files, err := filepath.Glob(pattern)
	if err != nil {
		return fmt.Errorf("failed to list baselines: %w", err)
	}

	if len(files) <= 10 {
		fmt.Println("No old baselines to clean (keeping last 10)")
		return nil
	}

	// Sort by modification time (oldest first)
	sort.Slice(files, func(i, j int) bool {
		fi, _ := os.Stat(files[i])
		fj, _ := os.Stat(files[j])
		return fi.ModTime().Before(fj.ModTime())
	})

	// Remove old files
	toRemove := files[:len(files)-10]
	for _, file := range toRemove {
		fmt.Printf("Removing: %s\n", filepath.Base(file))
		if err := os.Remove(file); err != nil {
			common.PrintError("Failed to remove %s: %v\n", file, err)
		}
	}

	common.PrintSuccess("\nRemoved %d old baselines\n", len(toRemove))
	return nil
}

func extractTagFromBaseline(filename string) string {
	file, err := os.Open(filename)
	if err != nil {
		return ""
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "Tag: ") {
			return strings.TrimPrefix(line, "Tag: ")
		}
		if strings.HasPrefix(line, "----") {
			break
		}
	}
	return ""
}

func runBenchstat(baseline, current string) error {
	cmd := exec.Command("benchstat", baseline, current)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func showSimpleDiff(baseline, current string) error {
	cmd := exec.Command("diff", "-u", baseline, current)
	output, _ := cmd.CombinedOutput()
	fmt.Print(string(output))
	return nil
}