Skip to content

Instantly share code, notes, and snippets.

@xiam
Last active August 30, 2016 13:32
Show Gist options
  • Save xiam/c61c53f2e463c344824ce563ed39eec8 to your computer and use it in GitHub Desktop.
Save xiam/c61c53f2e463c344824ce563ed39eec8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"math/rand"
"time"
"github.com/influxdata/influxdb/client/v2"
)
const (
MyDB = "square_holes"
)
func main() {
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
})
if err != nil {
log.Fatalln("Error: ", err)
}
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: MyDB,
Precision: "s",
})
if err != nil {
log.Fatalln("Error: ", err)
}
for i := 0; i < 1e5; i++ {
// Create a point and add to batch
tags := map[string]string{"ip": randomIP()}
fields := map[string]interface{}{
"value": 1,
}
pt, err := client.NewPoint("visitors", tags, fields, randomTime())
if err != nil {
log.Fatalln("Error: ", err)
}
bp.AddPoint(pt)
// Write the batch
err = c.Write(bp)
if err != nil {
log.Fatalln("Write: ", err)
}
}
}
func randomTime() time.Time {
now := time.Now()
span := 3600 * 24 * 60
return now.Add(-time.Duration(rand.Intn(span)) * time.Second)
}
func randomIP() string {
return fmt.Sprintf("%d.%d.%d.%d",
100+rand.Intn(155),
100+rand.Intn(155),
100+rand.Intn(155),
100+rand.Intn(155),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment