Skip to content

Commit

Permalink
Merge pull request #6 from sambart19/master
Browse files Browse the repository at this point in the history
Better shard balancing
  • Loading branch information
DamienBitrise authored Dec 9, 2020
2 parents 297eba4 + 0b9758e commit 97d03a7
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ myProj.parse(function (err) {

log("Tests found in test path: ", classNameTests.length);

const shard_size = Math.ceil(classNameTests.length / SHARDS);
const classNameShards = shard(classNameTests, shard_size);
const classNameShards = shard(classNameTests, SHARDS);
log('Shards: ', classNameShards.length)

if(DEBUG){
Expand Down Expand Up @@ -145,8 +144,7 @@ function updateTestPlan(shards){
let testPlanJson = JSON.parse(jsonString.replace(/\\\//g, "~"));
let otherTargets = testPlanJson.testTargets.filter((target) => target.target.name != TARGET)

const target_shard_size = Math.ceil(otherTargets.length / SHARDS);
const otherTargetsShards = shard(otherTargets, target_shard_size);
const otherTargetsShards = shard(otherTargets, SHARDS);

log('otherTargetsShards:', otherTargetsShards);

Expand Down Expand Up @@ -225,8 +223,7 @@ function addTestPlans(main_group_uuid, shards){
let defaultOptions = getDefaulOptions(schemeJson);

let otherTargets = getOtherTargets(schemeJson);
const target_shard_size = Math.ceil(otherTargets.length / SHARDS);
const otherTargetsShards = shard(otherTargets, target_shard_size);
const otherTargetsShards = shard(otherTargets, SHARDS);

log('otherTargetsShards:', otherTargetsShards);

Expand Down Expand Up @@ -492,14 +489,20 @@ function createTestPlan(defaultOptions, testTargets){
return JSON.stringify(testPlan).replace(/~/g, '\\/');
}

function shard(arr, howMany) {
let newArr = []; start = 0; end = howMany;
for(let i=1; i<= Math.ceil(arr.length / howMany); i++) {
newArr.push(arr.slice(start, end));
start = start + howMany;
end = end + howMany
function shard(arr, numShards) {
const newArr = [];
// initialize array of shards
for (let s = 0; s < numShards; s++) {
newArr.push([])
}
return newArr;

// iterate through shards to maintain balance
for (let i = 0; i < arr.length; i++) {
const mod = i % numShards
newArr[mod].push(arr[i])
}

return newArr
}

function log(msg, obj){
Expand Down

0 comments on commit 97d03a7

Please sign in to comment.