aboutsummaryrefslogtreecommitdiff
path: root/cmd/sigsum-debug/spam/leaf/stats.go
blob: b66e83a6b61dd3747929dd8cc43bdbac02e573cf (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package leaf

import (
	"fmt"
	"os"
	"time"
)

type samples struct {
	num uint64 // number of samples
	sum uint64 // total query time (ms)
	min uint64 // minimum query time (ms)
	max uint64 // maximum query time (ms)
}

func newSamples() samples {
	return samples{min: 18446744073709551615}
}

func (s *samples) update(sample uint64) {
	s.num += 1
	s.sum += sample
	if sample < s.min {
		s.min = sample
	}
	if sample > s.max {
		s.max = sample
	}
}

func (s *samples) avg() uint64 {
	if s.num == 0 {
		return 0
	}
	return s.sum / s.num
}

type status struct {
	start time.Time // time that measurement started

	num3xx uint64 // number of 3xx responses since last status
	num4xx uint64 // number of 4xx responses since last status
	num5xx uint64 // number of 5xx responses since last status

	status200 samples // info about 200 responses since last status
	status202 samples // info about 202 responses since last status
}

func newStatus() status {
	return status{
		start:     time.Now(),
		status200: newSamples(),
		status202: newSamples(),
	}
}

func (s *status) reset() {
	s.num3xx = 0
	s.num4xx = 0
	s.num5xx = 0

	s.status200 = newSamples()
	s.status202 = newSamples()
}

func (s *status) register(ev *event) {
	s.num3xx += ev.num3xx
	s.num4xx += ev.num4xx
	s.num5xx += ev.num5xx

	delay := uint64(ev.end.Sub(ev.start).Milliseconds())
	if ev.got200 {
		s.status200.update(delay)
	} else {
		s.status202.update(delay)
	}

	ev.resetCounters()
}

func (s *status) rotate(f *os.File) error {
	str := fmt.Sprintf("%s ", time.Now().Format(time.RFC3339))
	str += fmt.Sprintf("%d ", uint64(time.Now().Sub(s.start).Milliseconds()/1000))
	str += fmt.Sprintf("%d ", s.status200.num)
	str += fmt.Sprintf("%d ", s.status202.num)
	str += fmt.Sprintf("%d ", s.num3xx)
	str += fmt.Sprintf("%d ", s.num4xx)
	str += fmt.Sprintf("%d ", s.num5xx)
	str += fmt.Sprintf("%d ", s.status200.min)
	str += fmt.Sprintf("%d ", s.status200.avg())
	str += fmt.Sprintf("%d ", s.status200.max)
	str += fmt.Sprintf("%d ", s.status202.min)
	str += fmt.Sprintf("%d ", s.status202.avg())
	str += fmt.Sprintf("%d\n", s.status202.max)
	s.reset()

	_, err := f.WriteString(str)
	return err
}

func (s *status) format() string {
	return "Time RelTime Num200 Num202 Num3xx Num4xx Num5xx Min200 Avg200 Max200 Min202 Avg202 Max202\n" +
		"      (s)                                       ----> observed response delays (ms) <----"
}