Skip to content

Commit

Permalink
perf: optimize index.spec.mjs for memory (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno authored Dec 19, 2024
1 parent 19f6eff commit 47a3fe4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/regression.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

{
NODE_OPTIONS=--max_old_space_size=8192 npm test && exit 0
NODE_OPTIONS='--max_old_space_size=2048' npm test && exit 0
} || {
echo "Regression failed, rolling back..."
cd "${GITHUB_WORKSPACE}" && git checkout -- . && exit 0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
- run: npm i --no-save warframe-worldstate-data@^2
- name: Test
env:
NODE_OPTIONS: '--max_old_space_size=5120'
NODE_OPTIONS: '--max_old_space_size=2048'
run: npm test
- name: Regression
env:
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"chalk": "^5.3.0",
"cheerio": "^1.0.0",
"decache": "^4.6.2",
"expose-gc": "^1.0.0",
"get-image-colors": "^4.0.1",
"https-proxy-agent": "^7.0.5",
"husky": "^9.1.5",
Expand Down
51 changes: 36 additions & 15 deletions test/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert';
import { resolve } from 'node:path';
import { createRequire } from 'module';

import gc from 'expose-gc/function.js';
import { expect } from 'chai';

import dedupe from '../build/dedupe.mjs';
Expand All @@ -14,8 +15,14 @@ let Items;
let data;
const inits = [];

const importFresh = async (path) => {
return (await import(`${path}?update=${Date.now()}`)).default;
/**
* Use for fresh imports
* @param {string} path to import
* @param {string?} discriminator extra query param to force cache bust
* @returns {Promise<module>}
*/
const importFresh = async (path, discriminator) => {
return (await import(`${path}?discriminator=${discriminator}`)).default;
};
const wrapConstr = async (opts) => {
const before = Date.now();
Expand All @@ -27,26 +34,34 @@ const wrapConstr = async (opts) => {

const namedExclusions = ['Excalibur Prime', 'Helminth'];

for (const base of ['index.js', 'index.mjs']) {
const setupItems = async () => {
Items = await importFresh(itemPath);
data = Object.freeze({
items: await wrapConstr(),
warframes: await wrapConstr({ category: ['Warframes', 'Archwing'], i18n: 'en', i18nOnObject: true }),
weapons: await wrapConstr({
category: ['Primary', 'Secondary', 'Melee', 'Arch-Melee', 'Arch-Gun'],
i18n: 'en',
i18nOnObject: true,
}),
});
};

const test = (base) => {
describe(base, () => {
before(async () => {
itemPath = resolve(`./${base}`);
Items = await importFresh(itemPath);
data = Object.freeze({
items: await wrapConstr(),
warframes: await wrapConstr({ category: ['Warframes', 'Archwing'], i18n: 'en', i18nOnObject: true }),
weapons: await wrapConstr({
category: ['Primary', 'Secondary', 'Melee', 'Arch-Melee', 'Arch-Gun'],
i18n: 'en',
i18nOnObject: true,
}),
});
await setupItems();
});
beforeEach(async () => {
Items = await importFresh(itemPath);
gc();
await setupItems();
});
afterEach(async () => {
delete require.cache[itemPath];
Items = undefined;
data = undefined;
gc();
});
it('should contain items when initializing.', async () => {
const items = await wrapConstr();
Expand Down Expand Up @@ -86,7 +101,9 @@ for (const base of ['index.js', 'index.mjs']) {
assert(primes.length < items.length);
});
describe('i18n', () => {
beforeEach(gc);
it('should populate with a truthy boolean', async () => {
Items = await importFresh(itemPath, Date.now());
const items = await wrapConstr({ category: ['Mods'], i18n: ['es', 'tr'] });
assert(!!items.i18n[items[0].uniqueName].tr);
assert(!!items.i18n[items[0].uniqueName].es);
Expand All @@ -108,6 +125,7 @@ for (const base of ['index.js', 'index.mjs']) {
});
});
describe('drops', () => {
beforeEach(gc);
it('should not have drops for hikou', async () => {
const items = await wrapConstr({ category: ['Secondary'] });
const hikouMatches = items.filter((i) => i.name === 'Hikou');
Expand Down Expand Up @@ -157,6 +175,7 @@ for (const base of ['index.js', 'index.mjs']) {
});
});
describe('integrity', async () => {
beforeEach(gc);
it('weapons should only have 1 result for Mausolon', () => {
const matches = data.weapons
.filter((i) => i.name === 'Mausolon')
Expand Down Expand Up @@ -302,4 +321,6 @@ for (const base of ['index.js', 'index.mjs']) {
expect(helminth.abilities.length).lessThan(20);
});
});
}
};

['index.js', 'index.mjs'].forEach(test);

0 comments on commit 47a3fe4

Please sign in to comment.