Skip to content

Instantly share code, notes, and snippets.

@defp
Created March 25, 2014 02:21
Show Gist options
  • Save defp/9754143 to your computer and use it in GitHub Desktop.
Save defp/9754143 to your computer and use it in GitHub Desktop.
influxdb with go-metrics
package main
import (
"log"
"math/rand"
"runtime"
"time"
// "github.com/rcrowley/go-metrics"
// "github.com/rcrowley/go-metrics/influxdb"
"./go-metrics"
"./go-metrics/influxdb"
)
func allocateAndSum(arraySize int) int {
arr := make([]int, arraySize, arraySize)
for i, _ := range arr {
arr[i] = rand.Int()
}
time.Sleep(time.Duration(rand.Intn(3000)) * time.Millisecond)
result := 0
for _, v := range arr {
result += v
}
//log.Printf("Array size is: %d, sum is: %d\n", arraySize, result)
return result
}
var m = &runtime.MemStats{}
func doSomeJob(numRoutines int) {
for {
runtime.ReadMemStats(m)
// log.Printf("heapAlloc: %d MB", m.HeapAlloc/1024/1024)
// log.Printf("Alloc: %d MB", m.Alloc/1024/1024)
// log.Println("num goroutine:", runtime.NumGoroutine())
for i := 0; i < numRoutines; i++ {
go allocateAndSum(rand.Intn(1024) * 1024)
}
log.Printf("All %d routines started\n", numRoutines)
time.Sleep(1000 * time.Millisecond)
runtime.GC()
}
}
func main() {
go doSomeJob(20)
r := metrics.NewRegistry()
metrics.RegisterDebugGCStats(r)
go metrics.CaptureDebugGCStats(r, 5e9)
metrics.RegisterRuntimeMemStats(r)
go metrics.CaptureRuntimeMemStats(r, 5e9)
influxdb.Influxdb(r, 1*time.Second, &influxdb.Config{
Host: "127.0.0.1:8086",
Database: "metrics",
Username: "root",
Password: "root",
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment