Skip to content

Commit

Permalink
fix(generate-matrix): dont load-balance when paths is fewer than batches
Browse files Browse the repository at this point in the history
  • Loading branch information
aschwenn committed Apr 11, 2024
1 parent ab80885 commit 2166ae3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions dist/839.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/839.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/helpers/generate-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
/* eslint-disable functional/no-let */

import { HelperInputs } from '../types/generated';
import { chunk } from 'lodash';
import { chunk, sum } from 'lodash';

export class GenerateMatrix extends HelperInputs {
paths = '';
Expand All @@ -26,15 +26,15 @@ export class GenerateMatrix extends HelperInputs {
export const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_sizes }: GenerateMatrix) => {
const matrixValues = paths.split(/[\n,]/);
const batches = Number(_batches);
if (!load_balancing_sizes) {
if (!load_balancing_sizes || matrixValues.length <= batches) {
return {
include: chunk(matrixValues, Math.ceil(matrixValues.length / batches)).map(chunk => ({ path: chunk.join(',') }))
};
}
const loadBalancingSizes = load_balancing_sizes.split(/[\n,]/).map(size => Number(size));
if (loadBalancingSizes.length !== matrixValues.length)
throw new Error('load_balancing_sizes input must have the same length as paths input');
const targetLoadSize = loadBalancingSizes.reduce((acc, size) => acc + size, 0) / batches;
const targetLoadSize = sum(loadBalancingSizes) / batches;
const loadBalancedPaths: string[] = [];
let currentLoadSize = 0;
let currentBatch: string[] = [];
Expand Down
24 changes: 24 additions & 0 deletions test/helpers/generate-matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,28 @@ describe('generateMatrix', () => {
]
});
});

it("shouldn't load balance if the number of paths is lower than the desired batches", () => {
const result = generateMatrix({
paths: 'path/1,path/2,path/3,path/4',
load_balancing_sizes: '2,2,1,12',
batches: '8'
});
expect(result).toEqual({
include: [
{
path: 'path/1'
},
{
path: 'path/2'
},
{
path: 'path/3'
},
{
path: 'path/4'
}
]
});
});
});

0 comments on commit 2166ae3

Please sign in to comment.