-
Notifications
You must be signed in to change notification settings - Fork 130
/
test_suite.go
302 lines (266 loc) · 10.4 KB
/
test_suite.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package core
import (
"errors"
"fmt"
"math"
"os"
"strings"
"testing"
"github.com/wowsims/wotlk/sim/core/proto"
"github.com/wowsims/wotlk/sim/core/stats"
"google.golang.org/protobuf/encoding/prototext"
)
// Precise enough to detect very small changes to test results, but truncated
// enough that we don't have flaky tests due to different OS/Go versions with
// different float rounding behavior.
const storagePrecision = 5
type IndividualTestSuite struct {
Name string
// Names of all the tests, in the order they are tested.
testNames []string
testResults *proto.TestSuiteResult
}
func NewIndividualTestSuite(suiteName string) *IndividualTestSuite {
return &IndividualTestSuite{
Name: suiteName,
testResults: newTestSuiteResult(),
}
}
func (testSuite *IndividualTestSuite) TestCharacterStats(testName string, csr *proto.ComputeStatsRequest) {
testSuite.testNames = append(testSuite.testNames, testName)
result := ComputeStats(csr)
finalStats := stats.FromFloatArray(result.RaidStats.Parties[0].Players[0].FinalStats.Stats)
testSuite.testResults.CharacterStatsResults[testName] = &proto.CharacterStatsTestResult{
FinalStats: toFixedStats(finalStats[:], storagePrecision),
}
}
func (testSuite *IndividualTestSuite) TestStatWeights(testName string, swr *proto.StatWeightsRequest) {
testSuite.testNames = append(testSuite.testNames, testName)
result := StatWeights(swr)
weights := stats.FromFloatArray(result.Dps.Weights.Stats)
testSuite.testResults.StatWeightsResults[testName] = &proto.StatWeightsTestResult{
Weights: toFixedStats(weights[:], storagePrecision),
}
}
func (testSuite *IndividualTestSuite) TestDPS(testName string, rsr *proto.RaidSimRequest) {
testSuite.testNames = append(testSuite.testNames, testName)
result := RunRaidSim(rsr)
if result.Logs != "" {
fmt.Printf("LOGS: %s\n", result.Logs)
}
if result.ErrorResult != "" {
panic("simulation failed to run: " + result.ErrorResult)
}
testSuite.testResults.DpsResults[testName] = &proto.DpsTestResult{
Dps: toFixed(result.RaidMetrics.Dps.Avg, storagePrecision),
Tps: toFixed(result.RaidMetrics.Parties[0].Players[0].Threat.Avg, storagePrecision),
Dtps: toFixed(result.RaidMetrics.Parties[0].Players[0].Dtps.Avg, storagePrecision),
Hps: toFixed(result.RaidMetrics.Parties[0].Players[0].Hps.Avg, storagePrecision),
}
}
func (testSuite *IndividualTestSuite) TestCasts(testName string, rsr *proto.RaidSimRequest) {
testSuite.testNames = append(testSuite.testNames, testName)
result := RunRaidSim(rsr)
if result.Logs != "" {
fmt.Printf("LOGS: %s\n", result.Logs)
}
if result.ErrorResult != "" {
panic("simulation failed to run: " + result.ErrorResult)
}
castsByAction := make(map[string]float64, 0)
for _, metric := range result.RaidMetrics.Parties[0].Players[0].Actions {
name := metric.Id.String()
name = strings.ReplaceAll(name, " ", " ")
for _, targetMetrics := range metric.Targets {
if val, ok := castsByAction[name]; ok {
castsByAction[name] = val + float64(targetMetrics.Casts)
} else {
castsByAction[name] = float64(targetMetrics.Casts)
}
}
castsByAction[name] /= float64(rsr.SimOptions.Iterations)
castsByAction[name] *= 10
castsByAction[name] = math.Round(castsByAction[name]) / 10.0
}
casts := &proto.CastsTestResult{Casts: castsByAction}
testSuite.testResults.CastsResults[testName] = casts
}
func (testSuite *IndividualTestSuite) Done(t *testing.T) {
testSuite.writeToFile()
}
const tolerance = 0.00001
func (testSuite *IndividualTestSuite) writeToFile() {
str := prototext.Format(testSuite.testResults)
// For some reason the formatter sometimes outputs 2 spaces instead of one.
// Replace so we get consistent output.
str = strings.ReplaceAll(str, " ", " ")
data := []byte(str)
err := os.WriteFile(testSuite.Name+".results.tmp", data, 0644)
if err != nil {
panic(err)
}
}
func (testSuite *IndividualTestSuite) readExpectedResults() (*proto.TestSuiteResult, error) {
data, err := os.ReadFile(testSuite.Name + ".results")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return newTestSuiteResult(), nil
}
return nil, err
}
results := &proto.TestSuiteResult{}
if err = prototext.Unmarshal(data, results); err != nil {
return nil, err
}
return results, err
}
func newTestSuiteResult() *proto.TestSuiteResult {
return &proto.TestSuiteResult{
CharacterStatsResults: make(map[string]*proto.CharacterStatsTestResult),
StatWeightsResults: make(map[string]*proto.StatWeightsTestResult),
DpsResults: make(map[string]*proto.DpsTestResult),
CastsResults: make(map[string]*proto.CastsTestResult),
}
}
type TestGenerator interface {
// The total number of tests that this generator can generate.
NumTests() int
// The name and API request for the test with the given index.
GetTest(testIdx int) (string, *proto.ComputeStatsRequest, *proto.StatWeightsRequest, *proto.RaidSimRequest)
}
func RunTestSuite(t *testing.T, suiteName string, generator TestGenerator) {
testSuite := NewIndividualTestSuite(suiteName)
var currentTestName string
defer func() {
if p := recover(); p != nil {
panic(fmt.Sprintf("Panic during test %s: %v", currentTestName, p))
}
}()
expectedResults, err := testSuite.readExpectedResults()
if err != nil {
t.Logf("\n\n----- FAILURE LOADING RESULTS FILE TESTS WILL FAIL-----\n%s\n-----\n\n", err)
t.Fail()
}
numTests := generator.NumTests()
for i := 0; i < numTests; i++ {
testName, csr, swr, rsr := generator.GetTest(i)
if strings.Contains(testName, "Average") && testing.Short() {
continue
}
currentTestName = testName
t.Run(currentTestName, func(t *testing.T) {
fullTestName := suiteName + "-" + testName
if csr != nil {
testSuite.TestCharacterStats(fullTestName, csr)
if actualCharacterStats, ok := testSuite.testResults.CharacterStatsResults[fullTestName]; ok {
actualStats := stats.FromFloatArray(actualCharacterStats.FinalStats)
if expectedCharacterStats, ok := expectedResults.CharacterStatsResults[fullTestName]; ok {
expectedStats := stats.FromFloatArray(expectedCharacterStats.FinalStats)
if !actualStats.EqualsWithTolerance(expectedStats, tolerance) {
t.Logf("Stats expected %v but was %v", expectedStats, actualStats)
t.Fail()
}
} else {
t.Logf("Unexpected test %s with stats: %v", fullTestName, actualStats)
t.Fail()
}
} else if !ok {
t.Logf("Missing Result for test %s", fullTestName)
t.Fail()
}
} else if swr != nil {
testSuite.TestStatWeights(fullTestName, swr)
if actualStatWeights, ok := testSuite.testResults.StatWeightsResults[fullTestName]; ok {
actualWeights := stats.FromFloatArray(actualStatWeights.Weights)
if expectedStatWeights, ok := expectedResults.StatWeightsResults[fullTestName]; ok {
expectedWeights := stats.FromFloatArray(expectedStatWeights.Weights)
if !actualWeights.EqualsWithTolerance(expectedWeights, tolerance) {
t.Logf("Weights expected %v but was %v", expectedWeights, actualWeights)
t.Fail()
}
} else {
t.Logf("Unexpected test %s with stat weights: %v", fullTestName, actualWeights)
t.Fail()
}
} else if !ok {
t.Logf("Missing Result for test %s", fullTestName)
t.Fail()
}
} else if rsr != nil && !strings.Contains(testName, "Casts") {
testSuite.TestDPS(fullTestName, rsr)
if actualDpsResult, ok := testSuite.testResults.DpsResults[fullTestName]; ok {
if expectedDpsResult, ok := expectedResults.DpsResults[fullTestName]; ok {
// Check whichever of DPS/HPS is larger first, so we get better test diff printouts.
if actualDpsResult.Dps < actualDpsResult.Hps {
if actualDpsResult.Hps < expectedDpsResult.Hps-tolerance || actualDpsResult.Hps > expectedDpsResult.Hps+tolerance {
t.Logf("HPS expected %0.03f but was %0.03f!.", expectedDpsResult.Hps, actualDpsResult.Hps)
t.Fail()
}
}
if actualDpsResult.Dps < expectedDpsResult.Dps-tolerance || actualDpsResult.Dps > expectedDpsResult.Dps+tolerance {
t.Logf("DPS expected %0.03f but was %0.03f!.", expectedDpsResult.Dps, actualDpsResult.Dps)
t.Fail()
}
if actualDpsResult.Dps >= actualDpsResult.Hps {
if actualDpsResult.Hps < expectedDpsResult.Hps-tolerance || actualDpsResult.Hps > expectedDpsResult.Hps+tolerance {
t.Logf("HPS expected %0.03f but was %0.03f!.", expectedDpsResult.Hps, actualDpsResult.Hps)
t.Fail()
}
}
if actualDpsResult.Tps < expectedDpsResult.Tps-tolerance || actualDpsResult.Tps > expectedDpsResult.Tps+tolerance {
t.Logf("TPS expected %0.03f but was %0.03f!.", expectedDpsResult.Tps, actualDpsResult.Tps)
t.Fail()
}
if actualDpsResult.Dtps < expectedDpsResult.Dtps-tolerance || actualDpsResult.Dtps > expectedDpsResult.Dtps+tolerance {
t.Logf("DTPS expected %0.03f but was %0.03f!.", expectedDpsResult.Dtps, actualDpsResult.Dtps)
t.Fail()
}
} else {
t.Logf("Unexpected test %s with %0.03f DPS!", fullTestName, actualDpsResult.Dps)
t.Fail()
}
} else if !ok {
t.Logf("Missing Result for test %s", fullTestName)
t.Fail()
}
} else if rsr != nil && strings.Contains(testName, "Casts") {
testSuite.TestCasts(fullTestName, rsr)
if actualCastsResult, ok := testSuite.testResults.CastsResults[fullTestName]; ok {
if expectedCastsResult, ok := expectedResults.CastsResults[fullTestName]; ok {
for action, casts := range actualCastsResult.Casts {
if casts < expectedCastsResult.Casts[action]-tolerance || casts > expectedCastsResult.Casts[action]+tolerance {
t.Logf("Expected %0.03f casts of %s but was %0.03f!.", expectedCastsResult.Casts[action], action, casts)
t.Fail()
}
}
} else {
t.Logf("Unexpected test %s", fullTestName)
t.Fail()
}
} else if !ok {
t.Logf("Missing Result for test %s", fullTestName)
t.Fail()
}
} else {
panic("No test request provided")
}
})
}
testSuite.Done(t)
if t.Failed() {
t.Log("One or more tests failed! If the changes are intentional, update the expected results with 'make test && make update-tests'. Otherwise go fix your bugs!")
}
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}
func toFixedStats(s []float64, precision int) []float64 {
for i, val := range s {
s[i] = toFixed(val, precision)
}
return s
}