forked from topfreegames/khan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
216 lines (187 loc) · 5.67 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package loadtest
import (
"fmt"
"math/rand"
"net/http"
"strings"
"time"
"github.com/spf13/viper"
"github.com/topfreegames/khan/lib"
"github.com/topfreegames/khan/log"
"github.com/uber-go/zap"
)
// App represents a load test application
type App struct {
config *viper.Viper
logger zap.Logger
cache cache
client lib.KhanInterface
operations []operation
}
// GetApp returns a new app
func GetApp(configFile, sharedClansFile string, logger zap.Logger) *App {
app := &App{
config: viper.New(),
logger: logger,
}
app.configure(configFile, sharedClansFile)
return app
}
func (app *App) configure(configFile, sharedClansFile string) {
app.setConfigurationDefaults()
app.loadConfiguration(configFile)
app.configureOperations()
app.configureCache(sharedClansFile)
app.configureClient()
}
func (app *App) setConfigurationDefaults() {
app.setClientConfigurationDefaults()
}
func (app *App) setClientConfigurationDefaults() {
app.config.SetDefault("loadtest.client.timeout", 500*time.Millisecond)
app.config.SetDefault("loadtest.client.maxIdleConns", 100)
app.config.SetDefault("loadtest.client.maxIdleConnsPerHost", http.DefaultMaxIdleConnsPerHost)
}
func (app *App) loadConfiguration(configFile string) {
l := app.logger.With(
zap.String("source", "loadtest/app"),
zap.String("operation", "loadConfiguration"),
zap.String("configFile", configFile),
)
app.config.SetConfigType("yaml")
app.config.SetConfigFile(configFile)
app.config.SetEnvPrefix("khan")
app.config.AddConfigPath(".")
app.config.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
app.config.AutomaticEnv()
if err := app.config.ReadInConfig(); err == nil {
log.I(l, "Loaded config file successfully.")
} else {
log.P(l, "Config file failed to load.")
}
}
func (app *App) configureOperations() {
app.configurePlayerOperations()
app.configureClanOperations()
app.configureMembershipOperations()
}
func (app *App) configureCache(sharedClansFile string) {
gameMaxMembers := app.config.GetInt("loadtest.game.maxMembers")
var err error
app.cache, err = newCacheImpl(gameMaxMembers, sharedClansFile)
if err != nil {
l := app.logger.With(
zap.String("source", "loadtest/app"),
zap.String("operation", "configureCache"),
zap.Int("gameMaxMembers", gameMaxMembers),
zap.String("sharedClansFile", sharedClansFile),
zap.String("error", err.Error()),
)
log.P(l, "Error configuring cache.")
}
}
func (app *App) configureClient() {
app.client = lib.NewKhanWithParams(&lib.KhanParams{
Timeout: app.config.GetDuration("loadtest.client.timeout"),
MaxIdleConns: app.config.GetInt("loadtest.client.maxIdleConns"),
MaxIdleConnsPerHost: app.config.GetInt("loadtest.client.maxIdleConnsPerHost"),
URL: app.config.GetString("loadtest.client.url"),
User: app.config.GetString("loadtest.client.user"),
Pass: app.config.GetString("loadtest.client.pass"),
GameID: app.config.GetString("loadtest.client.gameid"),
})
}
// Run executes the load test suite
func (app *App) Run() error {
if err := app.cache.loadInitialData(app.client); err != nil {
return err
}
nOperations := app.config.GetInt("loadtest.operations.amount")
intervalDuration := app.config.GetDuration("loadtest.operations.interval.duration")
getPercent := func(cur int) int {
return int((100 * int64(cur)) / int64(nOperations))
}
l := app.logger.With(
zap.String("source", "loadtest/app"),
zap.String("operation", "Run"),
zap.Int("nOperations", nOperations),
zap.Duration("intervalDuration", intervalDuration),
)
for i := 0; i < nOperations; i++ {
err := app.performOperation()
if err != nil {
return err
}
if i < nOperations-1 {
time.Sleep(intervalDuration)
}
if getPercent(i+1)/10 > getPercent(i)/10 {
log.I(l, fmt.Sprintf("Goroutine completed %v%%.", getPercent(i+1)))
}
}
return nil
}
func (app *App) performOperation() error {
operation, err := app.getRandomOperation()
if err != nil {
return err
}
err = operation.execute()
if err != nil {
return err
}
return nil
}
func (app *App) getRandomOperation() (operation, error) {
sampleSpace, err := app.getOperationSampleSpace()
if err != nil {
return operation{}, err
}
return getRandomOperationFromSampleSpace(sampleSpace, -1), nil
}
func (app *App) getOperationSampleSpace() ([]operation, error) {
var pSum float64
var sampleSpace []operation
for _, operation := range app.operations {
ok, err := operation.canExecute()
if err != nil {
return nil, err
}
if ok && operation.probability > 0 {
pSum += operation.probability
sampleSpace = append(sampleSpace, operation)
}
}
if pSum <= 0 {
return nil, &GenericError{"NoOperationsAvailableError", "No operations can be executed anymore."}
}
for i := range sampleSpace {
sampleSpace[i].probability /= pSum
}
return sampleSpace, nil
}
func getRandomOperationFromSampleSpace(sampleSpace []operation, dice float64) operation {
if dice < 0 {
dice = rand.Float64()
}
var pSum float64
for _, operation := range sampleSpace {
pSum += operation.probability
if dice <= pSum {
return operation
}
}
return sampleSpace[0]
}
func (app *App) appendOperation(op operation) {
app.operations = append(app.operations, op)
}
func (app *App) getOperationProbabilityConfigKey(operation string) string {
return fmt.Sprintf("loadtest.operations.%s.probability", operation)
}
func (app *App) getOperationProbabilityConfig(operation string) float64 {
return app.config.GetFloat64(app.getOperationProbabilityConfigKey(operation))
}
func (app *App) setOperationProbabilityConfigDefault(operation string, probability float64) {
app.config.SetDefault(app.getOperationProbabilityConfigKey(operation), probability)
}