Skip to content

Commit 248ef3a

Browse files
committed
Add PGO
1 parent 9f78585 commit 248ef3a

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ endif
3030

3131
export CGO_ENABLED ?= 0
3232
BUILD_PKG = ./cmd/catp
33+
BUILD_LDFLAGS=-s -w
34+
BUILD_FLAGS=-trimpath -pgo=./cmd/catp.pgo
3335

3436
-include $(DEVGO_PATH)/makefiles/main.mk
3537
-include $(DEVGO_PATH)/makefiles/lint.mk
@@ -39,5 +41,3 @@ BUILD_PKG = ./cmd/catp
3941

4042
# Add your custom targets here.
4143

42-
## Run tests
43-
test: test-unit

cmd/catp.pgo

12.5 KB
Binary file not shown.

cmd/catp/main.go

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ package main
22

33
import (
44
"bufio"
5+
"bytes"
56
"flag"
67
"fmt"
78
"io"
89
"log"
910
"os"
11+
"runtime/pprof"
1012
"strings"
1113
"time"
1214

1315
"github.com/bool64/progress"
1416
"github.com/klauspost/compress/zstd"
1517
gzip "github.com/klauspost/pgzip"
16-
_ "gitlab.com/rackn/seekable-zstd"
1718
)
1819

1920
type runner struct {
@@ -22,6 +23,9 @@ type runner struct {
2223
readBytes int64
2324
totalBytes int64
2425

26+
grep [][]byte
27+
reverse bool
28+
2529
currentFile *progress.CountingReader
2630
currentTotal int64
2731
}
@@ -44,15 +48,34 @@ func (r *runner) st(s progress.ProgressStatus) string {
4448
return res
4549
}
4650

47-
func readFile(r io.Reader) {
48-
rd := bufio.NewReader(r)
51+
func (r *runner) readFile(rd io.Reader) {
52+
b := bufio.NewReaderSize(rd, 64*1024)
4953

50-
_, err := io.Copy(os.Stdout, rd)
54+
_, err := io.Copy(os.Stdout, b)
5155
if err != nil {
5256
log.Fatal(err)
5357
}
5458
}
5559

60+
func (r *runner) scanFile(rd io.Reader) {
61+
s := bufio.NewScanner(rd)
62+
s.Buffer(make([]byte, 64*1024), 10*1024*1024)
63+
64+
for s.Scan() {
65+
for _, g := range r.grep {
66+
if bytes.Contains(s.Bytes(), g) {
67+
_, _ = os.Stdout.Write(s.Bytes())
68+
69+
break
70+
}
71+
}
72+
}
73+
74+
if err := s.Err(); err != nil {
75+
log.Fatal(err)
76+
}
77+
}
78+
5679
func (r *runner) cat(filename string) {
5780
file, err := os.Open(filename)
5881
if err != nil {
@@ -75,6 +98,10 @@ func (r *runner) cat(filename string) {
7598
}
7699
}
77100

101+
if r.reverse {
102+
103+
}
104+
78105
r.pr.Start(func(t *progress.Task) {
79106
t.TotalBytes = func() int64 {
80107
return r.totalBytes
@@ -86,16 +113,49 @@ func (r *runner) cat(filename string) {
86113
t.Task = filename
87114
t.Continue = true
88115
})
89-
readFile(rd)
116+
117+
if len(r.grep) > 0 || r.reverse {
118+
r.scanFile(rd)
119+
} else {
120+
r.readFile(rd)
121+
}
90122

91123
r.pr.Stop()
92124
r.readBytes += r.currentFile.Bytes()
93125
}
94126

95127
func main() {
128+
grep := flag.String("grep", "", "grep pattern, may contain multiple patterns separated by \\|")
129+
cpuProfile := flag.String("dbg-cpu-prof", "", "write first 10 seconds of CPU profile to file")
130+
96131
flag.Parse()
97132

133+
if *cpuProfile != "" {
134+
f, err := os.Create(*cpuProfile) //nolint:gosec
135+
if err != nil {
136+
log.Fatal(err)
137+
}
138+
139+
if err = pprof.StartCPUProfile(f); err != nil {
140+
log.Fatal(err)
141+
}
142+
143+
go func() {
144+
time.Sleep(10 * time.Second)
145+
pprof.StopCPUProfile()
146+
println("CPU profile written to", *cpuProfile)
147+
}()
148+
}
149+
98150
r := &runner{}
151+
r.reverse = *reverse
152+
153+
if *grep != "" {
154+
for _, s := range strings.Split(*grep, "\\|") {
155+
r.grep = append(r.grep, []byte(s))
156+
}
157+
}
158+
99159
r.sizes = make(map[string]int64)
100160
r.pr = &progress.Progress{
101161
Interval: 5 * time.Second,

cmd/go.mod

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ require (
66
github.com/bool64/progress v0.1.0
77
github.com/klauspost/compress v1.16.5
88
github.com/klauspost/pgzip v1.2.5
9-
gitlab.com/rackn/seekable-zstd v0.8.1
10-
)
11-
12-
require (
13-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
14-
gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93 // indirect
159
)

cmd/go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc=
22
github.com/bool64/progress v0.1.0 h1:khsGInrTrQMyskfcRTE9gP982koSqKobYFJ977JLxhA=
33
github.com/bool64/progress v0.1.0/go.mod h1:XwIA2+r2sEKxIRa6TFR0FqD3GHt0wz4dk4IL5CwbuKo=
4-
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
5-
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
6-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
74
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
85
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
96
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
107
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
11-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12-
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
13-
gitlab.com/rackn/seekable-zstd v0.8.1 h1:vemYjUK3hr1uSipdVQGKX/SOOlWUr1kXTfjfgLfCPVE=
14-
gitlab.com/rackn/seekable-zstd v0.8.1/go.mod h1:9z8nf3qNXOi73VRm7KQgTmI3T0tz9YzDKKL7fzEBz9M=
15-
gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93 h1:lXoXk/e9YrtTyWzNZs1ak/ijpwZQDaJLEwKCjhp/dCw=
16-
gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93/go.mod h1:pXhP0EyrEy0pGf2DW4vTKub/As/UiamLFaZ1Q9YaFTs=
17-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)