forked from myzhan/boomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
48 lines (41 loc) · 1.36 KB
/
events.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
package boomer
import (
"fmt"
"reflect"
"github.com/asaskevich/EventBus"
)
// Events is core event bus instance of boomer
var Events = EventBus.New()
// According to locust, responseTime should be int64, in milliseconds.
// But previous version of boomer required responseTime to be float64, so sad.
func convertResponseTime(origin interface{}) int64 {
responseTime := int64(0)
if _, ok := origin.(float64); ok {
responseTime = int64(origin.(float64))
} else if _, ok := origin.(int64); ok {
responseTime = origin.(int64)
} else {
panic(fmt.Sprintf("responseTime should be float64 or int64, not %s", reflect.TypeOf(origin)))
}
return responseTime
}
func requestSuccessHandler(requestType string, name string, responseTime interface{}, responseLength int64) {
requestSuccessChannel <- &requestSuccess{
requestType: requestType,
name: name,
responseTime: convertResponseTime(responseTime),
responseLength: responseLength,
}
}
func requestFailureHandler(requestType string, name string, responseTime interface{}, exception string) {
requestFailureChannel <- &requestFailure{
requestType: requestType,
name: name,
responseTime: convertResponseTime(responseTime),
error: exception,
}
}
func initEvents() {
Events.Subscribe("request_success", requestSuccessHandler)
Events.Subscribe("request_failure", requestFailureHandler)
}