forked from nsone/dhammer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.go
58 lines (45 loc) · 1.16 KB
/
factory.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
package stats
import (
"errors"
"dhammer/config"
)
type Stat struct {
Name string `json:"stat_name"`
Value int `json:"stat_value"`
PreviousTickerValue int `json:"stat_previous_ticker_value"`
RatePerSecond float64 `json:"stat_rate_per_second"`
}
type StatValue int
type Stats interface {
AddStat(s StatValue) bool
Init() error
Run()
String() string
Stop() error
DeInit() error
}
type StatsInitParams struct {
options config.HammerConfig
logFunc func(string) bool
errFunc func(error) bool
}
var statters map[string]func(StatsInitParams) Stats = make(map[string]func(StatsInitParams) Stats)
func AddStatter(s string, f func(StatsInitParams) Stats) error {
if _, found := statters[s]; found {
return errors.New("Stats type already exists: " + s)
}
statters[s] = f
return nil
}
func New(o config.HammerConfig, logFunc func(string) bool, errFunc func(error) bool) (Stats, error) {
sip := StatsInitParams{
options: o,
logFunc: logFunc,
errFunc: errFunc,
}
sf, ok := statters[o.HammerType()]
if !ok {
return nil, errors.New("Statters - Hammer type not found: " + o.HammerType())
}
return sf(sip), nil
}