-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split specs based on timings from a JSON file (#125)
* start work on splitting specs based on timings * testing timings * add readme file
- Loading branch information
Showing
7 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/// <reference types="cypress" /> | ||
|
||
const { splitByDuration } = require('../../src/timings') | ||
|
||
it('splits specs based on timings', () => { | ||
const list = [ | ||
{ | ||
spec: 'a', | ||
duration: 1000, | ||
}, | ||
{ | ||
spec: 'b', | ||
duration: 6000, | ||
}, | ||
{ | ||
spec: 'c', | ||
duration: 6000, | ||
}, | ||
{ | ||
spec: 'd', | ||
duration: 1000, | ||
}, | ||
] | ||
const { chunks, sums } = splitByDuration(2, list) | ||
expect(chunks, 'chunks').to.deep.equal([ | ||
[ | ||
{ | ||
spec: 'b', | ||
duration: 6000, | ||
}, | ||
{ | ||
spec: 'a', | ||
duration: 1000, | ||
}, | ||
], | ||
[ | ||
{ | ||
spec: 'c', | ||
duration: 6000, | ||
}, | ||
{ | ||
spec: 'd', | ||
duration: 1000, | ||
}, | ||
], | ||
]) | ||
expect(sums, 'duration sums').to.deep.equal([7000, 7000]) | ||
}) | ||
|
||
it('splits specs based on timings, single result', () => { | ||
const list = [ | ||
{ | ||
spec: 'a', | ||
duration: 1000, | ||
}, | ||
{ | ||
spec: 'b', | ||
duration: 6000, | ||
}, | ||
{ | ||
spec: 'c', | ||
duration: 6000, | ||
}, | ||
{ | ||
spec: 'd', | ||
duration: 1000, | ||
}, | ||
] | ||
const { chunks, sums } = splitByDuration(4, list) | ||
expect(chunks).to.deep.equal([ | ||
[ | ||
{ | ||
spec: 'b', | ||
duration: 6000, | ||
}, | ||
], | ||
[ | ||
{ | ||
spec: 'c', | ||
duration: 6000, | ||
}, | ||
], | ||
[ | ||
{ | ||
spec: 'a', | ||
duration: 1000, | ||
}, | ||
], | ||
|
||
[ | ||
{ | ||
spec: 'd', | ||
duration: 1000, | ||
}, | ||
], | ||
]) | ||
expect(sums, 'duration sums').to.deep.equal([6000, 6000, 1000, 1000]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Split the list of items into "n" lists by "duration" property | ||
* in each item. Sorts the list first, then round-robin fills | ||
* the lists. Put the item into the list with the smallest sum. | ||
* @param {number} n Number of output lists | ||
* @returns {any} | ||
*/ | ||
function splitByDuration(n, list) { | ||
const result = [] | ||
const sums = [] | ||
for (let k = 0; k < n; k += 1) { | ||
result.push([]) | ||
sums.push(0) | ||
} | ||
const sorted = list.sort((a, b) => b.duration - a.duration) | ||
sorted.forEach((item) => { | ||
const smallestIndex = sums.reduce((currentSmallestIndex, value, index) => { | ||
return value < sums[currentSmallestIndex] ? index : currentSmallestIndex | ||
}, 0) | ||
result[smallestIndex].push(item) | ||
sums[smallestIndex] += item.duration | ||
}) | ||
|
||
// console.table(result) | ||
// console.table(sums) | ||
return { chunks: result, sums } | ||
} | ||
|
||
module.exports = { splitByDuration } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"durations": [ | ||
{ | ||
"spec": "cypress/e2e/chunks.cy.js", | ||
"duration": 300 | ||
}, | ||
{ | ||
"spec": "cypress/e2e/spec-a.cy.js", | ||
"duration": 10050 | ||
}, | ||
{ | ||
"spec": "cypress/e2e/spec-b.cy.js", | ||
"duration": 10100 | ||
}, | ||
{ | ||
"spec": "cypress/e2e/spec-c.cy.js", | ||
"duration": 10060 | ||
}, | ||
{ | ||
"spec": "cypress/e2e/spec-d.cy.js", | ||
"duration": 10070 | ||
}, | ||
{ | ||
"spec": "cypress/e2e/timings.cy.js", | ||
"duration": 100 | ||
} | ||
] | ||
} |