about summary refs log tree commit diff
path: root/tools/seqstat/seqstat.go
blob: 8709fa461c162c495f1d6d5d98d1a81ac01dc046 (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
package main

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	stats = flag.Bool("S", false, "Display statistics about the sequence.")
)

func main() {
	flag.Parse()

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: [-S] <INPUT>")
		flag.PrintDefaults()
	}

	elements := argsToElements(flag.Args())

	if len(elements) < 1 {
		scanner := bufio.NewScanner(os.Stdin)
		var e []string
		for scanner.Scan() {
			e = append(e, strings.Split(scanner.Text(), " ")...)
		}
		elements = argsToElements(e)
	}

	seq := newSequence(elements)

	fmt.Println(string(seq.histogram()))

	if *stats {
		fmt.Printf("min:   %f\n", seq.min)
		fmt.Printf("max:   %f\n", seq.max)
		fmt.Printf("avg:   %f\n", seq.avg())
		fmt.Printf("p50:   %f\n", seq.p50())
		fmt.Printf("p90:   %f\n", seq.p90())
		fmt.Printf("p99:   %f\n", seq.p99())
		fmt.Printf("p999:  %f\n", seq.p999())
		fmt.Printf("ordered sequence: %v\n", seq.elementsSorted)
	}
}

// converts the input to float64
func argsToElements(args []string) []float64 {
	elements := make([]float64, len(args))

	for i, input := range args {
		num, err := strconv.ParseFloat(input, 64)
		if err != nil {
			panic(err)
		}
		elements[i] = num
	}
	return elements
}