-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (56 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"math"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func primeNumbers() []int {
var primes []int
for i := 2; i < 10000; i++ {
isPrime := true
for j := 2; j <= int(math.Sqrt(float64(i))); j++ {
if i%j == 0 {
isPrime = false
break
}
}
if isPrime {
primes = append(primes, i)
}
}
return primes
}
func PrimeNumbersBenchmark(N int) {
for i := 0; i < N; i++ {
_ = primeNumbers()
}
}
func recordMetrics() {
go func() {
for {
start := time.Now()
N := 500
PrimeNumbersBenchmark(N)
duration := time.Since(start)
opsPerSecond := float64(N) / duration.Seconds()
fmt.Printf("Test finished in %v ms (%0.0f op/s)\n", duration.Milliseconds(), opsPerSecond)
perfTestOps.Set(opsPerSecond)
time.Sleep(300 * time.Second)
}
}()
}
var (
perfTestOps = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cpu_performance_test_ops",
Help: "The number of operations per second the test executed.",
})
)
func main() {
recordMetrics()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}