Skip to content

Commit

Permalink
added server meta in results and in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eze-kiel committed Apr 22, 2021
1 parent cc47b4b commit 65c2770
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/slowql-digest/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type results struct {
Date time.Time `json:"date"`
TotalDuration time.Duration `json:"total_duration"`
Hash string `json:"hash"`
ServerMeta serverMeta `json:"server_meta"`
Data []statistics `json:"data"`
}

Expand Down
71 changes: 67 additions & 4 deletions cmd/slowql-digest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/devops-works/slowql"
"github.com/devops-works/slowql/query"
"github.com/devops-works/slowql/server"
. "github.com/logrusorgru/aurora"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -60,6 +61,19 @@ type statistics struct {
QueryTimes []float64
}

type serverMeta struct {
Binary string
Port int
Socket string
Version string
VersionShort string
VersionDescription string

Duration time.Duration
RealDuration time.Duration
Bytes int
}

var orders = []string{"bytes_sent", "calls", "concurrency", "killed", "lock_time",
"max_time", "mean_time", "min_time", "p50", "p90", "query_time", "random", "rows_examined", "rows_sent"}

Expand Down Expand Up @@ -138,7 +152,7 @@ func main() {
a.logger.Fatalf("cannot sort results: %s", err)
}
}
showResults(stats, o.order, o.top, o.dec, res.TotalDuration)
showResults(stats, res.ServerMeta, o.order, o.top, o.dec)
return
}
a.logger.Info("cache will not be used")
Expand Down Expand Up @@ -166,8 +180,8 @@ func main() {

var q query.Query
var wg sync.WaitGroup
firstPass := true
var realStart, realEnd time.Time
firstPass := true
a.p = slowql.NewParser(a.kind, a.fd)
a.logger.Debug("slowql parser created")
a.logger.Debug("query analysis started")
Expand All @@ -194,16 +208,24 @@ func main() {
a.logger.Infof("parsed %d queries", a.queriesNumber)
a.logger.Infof("found %d different queries hashs", len(a.res))

srv := a.p.GetServerMeta()
srvMeta := getMeta(srv)
srvMeta.Duration = a.digestDuration

var res []statistics
for _, val := range a.res {
res = append(res, val)
srvMeta.Bytes += val.CumBytesSent
}

realDuration := realEnd.Sub(realStart)
srvMeta.RealDuration = realDuration

res, err = computeStats(res, realDuration)
if err != nil {
a.logger.Errorf("cannot compute statistics: %s. This can lead to inacurrate stats")
}

res, err = sortResults(res, o.order, o.dec)
if err != nil {
a.logger.Errorf("cannot sort results: %s", err)
Expand All @@ -214,14 +236,15 @@ func main() {
}
}

showResults(res, o.order, o.top, o.dec, realDuration)
showResults(res, srvMeta, o.order, o.top, o.dec)
if !o.nocache {
a.logger.Info("saving results in cache file")
cache := results{
File: o.logfile,
Date: time.Now(),
TotalDuration: realDuration,
Data: res,
ServerMeta: srvMeta,
}
if err := saveCache(cache); err != nil {
a.logger.Errorf("cannot save results in cache file: %s", err)
Expand All @@ -230,11 +253,40 @@ func main() {
a.logger.Debug("end of program, exiting")
}

func showResults(res []statistics, order string, count int, dec bool, realDuration time.Duration) {
func showResults(res []statistics, sm serverMeta, order string, count int, dec bool) {
howTo := "increasing"
if dec {
howTo = "decreasing"
}

// show server's meta
fmt.Printf(`
=-= Server meta =-=
Binary : %s
Port : %d
Socket : %s
Version : %s
Version short : %s
Version description : %s
Digest duration : %s
Real duration : %s
Bytes handled : %d
`,
sm.Binary,
sm.Port,
sm.Socket,
sm.Version,
sm.VersionShort,
sm.VersionDescription,
sm.Duration,
sm.RealDuration,
sm.Bytes)

// show queries stats
fmt.Printf("\n=-= Queries stats =-=\n")
fmt.Printf("\nSorted by: %s, %s\n", Bold(order), Bold(howTo))
fmt.Printf("Showing top %d queries\n", Bold(count))
for i := 0; i < len(res); i++ {
Expand Down Expand Up @@ -368,3 +420,14 @@ func sortResults(s []statistics, order string, dec bool) ([]statistics, error) {
}
return s, nil
}

func getMeta(srv server.Server) serverMeta {
var sm serverMeta
sm.Binary = srv.Binary
sm.Port = srv.Port
sm.Socket = srv.Socket
sm.Version = srv.Version
sm.VersionShort = srv.VersionShort
sm.VersionDescription = srv.VersionDescription
return sm
}

0 comments on commit 65c2770

Please sign in to comment.