Skip to content

Instantly share code, notes, and snippets.

@jwilder
Created June 26, 2015 16:04
Show Gist options
  • Save jwilder/a71cbaa7a9a06db868ba to your computer and use it in GitHub Desktop.
Save jwilder/a71cbaa7a9a06db868ba to your computer and use it in GitHub Desktop.
Load Test
package main
import (
crand "crypto/rand"
"encoding/base64"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"time"
"github.com/influxdb/influxdb/client"
)
func main() {
cl, err := client.NewClient(client.Config{
URL: url.URL{
Scheme: "http",
Host: "localhost:8086",
},
})
if err != nil {
fmt.Printf("Could not create client %s", err)
return
}
if _, v, e := cl.Ping(); e != nil {
fmt.Printf("Failed to connect to %s\n", cl.Addr())
} else {
fmt.Printf("Connected to %s version %s\n\n", cl.Addr(), v)
}
rand.Seed(time.Now().Unix())
var n int
//times := map[time.Time]struct{}{}
for x := 0; x < 100; x++ {
app := token(10)
bp := client.BatchPoints{
Database: "test",
Points: []client.Point{},
}
for z := 0; z < 10000; z++ {
t := time.Unix(time.Now().Unix()-int64(rand.Intn(60*60*24*30)), 0)
// if _, ok := times[t]; ok {
// println("dup ts", t.String())
// }
// times[t] = struct{}{}
bp.Points = append(bp.Points, client.Point{
Measurement: "events",
Time: t,
Tags: map[string]string{
"app": app,
},
Fields: map[string]interface{}{
"value": 1,
"name": token(2),
},
})
}
n += len(bp.Points)
write(cl, bp)
fmt.Printf("inserted events for app: %s: %d\n", app, n)
//break
}
}
func write(cl *client.Client, bp client.BatchPoints) {
res, err := cl.Write(bp)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if res != nil && res.Err != nil {
fmt.Println(res.Err.Error())
os.Exit(1)
}
fmt.Printf("%v\n", res)
}
func token(length int) string {
bytes := make([]byte, length)
crand.Read(bytes)
return strings.TrimRight(base64.URLEncoding.EncodeToString(bytes), "=")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment