-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.go
56 lines (48 loc) · 1.11 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"strconv"
"time"
)
type datapoint struct {
Series int `json:"series"`
Timestamp int64 `json:"timestamp"`
Value int `json:"value"`
}
func main() {
http.HandleFunc("/", handler)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
panic("expected http.ResponseWriter to be an http.Flusher")
}
// How many different metrics to generate
numSeries := 1
numSeriesParam := r.URL.Query().Get("numSeries")
if numSeriesParam != "" {
numSeries, _ = strconv.Atoi(numSeriesParam)
}
ticker := time.NewTicker(50 * time.Millisecond)
for {
select {
case t := <-ticker.C:
for i := 0; i < numSeries; i++ {
currentPoint := &datapoint{
Series: i,
Timestamp: t.UnixNano() / 1000000, // JS likes ms timestamps
Value: rand.Intn(10) + 10*i,
}
j, _ := json.Marshal(currentPoint)
fmt.Fprintf(w, "%s\n", j)
flusher.Flush() // Trigger "chunked" encoding and send a chunk...
}
}
}
}