Created
October 17, 2016 21:40
-
-
Save desa/6cc4e093afc06cc72450b7ca8f2233d2 to your computer and use it in GitHub Desktop.
Query Perf Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| "time" | |
| "github.com/influxdata/influxdb/client/v2" | |
| ) | |
| func main() { | |
| // Make client | |
| c, err := client.NewHTTPClient(client.HTTPConfig{ | |
| Addr: "http://localhost:8086", | |
| }) | |
| if err != nil { | |
| fmt.Println("Error creating InfluxDB Client: ", err.Error()) | |
| } | |
| defer c.Close() | |
| fixedQueries := []string{ | |
| "SELECT count(n) FROM ctr", | |
| "SELECT count(n) FROM ctr group by *", | |
| "SELECT max(n) FROM ctr", | |
| "SELECT max(n) FROM ctr group by *", | |
| "SELECT top(n,10) FROM ctr group by *", | |
| } | |
| liveQueries := []string{ | |
| "SELECT count(n) FROM ctr where time > now() - 1m", | |
| "SELECT count(n) FROM ctr where time > now() - 1m group by time(10s), *", | |
| "SELECT max(n) FROM ctr where time > now() - 1m", | |
| "SELECT max(n) FROM ctr where time > now() - 1m group by time(10s), *", | |
| "SELECT top(n,10) FROM ctr where time > now() - 1m group by time(10s), *", | |
| } | |
| queryResults := map[string][]time.Duration{} | |
| for _, q := range fixedQueries { | |
| for i := 0; i < 10; i++ { | |
| latNs, err := query(q, c) | |
| if err != nil { | |
| continue | |
| } | |
| queryResults[q] = append(queryResults[q], latNs) | |
| } | |
| } | |
| fmt.Println("Start stress...") | |
| time.Sleep(30 * time.Second) | |
| for _, q := range liveQueries { | |
| for i := 0; i < 10; i++ { | |
| latNs, err := query(q, c) | |
| if err != nil { | |
| continue | |
| } | |
| queryResults[q] = append(queryResults[q], latNs) | |
| } | |
| } | |
| for q, ts := range queryResults { | |
| outputStats(q, ts) | |
| } | |
| } | |
| func outputStats(q string, ts []time.Duration) { | |
| fmt.Printf("Query: '%v'\n", q) | |
| fmt.Printf("max: %v\n", max(ts)) | |
| fmt.Printf("avg: %v\n", avg(ts)) | |
| fmt.Printf("min: %v\n", min(ts)) | |
| fmt.Println() | |
| } | |
| func max(ts []time.Duration) (m time.Duration) { | |
| for _, t := range ts { | |
| if t > m { | |
| m = t | |
| } | |
| } | |
| return | |
| } | |
| func min(ts []time.Duration) (m time.Duration) { | |
| m = time.Duration(math.MaxInt64) | |
| for _, t := range ts { | |
| if t < m { | |
| m = t | |
| } | |
| } | |
| return | |
| } | |
| func avg(ts []time.Duration) (a time.Duration) { | |
| for _, t := range ts { | |
| a += t | |
| } | |
| a = a / time.Duration(len(ts)) | |
| return | |
| } | |
| func query(qry string, c client.Client) (time.Duration, error) { | |
| start := time.Now() | |
| q := client.NewQuery(qry, "stress", "ns") | |
| response, err := c.Query(q) | |
| if err != nil { | |
| return time.Since(start), err | |
| } | |
| if err := response.Error(); err != nil { | |
| return time.Since(start), err | |
| } | |
| return time.Since(start), nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment