/
main.go
121 lines (101 loc) 路 2.3 KB
/
main.go
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
package main
import (
"fmt"
"io"
"os"
"runtime/pprof"
"time"
"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/sessions"
"github.com/cfoust/cy/pkg/sessions/search"
"github.com/alecthomas/kong"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
B uint64 = 1
KB = B << 10
MB = KB << 10
GB = MB << 10
)
var CLI struct {
Query string `help:"" optional:""`
Limit int `help:"Limit the number of events."`
Borg struct {
Path string `arg:"" name:"path" help:".borg file to search inside." type:"path"`
} `cmd:"" help:"Search inside a borg file."`
Fake struct{} `cmd:"" help:"Search inside generated data."`
}
func main() {
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
log.Logger = log.Output(consoleWriter)
ctx := kong.Parse(&CLI,
kong.Name("perf"),
kong.Description("A small utility to benchmark search performance."),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
Summary: true,
}))
var events []sessions.Event
switch ctx.Command() {
case "borg <path>":
reader, err := sessions.Open(CLI.Borg.Path)
if err != nil {
panic(err)
}
for {
event, err := reader.Read()
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
panic(err)
}
events = append(events, event)
}
case "fake":
sim := sessions.NewSimulator()
sim.Add(
emu.LineFeedMode,
geom.DEFAULT_SIZE,
)
var numBytes, desiredBytes uint64
desiredBytes = 2 * MB
i := 0
numMatches := 0
for numBytes < desiredBytes {
s := "foo"
if (i % 100) == 0 {
numMatches++
s = "bar"
}
sim.Add(s)
numBytes += uint64(len([]byte(s)))
i++
}
fmt.Printf("%d matches in %d bytes", numMatches, desiredBytes)
events = sim.Events()
default:
panic(ctx.Command())
}
if CLI.Limit != 0 {
events = events[:CLI.Limit]
}
log.Info().Msgf("searching for %s in %d events", CLI.Query, len(events))
f, err := os.Create("search.prof")
if err != nil {
panic(err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
results, err := search.Search(events, CLI.Query, nil)
if err != nil {
panic(err)
}
log.Info().Msgf("found %d results", len(results))
}