Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SPLIT_TIME_THRESHOLD #171

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ $ npx cypress run --env split=3,splitFile=timings.json

For specs not in the timings file, it will use average duration of the known specs. The timings file might not exist, in this case the specs are split by name. At the end of the run, the duration of all run specs is printed and can be saved into the timings JSON file. **Note:** you would need to combine the timings from different runners into a single JSON file yourself.

If the timings file does not exist yet, the timings will be written into the file after the run finishes. If the file exists, and the new timings have new entries or the existing entries are off by more than 10% duration, the merged file is written back. Timing for specs without any passes tests or with failed tests is ignored.
If the timings file does not exist yet, the timings will be written into the file after the run finishes. If the file exists, and the new timings have new entries or the existing entries are off by more than 10% duration, the merged file is written back. Timing for specs without any passes tests or with failed tests is ignored. You can control the threshold to avoid changing the timings file if the times are too close. For example, to only update the timings file if any duration is different by 20% you can use the environment variable `SPLIT_TIME_THRESHOLD`

```
$ SPLIT_TIME_THRESHOLD=0.2 SPLIT_FILE=... npx cypress run ...
```

See example [bahmutov/cypress-split-timings-example](https://github.com/bahmutov/cypress-split-timings-example).

Expand Down
39 changes: 32 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ function cypressSplit(on, config) {
let SPLIT = process.env.SPLIT || config.env.split || config.env.SPLIT
let SPLIT_INDEX = process.env.SPLIT_INDEX || config.env.splitIndex
let SPLIT_FILE = process.env.SPLIT_FILE || config.env.splitFile
let SPLIT_OUTPUT_FILE = process.env.SPLIT_OUTPUT_FILE || config.env.outputFile || SPLIT_FILE
let SPLIT_OUTPUT_FILE =
process.env.SPLIT_OUTPUT_FILE || config.env.outputFile || SPLIT_FILE

console.log('cypress:split: Timings are read from %s', SPLIT_FILE)
console.log('cypress:split: Timings will be written to %s', SPLIT_OUTPUT_FILE)
console.log('%s Timings are read from %s', label, SPLIT_FILE)
console.log('%s Timings will be written to %s', label, SPLIT_OUTPUT_FILE)

// some CI systems like TeamCity provide agent index starting with 1
// let's check for SPLIT_INDEX1 and if it is set,
Expand Down Expand Up @@ -322,11 +323,31 @@ function cypressSplit(on, config) {
console.log(timingsString)

if (!foundSplitFile) {
console.log('%s writing out timings file %s', label, SPLIT_OUTPUT_FILE)
console.log(
'%s writing out timings file %s',
label,
SPLIT_OUTPUT_FILE,
)
fs.writeFileSync(SPLIT_OUTPUT_FILE, timingsString + '\n', 'utf8')
} else {
const splitFile = JSON.parse(fs.readFileSync(foundSplitFile, 'utf8'))
const hasUpdatedTimings = hasTimeDifferences(splitFile, timings, 0.1)
let splitThreshold = 0.1
if (
'SPLIT_TIME_THRESHOLD' in process.env &&
process.env.SPLIT_TIME_THRESHOLD
) {
debug(
'will use SPLIT_TIME_THRESHOLD value %s',
process.env.SPLIT_TIME_THRESHOLD,
)
splitThreshold = parseFloat(process.env.SPLIT_TIME_THRESHOLD)
debug('parsed SPLIT_TIME_THRESHOLD is %d', splitThreshold)
}
const hasUpdatedTimings = hasTimeDifferences(
splitFile,
timings,
splitThreshold,
)
if (hasUpdatedTimings) {
// TODO: merge split file with new timings
// do not forget specs not present in the current run!
Expand All @@ -347,8 +368,12 @@ function cypressSplit(on, config) {
fs.writeFileSync(SPLIT_OUTPUT_FILE, mergedText + '\n', 'utf8')
} else {
console.log('%s spec timings unchanged', label)
if(SPLIT_OUTPUT_FILE !== SPLIT_FILE){
console.log('%s writing out timings file %s', label, SPLIT_OUTPUT_FILE)
if (SPLIT_OUTPUT_FILE !== SPLIT_FILE) {
console.log(
'%s writing out timings file %s',
label,
SPLIT_OUTPUT_FILE,
)
fs.writeFileSync(SPLIT_OUTPUT_FILE, timingsString + '\n', 'utf8')
}
}
Expand Down