test_budget 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.test_budget.yml +4 -0
- data/CHANGELOG.md +36 -1
- data/README.md +297 -17
- data/Rakefile +1 -0
- data/exe/test_budget +6 -0
- data/lib/test_budget/allowlist.rb +68 -0
- data/lib/test_budget/audit.rb +23 -0
- data/lib/test_budget/auditor.rb +33 -0
- data/lib/test_budget/budget/estimate.rb +82 -0
- data/lib/test_budget/budget.rb +107 -0
- data/lib/test_budget/cli.rb +163 -0
- data/lib/test_budget/format.rb +39 -0
- data/lib/test_budget/inflector.rb +20 -0
- data/lib/test_budget/parser/rspec.rb +53 -0
- data/lib/test_budget/reporter.rb +33 -0
- data/lib/test_budget/statistics.rb +28 -0
- data/lib/test_budget/table.rb +55 -0
- data/lib/test_budget/test_case.rb +59 -0
- data/lib/test_budget/test_run/breakdown.rb +48 -0
- data/lib/test_budget/test_run/delta.rb +28 -0
- data/lib/test_budget/test_run/diff.rb +55 -0
- data/lib/test_budget/test_run.rb +33 -0
- data/lib/test_budget/version.rb +1 -1
- data/lib/test_budget/violation.rb +19 -0
- data/lib/test_budget/warning.rb +10 -0
- data/lib/test_budget.rb +22 -1
- metadata +47 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 629009a91d15d30245f7a822ebb05fffc4a22b049d41f34750a7e3eee4ba3a73
|
|
4
|
+
data.tar.gz: 18067442b89424baafeeae02d9faa972214f09040f4b7fa5feb338076a9ea2d8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ff11faadb6422e8af49ff601cf66e71e4ffed2743322ce6117642b0269273ed1b91cffe031e857168dec49a0b182c7209c1c72115d1fcbaef415b28b1bc720de
|
|
7
|
+
data.tar.gz: 9918fb99ddc3321d53db2c6ea0b7672adb6230a768055b8634cb88c0f833022914edaa8cbf75fccf273ca4dded3cfc8b031e519d94b0d4c556fb3bff580a1fdd
|
data/.test_budget.yml
ADDED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
-
## [0.
|
|
3
|
+
## [0.2.0] - 2026-11-23
|
|
4
|
+
|
|
5
|
+
- Add `diff` command to compare two test runs
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
$ bundle exec test_budget diff tmp/before.json tmp/after.json
|
|
9
|
+
┌───────────┬─────────┬─────────┬────────────┬─────────┐
|
|
10
|
+
│ Test Type │ Δ Count │ % │ Δ Duration │ % │
|
|
11
|
+
├───────────┼─────────┼─────────┼────────────┼─────────┤
|
|
12
|
+
│ system │ -2 │ -50.0% │ -3m 8s │ -73.4% │
|
|
13
|
+
│ request │ +5 │ +41.7% │ +32s │ +41.0% │
|
|
14
|
+
│ job │ +4 │ new │ +3s │ new │
|
|
15
|
+
├───────────┼─────────┼─────────┼────────────┼─────────┤
|
|
16
|
+
│ Total │ +7 │ +14.0% │ -2m 33s │ -39.5% │
|
|
17
|
+
└───────────┴─────────┴─────────┴────────────┴─────────┘
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
- Add `breakdown` command to show time distribution across test types
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
$ bundle exec test_budget breakdown
|
|
24
|
+
┌───────────┬───────┬───────┬──────────┬───────┐
|
|
25
|
+
│ Test Type │ Count │ % │ Duration │ % │
|
|
26
|
+
├───────────┼───────┼───────┼──────────┼───────┤
|
|
27
|
+
│ system │ 4 │ 8.0% │ 4m 16s │ 66.2% │
|
|
28
|
+
│ request │ 12 │ 24.0% │ 1m 18s │ 20.2% │
|
|
29
|
+
│ model │ 30 │ 60.0% │ 50s │ 12.9% │
|
|
30
|
+
│ job │ 4 │ 8.0% │ 3s │ 0.8% │
|
|
31
|
+
├───────────┼───────┼───────┼──────────┼───────┤
|
|
32
|
+
│ Total │ 50 │ │ 6m 27s │ │
|
|
33
|
+
└───────────┴───────┴───────┴──────────┴───────┘
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- Add `--sort` flag to `breakdown` command (`duration`, `count`, or `type`)
|
|
37
|
+
|
|
38
|
+
## [0.1.0] - 2026-03-09
|
|
4
39
|
|
|
5
40
|
- Initial release
|
data/README.md
CHANGED
|
@@ -1,39 +1,319 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Test Budget
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Prevent slow tests from creeping into your suite.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Test suites get slow one test at a time. By the time you notice, your CI takes
|
|
6
|
+
40 minutes and nobody wants to touch it.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
Test Budget is **a linter for test performance**. It reads your test results
|
|
9
|
+
after the run, checks durations against configured budgets, and fails if
|
|
10
|
+
anything goes over.
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
It doesn't change how your tests run. It just tells you when they're too slow —
|
|
13
|
+
before it gets worse.
|
|
10
14
|
|
|
11
|
-
Install
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
Add to your Gemfile:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
gem "test_budget"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
> [!NOTE]
|
|
24
|
+
> Test Budget currently supports **RSpec only**. Minitest support is not yet available.
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
Three steps: generate timing data, create a budget config, then audit.
|
|
29
|
+
|
|
30
|
+
**1. Run your tests with JSON output** to collect timing data:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
bundle exec rspec --format progress --format json --out tmp/test_timings.json
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
> [!NOTE]
|
|
37
|
+
> Using a parallel runner? See [this section](#parallel-test-runners)
|
|
38
|
+
> for setup instructions.
|
|
39
|
+
|
|
40
|
+
**2. Generate a budget config** from the timing data:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
bundle exec test_budget init tmp/test_timings.json
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This creates `.test_budget.yml` with budgets derived from your actual data
|
|
47
|
+
(see [Configuration](#configuration) for details). Edit it freely to match your
|
|
48
|
+
standards.
|
|
49
|
+
|
|
50
|
+
> [!TIP]
|
|
51
|
+
> Don't want to generate timing data first? Run `bundle exec test_budget init`
|
|
52
|
+
> without arguments to create a config with Rails defaults instead.
|
|
53
|
+
|
|
54
|
+
**3. Audit your test suite** against the budget:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
bundle exec rspec --format progress --format json --out tmp/test_timings.json
|
|
58
|
+
bundle exec test_budget audit
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Example output:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
Test budget: 1 violation(s) found
|
|
65
|
+
|
|
66
|
+
1) spec/system/signup_spec.rb -- creates account (11.20s) exceeds system limit (6.00s)
|
|
67
|
+
To allowlist, run:
|
|
68
|
+
bundle exec test_budget allowlist spec/system/signup_spec.rb:15 --reason "<reason>"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
That's it. From here on, run the audit after every test run (see [CI
|
|
72
|
+
integration](#ci-integration)).
|
|
73
|
+
|
|
74
|
+
### Init options
|
|
75
|
+
|
|
76
|
+
Use `--force` to overwrite an existing `.test_budget.yml`.
|
|
77
|
+
|
|
78
|
+
`estimate` is an alias for `init`. Use whichever name feels right:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
bundle exec test_budget estimate tmp/test_timings.json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Parallel test runners
|
|
85
|
+
|
|
86
|
+
If you use [`parallel_tests`][parallel_tests],
|
|
87
|
+
`$TEST_ENV_NUMBER` in command arguments is replaced per worker (empty string for
|
|
88
|
+
worker 1, `2` for worker 2, etc.). Use it to write a separate output file per
|
|
89
|
+
worker:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
bundle exec parallel_rspec -- --format json --out 'tmp/test_timings$TEST_ENV_NUMBER.json'
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This produces `test_timings.json`, `test_timings2.json`, `test_timings3.json`,
|
|
96
|
+
etc. Then set your `timings_path` to a glob pattern:
|
|
97
|
+
|
|
98
|
+
```yaml
|
|
99
|
+
timings_path: "tmp/test_timings*.json"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
If you use [`flatware`][flatware], each worker appends its results to the same
|
|
103
|
+
output file. Test Budget handles this automatically:
|
|
12
104
|
|
|
13
105
|
```bash
|
|
14
|
-
|
|
106
|
+
flatware rspec --format json --out tmp/test_timings.json
|
|
15
107
|
```
|
|
16
108
|
|
|
17
|
-
|
|
109
|
+
## Configuration
|
|
110
|
+
|
|
111
|
+
The `init` command creates a `.test_budget.yml` in your project root with
|
|
112
|
+
budgets based on your actual data (per-test limits use the 99th percentile
|
|
113
|
+
rounded to the nearest 0.5s; the suite limit uses total duration + 10%
|
|
114
|
+
headroom). The generated file is a starting point — edit it freely to
|
|
115
|
+
match your team's standards. You can also create one manually:
|
|
116
|
+
|
|
117
|
+
```yaml
|
|
118
|
+
timings_path: tmp/test_timings.json
|
|
119
|
+
|
|
120
|
+
suite:
|
|
121
|
+
max_duration: 300 # seconds
|
|
122
|
+
|
|
123
|
+
per_test_case:
|
|
124
|
+
default: 2
|
|
125
|
+
system: 6
|
|
126
|
+
request: 3
|
|
127
|
+
model: 1.5
|
|
128
|
+
|
|
129
|
+
allowlist:
|
|
130
|
+
- test_case: "spec/services/invoice_pdf_spec.rb -- generates PDF with line items"
|
|
131
|
+
reason: "PDF generation is inherently slow, tracking in JIRA-1234"
|
|
132
|
+
expires_on: "2025-06-01"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- **`timings_path`** (required) — path (or glob pattern) to the RSpec JSON output file(s).
|
|
136
|
+
- **`suite.max_duration`** — total duration budget for the entire suite.
|
|
137
|
+
- **`per_test_case.default`** — default per-test limit. Applies to any type without a specific limit.
|
|
138
|
+
- **`per_test_case.<type>`** — per-test limit for a specific type. Types are inferred from file paths by singularizing the directory name (`spec/models/` -> `model`, `spec/features/` -> `feature`, `spec/system/` -> `system`, etc).
|
|
139
|
+
- **`allowlist`** — known slow tests to skip. Each entry requires an `expires_on` date (YYYY-MM-DD). Expired entries stop exempting their tests. Use this as a temporary escape hatch, not a permanent solution.
|
|
140
|
+
|
|
141
|
+
> [!IMPORTANT]
|
|
142
|
+
> At least one limit (`suite.max_duration`, `per_test_case.default`, or a type-specific limit) must be configured.
|
|
143
|
+
|
|
144
|
+
## Audit
|
|
18
145
|
|
|
19
146
|
```bash
|
|
20
|
-
|
|
147
|
+
bundle exec test_budget audit
|
|
21
148
|
```
|
|
22
149
|
|
|
23
|
-
|
|
150
|
+
Use `--budget` to point to a different config file:
|
|
24
151
|
|
|
25
|
-
|
|
152
|
+
```bash
|
|
153
|
+
bundle exec test_budget audit --budget config/test_budget.yml
|
|
154
|
+
```
|
|
26
155
|
|
|
27
|
-
|
|
156
|
+
Use `--tolerant` to apply a 10% tolerance to all limits. This is useful on
|
|
157
|
+
shared CI infrastructure where CPU contention causes small fluctuations in test
|
|
158
|
+
durations:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
bundle exec test_budget audit --tolerant
|
|
162
|
+
```
|
|
28
163
|
|
|
29
|
-
|
|
164
|
+
With `--tolerant`, a test only fails if it exceeds the limit by more than 10%
|
|
165
|
+
(e.g., a 5s limit becomes an effective 5.5s limit).
|
|
30
166
|
|
|
31
|
-
|
|
167
|
+
Exit code is `0` when all tests are within budget, `1` when there are violations.
|
|
32
168
|
|
|
33
|
-
##
|
|
169
|
+
## Allowlist
|
|
34
170
|
|
|
35
|
-
|
|
171
|
+
You can allowlist individual tests via the CLI:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
bundle exec test_budget allowlist spec/models/user_spec.rb:10 --reason "Tracking in JIRA-1234"
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Entries are created with a 60-day expiration by default. Edit the `expires_on`
|
|
178
|
+
date in the YAML file if you need a different window.
|
|
179
|
+
|
|
180
|
+
### Pruning obsolete entries
|
|
181
|
+
|
|
182
|
+
Over time, allowlisted tests may be fixed or removed. Use `prune` to clean up
|
|
183
|
+
entries that are no longer needed:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
bundle exec test_budget prune
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
This removes **stale** entries (test no longer exists) and **unnecessary** entries
|
|
190
|
+
(test is now within budget). The `audit` command also warns about these entries
|
|
191
|
+
so you know when it's time to prune.
|
|
192
|
+
|
|
193
|
+
### Example output
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
Test budget: 2 violation(s) found
|
|
197
|
+
|
|
198
|
+
1) spec/models/user_spec.rb -- User#full_name (2.50s) exceeds model limit (1.00s)
|
|
199
|
+
To allowlist, run:
|
|
200
|
+
bundle exec test_budget allowlist spec/models/user_spec.rb:10 --reason "<reason>"
|
|
201
|
+
|
|
202
|
+
2) Suite total (650.00s) exceeds limit (600.00s)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Breakdown
|
|
206
|
+
|
|
207
|
+
See where your test time goes:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
bundle exec test_budget breakdown tmp/test_timings.json
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Example output:
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
┌───────────┬───────┬───────┬──────────┬───────┐
|
|
217
|
+
│ Test Type │ Count │ % │ Duration │ % │
|
|
218
|
+
├───────────┼───────┼───────┼──────────┼───────┤
|
|
219
|
+
│ system │ 4 │ 8.0% │ 4m 16s │ 66.2% │
|
|
220
|
+
│ request │ 12 │ 24.0% │ 1m 18s │ 20.2% │
|
|
221
|
+
│ model │ 30 │ 60.0% │ 50s │ 12.9% │
|
|
222
|
+
│ job │ 4 │ 8.0% │ 3s │ 0.8% │
|
|
223
|
+
├───────────┼───────┼───────┼──────────┼───────┤
|
|
224
|
+
│ Total │ 50 │ │ 6m 27s │ │
|
|
225
|
+
└───────────┴───────┴───────┴──────────┴───────┘
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Without arguments, it reads from `tmp/test_timings.json`.
|
|
229
|
+
|
|
230
|
+
## Diff
|
|
231
|
+
|
|
232
|
+
Compare two test runs to see what changed:
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
bundle exec test_budget diff tmp/before.json tmp/after.json
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Example output:
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
┌───────────┬─────────┬─────────┬────────────┬─────────┐
|
|
242
|
+
│ Test Type │ Δ Count │ % │ Δ Duration │ % │
|
|
243
|
+
├───────────┼─────────┼─────────┼────────────┼─────────┤
|
|
244
|
+
│ system │ -2 │ -50.0% │ -3m 8s │ -73.4% │
|
|
245
|
+
│ request │ +5 │ +41.7% │ +32s │ +41.0% │
|
|
246
|
+
│ job │ +4 │ new │ +3s │ new │
|
|
247
|
+
├───────────┼─────────┼─────────┼────────────┼─────────┤
|
|
248
|
+
│ Total │ +7 │ +14.0% │ -2m 33s │ -39.5% │
|
|
249
|
+
└───────────┴─────────┴─────────┴────────────┴─────────┘
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
New test types show `new` in the percent columns. Removed types show `-100.0%`.
|
|
253
|
+
Types with no change are hidden. When runs are identical, nothing is printed.
|
|
254
|
+
|
|
255
|
+
## CI integration
|
|
256
|
+
|
|
257
|
+
Run the audit after your test suite:
|
|
258
|
+
|
|
259
|
+
```yaml
|
|
260
|
+
# .github/workflows/ci.yml
|
|
261
|
+
- run: bundle exec rspec --format progress --format json --out tmp/test_timings.json
|
|
262
|
+
- run: bundle exec test_budget audit
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
The second step fails the build if any test exceeds its budget.
|
|
266
|
+
|
|
267
|
+
## I have violations. Now what?
|
|
268
|
+
|
|
269
|
+
Violations mean tests are slower than you decided they should be. You have
|
|
270
|
+
options:
|
|
271
|
+
|
|
272
|
+
- **Make the tests faster.** This is the best option. Look for unnecessary
|
|
273
|
+
setup, N+1 queries, slow external calls that could be stubbed. Can the same
|
|
274
|
+
behavior be exercised with a faster test type? (e.g. system -> request,
|
|
275
|
+
request -> model)
|
|
276
|
+
- **Split the work.** A test doing too much can often be broken into
|
|
277
|
+
focused scenarios.
|
|
278
|
+
- **Parallelize.** Tools like [`parallel_tests`][parallel_tests] and
|
|
279
|
+
[`flatware`][flatware] reduce wall time without changing individual test
|
|
280
|
+
durations, but consider also setting per-test budgets to keep individual tests
|
|
281
|
+
honest.
|
|
282
|
+
- **Upgrade infrastructure.** Faster CI (or developer) machines buy time.
|
|
283
|
+
- **Allowlist temporarily.** If a fix isn't immediate, add the test to the
|
|
284
|
+
allowlist and create a ticket. This keeps the budget enforced for everything
|
|
285
|
+
else.
|
|
286
|
+
|
|
287
|
+
The goal isn't zero violations on day one. It's to stop the bleeding and make
|
|
288
|
+
test performance visible.
|
|
36
289
|
|
|
37
290
|
## License
|
|
38
291
|
|
|
39
|
-
The gem is available as open source under the terms of the [MIT
|
|
292
|
+
The gem is available as open source under the terms of the [MIT
|
|
293
|
+
License](https://opensource.org/licenses/MIT).
|
|
294
|
+
|
|
295
|
+
## Code of Conduct
|
|
296
|
+
|
|
297
|
+
Everyone interacting in the Test Budget project's codebases, issue trackers,
|
|
298
|
+
chat rooms and mailing lists is expected to follow the [code of
|
|
299
|
+
conduct](https://github.com/thoughtbot/test_budget/blob/main/CODE_OF_CONDUCT.md).
|
|
300
|
+
|
|
301
|
+
<!-- START /templates/footer.md -->
|
|
302
|
+
|
|
303
|
+
## About thoughtbot
|
|
304
|
+
|
|
305
|
+

|
|
306
|
+
|
|
307
|
+
This repo is maintained and funded by thoughtbot, inc. The names and logos for
|
|
308
|
+
thoughtbot are trademarks of thoughtbot, inc.
|
|
309
|
+
|
|
310
|
+
We love open source software! See [our other projects][community]. We are
|
|
311
|
+
[available for hire][hire].
|
|
312
|
+
|
|
313
|
+
[community]: https://thoughtbot.com/community?utm_source=github&utm_medium=readme&utm_campaign=test_budget
|
|
314
|
+
[hire]: https://thoughtbot.com/hire-us?utm_source=github&utm_medium=readme&utm_campaign=test_budget
|
|
315
|
+
|
|
316
|
+
<!-- END /templates/footer.md -->
|
|
317
|
+
|
|
318
|
+
[parallel_tests]: https://github.com/grosser/parallel_tests
|
|
319
|
+
[flatware]: https://github.com/briandunn/flatware
|
data/Rakefile
CHANGED
data/exe/test_budget
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
|
|
5
|
+
module TestBudget
|
|
6
|
+
class Allowlist
|
|
7
|
+
def initialize(raw_entries = [])
|
|
8
|
+
@entries = raw_entries.map do |entry|
|
|
9
|
+
Entry.new(
|
|
10
|
+
test_case_key: entry["test_case"],
|
|
11
|
+
reason: entry["reason"],
|
|
12
|
+
expires_on: parse_date(entry["expires_on"])
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def allowed?(key)
|
|
18
|
+
@entries.find { |e| e.matches?(key) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def add(key, reason:, expires_on:)
|
|
22
|
+
raise Error, "#{key} is already allowlisted" if allowed?(key)
|
|
23
|
+
|
|
24
|
+
entry = Entry.new(test_case_key: key, reason: reason, expires_on: expires_on)
|
|
25
|
+
@entries << entry
|
|
26
|
+
entry
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def prune(test_run, budget)
|
|
30
|
+
removed, @entries = @entries.partition { |entry| entry.obsolete?(test_run, budget) }
|
|
31
|
+
removed
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_a
|
|
35
|
+
@entries.dup
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def parse_date(raw)
|
|
41
|
+
Date.parse(raw.to_s)
|
|
42
|
+
rescue Date::Error, TypeError
|
|
43
|
+
raise Error, "expires_on is required and must be a valid date (YYYY-MM-DD)"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
Entry = Data.define(:test_case_key, :reason, :expires_on) do
|
|
47
|
+
def expired? = Date.today > expires_on
|
|
48
|
+
|
|
49
|
+
def matches?(key)
|
|
50
|
+
test_case_key == key
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def obsolete?(test_run, budget)
|
|
54
|
+
test_case = test_run.test_cases.find { |tc| tc.key == test_case_key }
|
|
55
|
+
|
|
56
|
+
if test_case.nil?
|
|
57
|
+
:stale
|
|
58
|
+
elsif test_case.within?(budget)
|
|
59
|
+
:unnecessary
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def to_h
|
|
64
|
+
{"test_case" => test_case_key, "reason" => reason, "expires_on" => expires_on.to_s}
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TestBudget
|
|
4
|
+
class Audit
|
|
5
|
+
Result = Data.define(:violations, :warnings) do
|
|
6
|
+
def passed? = violations.empty?
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def initialize(budget_path:, tolerant: false)
|
|
10
|
+
@budget_path = budget_path
|
|
11
|
+
@tolerant = tolerant
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def perform
|
|
15
|
+
budget = Budget.load(@budget_path)
|
|
16
|
+
test_run = Parser::Rspec.parse(budget.timings_path)
|
|
17
|
+
result = Auditor.new(budget, tolerant: @tolerant).audit(test_run)
|
|
18
|
+
Reporter.new(budget_path: @budget_path).report(result)
|
|
19
|
+
|
|
20
|
+
result
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TestBudget
|
|
4
|
+
class Auditor
|
|
5
|
+
TOLERANCE = 0.1
|
|
6
|
+
|
|
7
|
+
def initialize(budget, tolerant: false)
|
|
8
|
+
@budget = tolerant ? budget.inflate_by(TOLERANCE) : budget
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def audit(test_run)
|
|
12
|
+
Audit::Result.new(
|
|
13
|
+
violations: violations_for(test_run),
|
|
14
|
+
warnings: warnings_for(test_run)
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def violations_for(test_run)
|
|
21
|
+
violations = test_run.test_cases.filter_map { |tc| tc.violation_for(@budget) }
|
|
22
|
+
violations << test_run.over?(@budget)
|
|
23
|
+
violations.compact
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def warnings_for(test_run)
|
|
27
|
+
@budget.allowlist.to_a.filter_map do |entry|
|
|
28
|
+
kind = entry.obsolete?(test_run, @budget)
|
|
29
|
+
Warning.new(entry: entry, kind: kind) if kind
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module TestBudget
|
|
6
|
+
class Budget
|
|
7
|
+
class Estimate
|
|
8
|
+
TOLERANCE = 0.10
|
|
9
|
+
PER_TEST_CASE_DEFAULTS = {"default" => 3, "system" => 6, "request" => 3, "model" => 1.5}.freeze
|
|
10
|
+
|
|
11
|
+
def initialize(timings_path: nil, force: false)
|
|
12
|
+
@timings_path = timings_path
|
|
13
|
+
@force = force
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def generate
|
|
17
|
+
guard_existing_config!
|
|
18
|
+
|
|
19
|
+
if @timings_path
|
|
20
|
+
generate_from_results
|
|
21
|
+
else
|
|
22
|
+
generate_defaults
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
puts "Created #{DEFAULT_BUDGET_PATH}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def guard_existing_config!
|
|
31
|
+
return if @force || !File.exist?(DEFAULT_BUDGET_PATH)
|
|
32
|
+
|
|
33
|
+
raise TestBudget::Error, "#{DEFAULT_BUDGET_PATH} already exists. Use --force to overwrite."
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def generate_from_results
|
|
37
|
+
test_run = Parser::Rspec.parse(@timings_path)
|
|
38
|
+
suite_budget = (test_run.wall_time * (1 + TOLERANCE)).ceil
|
|
39
|
+
per_test_case_limits = derive_per_test_case(test_run.test_cases)
|
|
40
|
+
|
|
41
|
+
budget = build_budget(
|
|
42
|
+
timings_path: @timings_path,
|
|
43
|
+
suite_max_duration: suite_budget,
|
|
44
|
+
per_test_case_limits: per_test_case_limits
|
|
45
|
+
)
|
|
46
|
+
budget.save
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def generate_defaults
|
|
50
|
+
build_budget(
|
|
51
|
+
timings_path: DEFAULT_TIMINGS_PATH,
|
|
52
|
+
per_test_case_limits: PER_TEST_CASE_DEFAULTS
|
|
53
|
+
).save
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def build_budget(timings_path:, per_test_case_limits:, suite_max_duration: nil)
|
|
57
|
+
types = per_test_case_limits.except("default").transform_keys(&:to_sym)
|
|
58
|
+
|
|
59
|
+
Budget.new(
|
|
60
|
+
path: DEFAULT_BUDGET_PATH,
|
|
61
|
+
timings_path: timings_path,
|
|
62
|
+
suite: Budget::Suite.new(max_duration: suite_max_duration),
|
|
63
|
+
per_test_case: Budget::PerTestCase.new(
|
|
64
|
+
default: per_test_case_limits["default"],
|
|
65
|
+
types: types
|
|
66
|
+
),
|
|
67
|
+
allowlist: Allowlist.new
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def derive_per_test_case(test_cases)
|
|
72
|
+
grouped = test_cases.group_by { |tc| tc.type.to_s }
|
|
73
|
+
|
|
74
|
+
PER_TEST_CASE_DEFAULTS.merge(
|
|
75
|
+
grouped.transform_values { |cases|
|
|
76
|
+
Statistics.p99(cases.map(&:duration), tolerance: TOLERANCE)
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|