-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (77 loc) · 2.34 KB
/
main.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
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
package main
import (
"github.com/montanaflynn/stats"
"log"
"time"
)
const ExternalIterations = 5
const InternalIterations = 1000
const NumberOfPersons = 100
func main() {
m := make(map[string]func() (time.Duration, time.Duration, float64))
m["Json"] = func() (time.Duration, time.Duration, float64) {
jsonInit()
jsonEncodeTime, jsonSize := measure(testJsonEncode)
jsonDecodeTime, _ := measure(testJsonDecode)
return jsonEncodeTime, jsonDecodeTime, jsonSize
}
m["Protobuf"] = func() (time.Duration, time.Duration, float64) {
protoInit()
jsonEncodeTime, jsonSize := measure(testProtoEncode)
jsonDecodeTime, _ := measure(testProtoDecode)
return jsonEncodeTime, jsonDecodeTime, jsonSize
}
m["Thrift Compact"] = func() (time.Duration, time.Duration, float64) {
thriftInit()
jsonEncodeTime, jsonSize := measure(testThriftEncode)
jsonDecodeTime, _ := measure(testThriftDecode)
return jsonEncodeTime, jsonDecodeTime, jsonSize
}
for protocol, cb := range m {
took, _ := measure(func() float64 {
testProtocol(protocol, cb)
return 0
})
log.Println("Took: ", took)
}
}
func measure(cb func() float64) (time.Duration, float64) {
now := time.Now()
median := cb()
return time.Since(now), median
}
func testProtocol(name string, cb func() (time.Duration, time.Duration, float64)) {
var encodeStats []float64
var decodeStats []float64
var sizeStats []float64
for i := 0; i < ExternalIterations; i++ {
encodeTime, decodeTime, size := cb()
//log.Println(encodeTime, decodeTime, size)
encodeStats = append(encodeStats, float64(encodeTime))
decodeStats = append(decodeStats, float64(decodeTime))
sizeStats = append(sizeStats, size)
}
log.Println(name, ": ")
PrintMetrics(calcMedians(encodeStats, decodeStats, sizeStats))
}
func calcMedians(encodeStats, decodeStats, sizeStats []float64) (encodeMedian, decodeMedian, sizeMedian float64) {
var err error
encodeMedian, err = stats.Median(encodeStats)
if err != nil {
return
}
decodeMedian, err = stats.Median(decodeStats)
if err != nil {
return
}
sizeMedian, err = stats.Median(sizeStats)
if err != nil {
return
}
return
}
func PrintMetrics(encodeMedian, decodeMedian, sizeMedian float64) {
log.Println("Encode median time: ", time.Duration(encodeMedian))
log.Println("Decode median time: ", time.Duration(decodeMedian))
log.Println("Size median bytes: ", sizeMedian)
}