Skip to content

Commit

Permalink
(perf) grinder allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Aug 8, 2024
1 parent a77df24 commit 6d6fc3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ go-watch-logs --file-path=my.log --match='HTTP/1.1" 50' --every=60
## Performance Notes

```sh
BenchmarkReadFileAndMatchErrors-10 10215 122666 ns/op 8684 B/op 53 allocs/op
BenchmarkLoadAndSaveState-10 15241 82295 ns/op 10562 B/op 38 allocs/op
BenchmarkLogRotation-10 9712 143824 ns/op 9707 B/op 74 allocs/op
BenchmarkReadFileAndMatchErrors-10 969 1173870 ns/op 12920 B/op 146 allocs/op
BenchmarkLoadAndSaveState-10 5296 230536 ns/op 9179 B/op 180 allocs/op
BenchmarkLogRotation-10 1036 1175464 ns/op 12930 B/op 146 allocs/op
```


Expand Down
23 changes: 16 additions & 7 deletions pkg/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"crypto/sha256"
"sync"

"github.com/gravwell/gravwell/v3/timegrinder"
)
Expand All @@ -24,17 +25,25 @@ func Truncate(s string, n int) string {
return s[:n] + "..."
}

func SearchDate(input string) string {
var (
tg *timegrinder.TimeGrinder
once sync.Once
)

func initTimeGrinder() {
cfg := timegrinder.Config{}
tg, err := timegrinder.NewTimeGrinder(cfg)
var err error
tg, err = timegrinder.NewTimeGrinder(cfg)
if err != nil {
return ""
panic(err)
}
}

func SearchDate(input string) string {
once.Do(initTimeGrinder)

ts, ok, err := tg.Extract([]byte(input))
if err != nil {
return ""
}
if !ok {
if err != nil || !ok {
return ""
}
return ts.Format("2006-01-02 15:04:05")
Expand Down

0 comments on commit 6d6fc3f

Please sign in to comment.