Skip to content

Commit

Permalink
Merge pull request #225 from klauspost/add-cli-benchmark
Browse files Browse the repository at this point in the history
Add CLI benchmarks
  • Loading branch information
pierrec authored Jan 12, 2025
2 parents 6945807 + eb139b7 commit 1a4bbae
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
42 changes: 42 additions & 0 deletions cmd/lz4c/compress.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"os"
"runtime"
"sync/atomic"
"time"

"code.cloudfoundry.org/bytefmt"
"github.com/schollz/progressbar/v3"
Expand All @@ -26,6 +29,7 @@ func Compress(fs *flag.FlagSet) cmdflag.Handler {
fs.UintVar(&level, "l", 0, "compression level (0=fastest)")
var concurrency int
fs.IntVar(&concurrency, "c", -1, "concurrency (default=all CPUs")
bench := fs.Int("bench", 0, "Run benchmark n times. No output will be written")

var lvl lz4.CompressionLevel
switch level {
Expand Down Expand Up @@ -87,6 +91,33 @@ func Compress(fs *flag.FlagSet) cmdflag.Handler {
if err != nil {
return fidx, err
}
if *bench > 0 {
fmt.Print("Reading ", filename, "...")
input, err := io.ReadAll(file)
if err != nil {
return fidx, err
}
file.Close()
for i := 0; i < *bench; i++ {
fmt.Print("\nCompressing...")
runtime.GC()
start := time.Now()
counter := wCounter{out: io.Discard}
zw.Reset(&counter)
_, err := io.Copy(zw, bytes.NewReader(input))
if err != nil {
return fidx, err
}
output := counter.n
elapsed := time.Since(start)
ms := elapsed.Round(time.Millisecond)
mbPerSec := (float64(len(input)) / 1e6) / (float64(elapsed) / (float64(time.Second)))
pct := float64(output) * 100 / float64(len(input))
fmt.Printf(" %d -> %d [%.02f%%]; %v, %.01fMB/s", len(input), output, pct, ms, mbPerSec)
}
fmt.Println("")
continue
}
finfo, err := file.Stat()
if err != nil {
return fidx, err
Expand Down Expand Up @@ -147,3 +178,14 @@ func Compress(fs *flag.FlagSet) cmdflag.Handler {
return len(args), nil
}
}

type wCounter struct {
n int
out io.Writer
}

func (w *wCounter) Write(p []byte) (n int, err error) {
n, err = w.out.Write(p)
w.n += n
return n, err
}
32 changes: 31 additions & 1 deletion cmd/lz4c/uncompress.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"os"
"runtime"
"strings"
"time"

"github.com/schollz/progressbar/v3"

Expand All @@ -14,7 +17,8 @@ import (
)

// Uncompress uncompresses a set of files or from stdin to stdout.
func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
func Uncompress(fs *flag.FlagSet) cmdflag.Handler {
bench := fs.Int("bench", 0, "Run benchmark n times. No output will be written")
return func(args ...string) (int, error) {
zr := lz4.NewReader(nil)

Expand All @@ -31,6 +35,32 @@ func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
if err != nil {
return fidx, err
}

if *bench > 0 {
fmt.Print("Reading ", zfilename, "...")
compressed, err := io.ReadAll(zfile)
if err != nil {
return fidx, err
}
zfile.Close()
for i := 0; i < *bench; i++ {
fmt.Print("\nDecompressing...")
runtime.GC()
start := time.Now()
zr.Reset(bytes.NewReader(compressed))
output, err := io.Copy(io.Discard, zr)
if err != nil {
return fidx, err
}
elapsed := time.Since(start)
ms := elapsed.Round(time.Millisecond)
mbPerSec := (float64(output) / 1e6) / (float64(elapsed) / (float64(time.Second)))
pct := float64(output) * 100 / float64(len(compressed))
fmt.Printf(" %d -> %d [%.02f%%]; %v, %.01fMB/s", len(compressed), output, pct, ms, mbPerSec)
}
fmt.Println("")
continue
}
zinfo, err := zfile.Stat()
if err != nil {
return fidx, err
Expand Down

0 comments on commit 1a4bbae

Please sign in to comment.