Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 benchmark sampled logs #130

Merged
merged 9 commits into from
Nov 30, 2024
101 changes: 101 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package humanlog

import (
"bytes"
"compress/gzip"
"context"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"testing"

"github.com/humanlogio/humanlog/pkg/sink/bufsink"
"github.com/stretchr/testify/require"
)

func BenchmarkHarness(b *testing.B) {
ctx := context.Background()
root := "test/benchmark"
des, err := os.ReadDir(root)
if err != nil {
b.Fatal(err)
}

for _, de := range des {
if !de.IsDir() {
continue
}

dir := filepath.Join(root, de.Name())
fileName, err := findfirstMatchedFileName(dir, "*.gz")
require.NoError(b, err)

b.ResetTimer()
testCase := dir
b.Run(testCase, func(bb *testing.B) {
path := filepath.Join(dir, fileName)
f, err := os.Open(path)
require.NoError(bb, err)
defer f.Close()

gzipReader, err := gzip.NewReader(f)
require.NoError(bb, err)

buf := make([]byte, 0, 100*1024) // set initial capacity to 100kb
err = loadAll(&buf, gzipReader)
aybabtme marked this conversation as resolved.
Show resolved Hide resolved

src := bytes.NewBuffer(buf)
require.NoError(bb, err)

sink := bufsink.NewSizedBufferedSink(len(buf), nil) // size must be greater than src size
aybabtme marked this conversation as resolved.
Show resolved Hide resolved
opt := DefaultOptions()

bb.SetBytes(int64(src.Len()))
bb.StartTimer()
err = Scan(ctx, src, sink, opt)
bb.StopTimer()

require.NoError(bb, err)
})
}
}

func findfirstMatchedFileName(dirPath string, pattern string) (string, error) {
firstMatched := ""
walkError := filepath.Walk(dirPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fileName := filepath.Base(path)
match, err := filepath.Match(pattern, fileName)
if err != nil {
return err
}
if match {
firstMatched = fileName
return filepath.SkipAll
}
return nil
})
return firstMatched, walkError
}

func loadAll(dest *[]byte, r io.Reader) error {
for {
temp := make([]byte, 1024)
n, err := r.Read(temp)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return err
}
*dest = append(*dest, temp[:n]...)
}
return nil
}
Loading