mattermost/e2e-tests/cypress/utils/even_distribution.js
2023-03-28 18:10:00 +02:00

48 lines
1.2 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
function* distributeItems(total, divider) {
if (divider === 0) {
yield 0;
} else {
let rest = total % divider;
const result = total / divider;
for (let i = 0; i < divider; i++) {
if (rest-- > 0) {
yield Math.ceil(result);
} else {
yield Math.floor(result);
}
}
}
}
function getTestFilesIdentifier(numberOfTestFiles, part, of) {
const PART = parseInt(part, 10) || 1;
const OF = parseInt(of, 10) || 1;
if (PART > OF) {
throw new Error(`"--part=${PART}" should not be greater than "--of=${OF}"`);
}
const distributions = [];
for (const member of distributeItems(numberOfTestFiles, OF)) {
distributions.push(member);
}
const indexedPart = (PART - 1);
let start = 0;
for (let i = 0; i < indexedPart; i++) {
start += distributions[i];
}
const end = distributions[indexedPart] + start;
const count = distributions[indexedPart];
return {start, end, count};
}
module.exports = {
getTestFilesIdentifier,
};