Skip to content

Commit

Permalink
Add cmp cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
schattian committed Jan 17, 2022
1 parent 97e2fa4 commit 2323773
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions cmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os"

"github.com/docker/docker/client"
"github.com/mitchellh/cli"
"github.com/pkg/errors"
"github.com/schattian/bencher/internal/bencher"
"go.etcd.io/bbolt"
"golang.org/x/perf/benchstat"
)

type cmpCmd struct {
docker *client.Client
}

func prepareCmp() (cli.Command, error) {
docker, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
pruneContainers(context.Background(), docker)
return &cmpCmd{docker: docker}, nil
}

func (cmd *cmpCmd) Run(args []string) int {
if len(args) < 2 {
return cli.RunResultHelp
}
db, err := initDB()
if err != nil {
fmt.Printf("err initDB: %v", err)
return 1
}
defer db.Close()
err = cmd.cmp(db, args...)
if err != nil {
log.Fatal(err)
}
return 0
}

func getJobs(db *bbolt.DB, versions ...string) (jobs []*bencher.Job, err error) {
err = db.View(func(tx *bbolt.Tx) error {
bJobs := tx.Bucket(bencher.KeyJob)
if bJobs == nil {
return nil
}
for _, v := range versions {
b := bJobs.Get([]byte(v))
if b == nil {
continue
}
j := &bencher.Job{}
json.Unmarshal(b, j)
jobs = append(jobs, j)
}
return nil
})
return
}

func (cmd *cmpCmd) cmp(db *bbolt.DB, versions ...string) error {
jobs, err := getJobs(db, versions...)
if err != nil {
return errors.Wrap(err, "getJobs")
}
c := &benchstat.Collection{}
for _, job := range jobs {
if job.Status() != "done" {
continue
}
fmt.Println(job.Stdout)
err := c.AddFile(job.Version, bytes.NewBufferString(job.Stdout))
if err != nil {
return errors.Wrap(err, "benchstat.Collection.AddFile: %v")
}
}
var buf bytes.Buffer
benchstat.FormatText(&buf, c.Tables())
_, err = os.Stdout.Write(buf.Bytes())
return errors.Wrap(err, "os.Stdout.Write")
}

func (cmd *cmpCmd) Synopsis() string {
return `compare two or more versions`
}

func (cmd *cmpCmd) Help() string {
return `Usage: bencher cmp <version1> <version2> [version3] [...]
Compare two or more versions with benchstat`
}

0 comments on commit 2323773

Please sign in to comment.