about summary refs log tree commit diff
path: root/tools/seqstat/sequence.go
blob: d4ec91bcec64367b2c3b5d758e9dd9e4911e6a18 (plain) (blame)
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
package main

import (
	"sort"
)

var (
	ticks = []rune{'\u2581', '\u2582', '\u2583', '\u2584', '\u2585', '\u2586', '\u2587', '\u2588'}
)

type sequence struct {
	elements       []float64
	elementsSorted []float64
	min            float64
	max            float64
	factor         int
	sum            float64
}

func (s *sequence) avg() float64 {
	return s.sum / float64(len(s.elements))
}

func (s *sequence) p50() float64 {
	return s.elementsSorted[len(s.elementsSorted)*50/100]
}

func (s *sequence) p90() float64 {
	return s.elementsSorted[len(s.elementsSorted)*90/100]
}

func (s *sequence) p99() float64 {
	return s.elementsSorted[len(s.elementsSorted)*99/100]
}

func (s *sequence) p999() float64 {
	return s.elementsSorted[len(s.elementsSorted)*999/1000]
}
func (s *sequence) histogram() []rune {
	histogram := make([]rune, len(s.elements))
	for i, num := range s.elements {
		v := (((int(num) - int(s.min)) << 8) / s.factor)
		histogram[i] = ticks[v]
	}
	return histogram
}

func newSequence(elements []float64) *sequence {
	s := new(sequence)
	s.elements = elements

	s.min = s.elements[0]
	s.max = s.elements[0]

	s.sum = 0

	for _, element := range s.elements {
		if element > s.max {
			s.max = element
		}
		if element < s.min {
			s.min = element
		}
		s.sum += element
	}

	s.factor = ((int(s.max) - int(s.min)) << 8) / (len(ticks) - 1)

	if s.factor < 1 {
		s.factor = 1
	}

	elementsSorted := make([]float64, len(elements))
	copy(elementsSorted, elements)
	sort.Float64s(elementsSorted)
	s.elementsSorted = elementsSorted

	return s
}