mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-16 16:23:30 +00:00
PMM-1429: generate table of supported versions
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/template"
|
||||
"time"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/percona/percona-toolkit/src/go/lib/tutil"
|
||||
@@ -232,16 +236,16 @@ func TestStatsAll(t *testing.T) {
|
||||
s := New(fp)
|
||||
|
||||
for _, file := range files {
|
||||
doc := proto.SystemProfile{}
|
||||
err = tutil.LoadBson(dir+file.Name(), &doc)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot load sample %s: %s", dir+file.Name(), err)
|
||||
}
|
||||
doc := proto.SystemProfile{}
|
||||
err = tutil.LoadBson(dir+file.Name(), &doc)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot load sample %s: %s", dir+file.Name(), err)
|
||||
}
|
||||
|
||||
err = s.Add(doc)
|
||||
if err != nil {
|
||||
t.Errorf("Error processing doc: %s\n", err.Error())
|
||||
}
|
||||
err = s.Add(doc)
|
||||
if err != nil {
|
||||
t.Errorf("Error processing doc: %s\n", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
got := s.Queries()
|
||||
@@ -261,3 +265,197 @@ func TestStatsAll(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvailableMetrics(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var err error
|
||||
dirExpect := vars.RootPath + samples + "/expect/available_metrics/"
|
||||
dir := vars.RootPath + samples + "/doc/out/"
|
||||
|
||||
versions := []string{
|
||||
"2.6.12",
|
||||
"3.0.15",
|
||||
"3.2.16",
|
||||
"3.4.7",
|
||||
"3.5.11",
|
||||
}
|
||||
|
||||
samples := []string{
|
||||
"aggregate",
|
||||
"count",
|
||||
"count_with_query",
|
||||
"delete",
|
||||
"delete_all",
|
||||
"distinct",
|
||||
"find",
|
||||
"find_andrii",
|
||||
"find_empty",
|
||||
"findandmodify",
|
||||
"geonear",
|
||||
"group",
|
||||
"insert",
|
||||
"mapreduce",
|
||||
"update",
|
||||
"explain",
|
||||
"eval",
|
||||
}
|
||||
|
||||
type sp struct {
|
||||
DocsExamined *int `bson:"docsExamined" json:",omitempty"`
|
||||
NscannedObjects *int `bson:"nscannedObjects" json:",omitempty"`
|
||||
Millis *int `bson:"millis" json:",omitempty"`
|
||||
Nreturned *int `bson:"nreturned" json:",omitempty"`
|
||||
ResponseLength *int `bson:"responseLength" json:",omitempty"`
|
||||
}
|
||||
|
||||
data := map[string]map[string]sp{}
|
||||
|
||||
for _, sample := range samples {
|
||||
for _, v := range versions {
|
||||
f := sample + "_" + v
|
||||
s := sp{}
|
||||
err = tutil.LoadBson(dir+f, &s)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot load sample %s: %s", dir+f, err)
|
||||
}
|
||||
|
||||
if data[sample] == nil {
|
||||
data[sample] = map[string]sp{}
|
||||
}
|
||||
data[sample][v] = s
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("available_metrics", func(t *testing.T) {
|
||||
got := data
|
||||
fExpect := dirExpect + "available_metrics"
|
||||
if tutil.ShouldUpdateSamples() {
|
||||
err := tutil.WriteJson(fExpect, got)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot update samples: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
expect := map[string]map[string]sp{}
|
||||
err = tutil.LoadJson(fExpect, &expect)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot load expected data %s: %s", fExpect, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, expect) {
|
||||
t.Errorf("s.Queries() = %s, want %s", got, expect)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
t.Run("cmd_metric", func(t *testing.T) {
|
||||
got := map[string]map[string][]string{}
|
||||
for s := range data {
|
||||
for v := range data[s] {
|
||||
if got[s] == nil {
|
||||
got[s] = map[string][]string{}
|
||||
}
|
||||
if data[s][v].Millis != nil {
|
||||
got[s]["Query Time"] = append(got[s]["Query Time"], v)
|
||||
}
|
||||
if data[s][v].DocsExamined != nil || data[s][v].NscannedObjects != nil {
|
||||
got[s]["Docs Scanned"] = append(got[s]["Docs Scanned"], v)
|
||||
}
|
||||
if data[s][v].Nreturned != nil {
|
||||
got[s]["Docs Returned"] = append(got[s]["Docs Returned"], v)
|
||||
}
|
||||
if data[s][v].ResponseLength != nil {
|
||||
got[s]["Bytes Sent"] = append(got[s]["Bytes Sent"], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
metrics := []string{
|
||||
"Query Time",
|
||||
"Docs Scanned",
|
||||
"Docs Returned",
|
||||
"Bytes Sent",
|
||||
}
|
||||
for cmd := range got {
|
||||
for metric := range got[cmd] {
|
||||
if len(got[cmd][metric]) == len(versions) {
|
||||
got[cmd][metric] = []string{"yes"}
|
||||
} else {
|
||||
sort.Strings(got[cmd][metric])
|
||||
}
|
||||
}
|
||||
|
||||
for _, metric := range metrics {
|
||||
if len(got[cmd][metric]) == 0 {
|
||||
got[cmd][metric] = []string{"no"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fExpect := dirExpect + "cmd_metric"
|
||||
if tutil.ShouldUpdateSamples() {
|
||||
err := tutil.WriteJson(fExpect, got)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot update samples: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
expect := map[string]map[string][]string{}
|
||||
err = tutil.LoadJson(fExpect, &expect)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot load expected data %s: %s", fExpect, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, expect) {
|
||||
t.Errorf("s.Queries() = %s, want %s", got, expect)
|
||||
}
|
||||
|
||||
data := got
|
||||
t.Run("md", func(t *testing.T) {
|
||||
type result struct {
|
||||
Metrics []string
|
||||
Samples []string
|
||||
Data map[string]map[string][]string
|
||||
}
|
||||
r := result{
|
||||
Metrics: metrics,
|
||||
Samples: samples,
|
||||
Data: data,
|
||||
}
|
||||
sort.Strings(r.Metrics)
|
||||
sort.Strings(r.Samples)
|
||||
|
||||
tmpl := template.New("")
|
||||
tmpl = tmpl.Funcs(template.FuncMap{"join": strings.Join})
|
||||
tmpl, err := tmpl.Parse(`| | {{range .Metrics}}{{.}} | {{end}}
|
||||
| - | {{range .Metrics}}- | {{end}}{{range $s := .Samples}}
|
||||
| {{$s}} | {{range $m := $.Metrics}}{{join (index $.Data $s $m) ", "}} | {{end}}{{end}}`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var got bytes.Buffer
|
||||
err = tmpl.Execute(&got, r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fExpect := dirExpect + "cmd_metric.md"
|
||||
if tutil.ShouldUpdateSamples() {
|
||||
err = ioutil.WriteFile(fExpect, got.Bytes(), 0777)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot update samples: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
buf, err := ioutil.ReadFile(fExpect)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot load expected data %s: %s", fExpect, err)
|
||||
}
|
||||
expect := string(buf)
|
||||
|
||||
if !reflect.DeepEqual(got, expect) {
|
||||
t.Errorf("s.Queries() = %s, want %s", got, expect)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user