-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.go
117 lines (100 loc) · 2.77 KB
/
util.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Paulo Suzart. All rights reserved.
// The use and distribution terms for this software are covered by the
// Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
// which can be found in the file epl-v10.html at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license.
// You must not remove this notice, or any other, from this software.
package main
import (
"errors"
"fmt"
"text/template"
"strings"
"time"
)
const (
WORKER string = "worker"
STANDALONE string = "standalone"
MASTER string = "master"
)
//Return the min or y if x is -1
func Min(x, y int64) int64 {
if x == -1 {
return y
}
if x < y {
return x
}
return y
}
//Return the max
func Max(x, y int64) int64 {
if x > y {
return x
}
return y
}
//Masters implements this interface.
//If a timeout occour, the gb will call the
//shutdown function.
type Supervised interface {
Shutdown()
}
//Used by template to generate gb output
type StringWritter struct {
s string
}
//Writes the template as string
func (self *StringWritter) Write(p []byte) (n int, err error) {
self.s += string(p)
return len(self.s), nil
}
//Parse any flag represented by
//a key-value with a separator
func parseKV(param *string, separator, errmsg string) (k, v string, err error) {
if *param == "" {
return
}
data := strings.SplitN(*param, separator, 2)
if len(data) != 2 {
err = errors.New(errmsg)
}
k = data[0]
v = data[1]
return
}
func counting(f func()) int64 {
start := time.Now().UnixNano()
f()
return time.Now().UnixNano() - start
}
//Just converts a nanosecond value to a milisecond value.
func nan2mi(value float64) float64 {
return value * 0.000001
}
//Custom formats used in the output template.
//f2mi means float64 to miliseconds and i2mi
//means int64 to miliseconds, returning a float64
//representing it
var CustomFormatter = template.FuncMap{
"f2mi": func(value ...interface{}) string {
return fmt.Sprint(nan2mi(value[0].(float64)), 'f', -1)
},
"i2mi": func(value ...interface{}) string {
return fmt.Sprint(nan2mi(float64(value[0].(int64))), 'f', -1)
},
}
//Template used in console output.
var OutPutTemplate = `
=========================================================================
Test Summary (gb. Version: 0.0.2 beta)
-------------------------------------------------------------------------
Total Go Benchmark Time | {Elapsed|i2mi} milisecs
Requests Performed | {TotalSuc}
Requests Lost | {TotalErr}
Target supports (reqs/s) | ~{RequestsPerSecond}
Average Response Time | {Avg|f2mi} milisecs
Max Response Time | {Max|i2mi} milisecs
Min Response Time | {Min|i2mi} milisecs
`