summaryrefslogtreecommitdiff
path: root/support/matchstats.myr
blob: b0375d52a3673a391399e285a572f2fb07825bd5 (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
105
106
107
108
109
110
111
112
use std
use bio
use math

const atoi = {s
	match std.intparse(s)
	| `std.Some v: -> v
	| `std.None: std.fatal("error")
	;;
}

const avg = {xs
	var sum

	sum = 0
	for x : xs
		sum += x
	;;
	-> (sum : flt64) / (xs.len : flt64)
}

const intcmp = {a, b
	if a < b
		-> `std.After
	elif a > b
		-> `std.Before
	else
		-> `std.Equal
	;;
}

const percentile = {percent, xs
	var sorted
	var idx, i
	var ret

	sorted = std.sort(xs, intcmp)
	idx = ((percent : flt64) / 100.0) * (sorted.len : flt64)
	i = (math.floor(idx) : int)
	if idx == math.floor(idx)
		ret = (xs[i-1] : flt64)
	elif idx > 1.0
		ret = ((xs[i-1] + xs[i]) / 2 : flt64)
	else
		std.fatal("percentile out-of-bunds\n")
	;;
	-> ret
}

const maximum = {xs
	var m

	m = xs[0]
	for v : xs
		if v > m
			m = v
		;;
	;;
	-> m
}

const main = {args : byte[:][:]
	var f, locs, refcnts, sizes, heights, count

	if args.len < 2
		std.put("need input file\n")
		std.exit(1)
	;;

	match bio.open(args[1], bio.Rd)
	| `std.Ok fd:  f = fd
	| `std.Err e:  std.fatal("error opening {}: {}\n", args[0], e)
	;;

	locs = [][:]
	refcnts = [][:]
	sizes = [][:]
	heights = [][:]
	count = 0

	while true
		match bio.readto(f, ",")
		| `std.Ok loc: std.slpush(&locs, std.strstrip(loc))
		| `std.Err `bio.Eof: break
		| `std.Err e: std.fatal("error read loc: {}\n", e)
		;;

		match bio.readto(f, ",")
		| `std.Ok refcnt: std.slpush(&refcnts, atoi(std.strstrip(refcnt)))
		| `std.Err e: std.fatal("error read refcnt: {}\n", e)
		;;

		match bio.readto(f, ",")
		| `std.Ok size: std.slpush(&sizes, atoi(std.strstrip(size)))
		| `std.Err e: std.fatal("error read size: {}\n", e)
		;;

		match bio.readto(f, "\n")
		| `std.Ok height: std.slpush(&heights, atoi(std.strstrip(height)))
		| `std.Err e: std.fatal("error read height: {}\n", e)
		;;
		count ++
	;;

	std.put("Sample count: {}\n", count)
	std.put("Dtree Refcnt\tavg: {s=3}\t95th percentile: {s=3}\t maximum: {}\n", avg(refcnts), percentile(95, refcnts), maximum(refcnts))
	std.put("Dtree Size\tavg: {s=3}\t95th percentile: {s=3}\t maximum: {}\n", avg(sizes), percentile(95, sizes), maximum(sizes))
	std.put("Dtree Height\tavg: {s=3}\t95th percentile: {s=3}\t maximum: {}\n", avg(heights), percentile(95, heights), maximum(heights))

	bio.close(f)
}