...

Source file src/golang.conradwood.net/tests/prometheus/promtest.go

Documentation: golang.conradwood.net/tests/prometheus

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"golang.conradwood.net/go-easyops/prometheus"
     7  	"golang.conradwood.net/go-easyops/server"
     8  	"golang.conradwood.net/go-easyops/utils"
     9  	"time"
    10  )
    11  
    12  type Timer struct {
    13  	start time.Time
    14  	txt   string
    15  }
    16  
    17  func NewTimer(txt string) *Timer {
    18  	t := &Timer{txt: txt, start: time.Now()}
    19  	fmt.Printf("Starting \"%s\"...\n", txt)
    20  	return t
    21  }
    22  func (t *Timer) Finish(err error) {
    23  	utils.Bail(fmt.Sprintf("\"%s\" failed", t.txt), err)
    24  	d := time.Since(t.start)
    25  	fmt.Printf("Duration of \"%s\": %0.2f seconds\n", t.txt, d.Seconds())
    26  }
    27  
    28  func main() {
    29  	flag.Parse()
    30  	fmt.Printf("Prometheus go-easyops registry test code\n")
    31  	server.StartFakeService("foo.Foo")
    32  	i := 0
    33  	ctr := prometheus.NewCounterVec(
    34  		prometheus.CounterOpts{
    35  			Name: fmt.Sprintf("prometheus_test_metric_%d", i),
    36  			Help: fmt.Sprintf("desc_prometheus_test_metric_%d", i),
    37  		},
    38  		[]string{"foo"})
    39  	prometheus.MustRegister(ctr)
    40  	prometheus.GetGatherer().Expiry = 0
    41  	t := NewTimer("Gather() - plain")
    42  	g, err := prometheus.GetRegistry().Gather()
    43  	t.Finish(err)
    44  	t = NewTimer("Gather() - arraylen")
    45  	fmt.Printf("Got %d metric familes\n", len(g))
    46  	t.Finish(nil)
    47  
    48  	t = NewTimer("Incrementing counters")
    49  	for i := 0; i < 30000; i++ {
    50  		l := prometheus.Labels{"foo": fmt.Sprintf("%d", i)}
    51  		ctr.With(l).Inc()
    52  	}
    53  	t.Finish(nil)
    54  
    55  	t = NewTimer("Gather() - with labels")
    56  	g, err = prometheus.GetRegistry().Gather()
    57  	t.Finish(err)
    58  	fmt.Printf("Got %d metric familes\n", len(g))
    59  	select {}
    60  }
    61  

View as plain text