Skip to content

Commit

Permalink
Add perf test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jul 30, 2024
1 parent 8feed72 commit c98c294
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ class CachedDict implements CachingDictionary {
// console.log(`CachedDict for ${this.name}`);
}

has = (word: string): boolean => {
if (logRequests) {
const time = performance.now() - startTime;
const value = this.#has(word);
log.push({ time, method: 'has', word, value });
return value;
}
return this.#has(word);
};

#has = autoCache((word: string) => this.dict.has(word, this.options), DefaultAutoCacheSize);
has = logRequests
? (word: string): boolean => {
const time = performance.now() - startTime;
const value = this.#has(word);
log.push({ time, method: 'has', word, value });
return value;
}
: this.#has;

readonly isNoSuggestWord = autoCache(
(word: string) => this.dict.isNoSuggestWord(word, this.options),
DefaultAutoCacheSize,
Expand Down
54 changes: 54 additions & 0 deletions packages/cspell-dictionary/src/perf/has.perf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,45 @@ suite('dictionary has sampling', async (test) => {
});
});

suite('dictionary isForbidden sampling', async (test) => {
const words1 = genWords(10_000);
const words2 = genWords(1000);
const words3 = genWords(1000);

const sampleIdx = genSamples(100_000, words1.length);
const wordsSample = sampleIdx.map((i) => words1[i]);

const dict = createSpellingDictionary(words1, 'test', import.meta.url);
const dict2 = createSpellingDictionary(words2, 'test2', import.meta.url);
const dict3 = createSpellingDictionary(words3, 'test3', import.meta.url);

const dictCol = createCollection([dict, dict2, dict3], 'test-collection');
const dictColRev = createCollection([dict3, dict2, dict], 'test-collection-reverse');

const cacheDictSingle = createCachingDictionary(dict, {});
const cacheDictCol = createCachingDictionary(dictCol, {});

test('dictionary isForbidden 100k words', () => {
checkForForbiddenWords(dict, wordsSample);
});

test('collection isForbidden 100k words', () => {
checkForForbiddenWords(dictCol, wordsSample);
});

test('collection reverse isForbidden 100k words', () => {
checkForForbiddenWords(dictColRev, wordsSample);
});

test('cache dictionary isForbidden 100k words', () => {
checkForForbiddenWords(cacheDictSingle, wordsSample);
});

test('cache collection isForbidden 100k words', () => {
checkForForbiddenWords(cacheDictCol, wordsSample);
});
});

function checkWords(dict: { has: (word: string) => boolean }, words: string[], expected = true, totalChecks = 100_000) {
let has = true;
const len = words.length;
Expand All @@ -179,6 +218,21 @@ function checkWords(dict: { has: (word: string) => boolean }, words: string[], e
assert(has, 'All words should be found in the dictionary');
}

function checkForForbiddenWords(
dict: { isForbidden: (word: string) => boolean },
words: string[],
totalChecks = 100_000,
) {
let result = true;
const len = words.length;
for (let i = 0; i < totalChecks; ++i) {
const word = words[i % len];
const r = !dict.isForbidden(word);
result = r && result;
}
assert(result, 'All words should not be forbidden');
}

function genWords(count: number, includeForbidden = true): string[] {
const setOfWords = new Set(loremIpsum({ count }).split(' '));

Expand Down

0 comments on commit c98c294

Please sign in to comment.