forked from packetstream/proxysampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
69 lines (55 loc) · 1.17 KB
/
report.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
package main
import (
"encoding/json"
"fmt"
"os"
"gopkg.in/yaml.v1"
)
// displayReport outputs a report of proxy performance & health
func displayReport(results []*result) {
success := 0
fail := 0
averageTTFB := int64(0)
// Calculate stats
for _, v := range results {
if v == nil {
continue
}
if v.StatusCode == -1 {
fail++
continue
}
averageTTFB += v.Latency.TTFB
success++
}
if success > 0 {
averageTTFB /= int64(success)
}
switch output {
case "json":
// JSON formatted report
report := &report{Success: success, Fail: fail, AverageTTFB: averageTTFB, Results: results}
b, err := json.Marshal(report)
if err != nil {
panic(err)
}
// Write to stdout
os.Stdout.Write(b)
return
case "yaml":
// YAML formatted report
report := &report{Success: success, Fail: fail, AverageTTFB: averageTTFB, Results: results}
b, err := yaml.Marshal(report)
if err != nil {
panic(err)
}
// Write to stdout
os.Stdout.Write(b)
return
default:
break
}
// Plaintext formatted report
fmt.Println(fmt.Sprintf("Success rate: %d/%d", success, success+fail))
fmt.Println(fmt.Sprintf("Average TTFB: %dms", averageTTFB))
}