Skip to content

Commit b4fb361

Browse files
committed
Clear cache on benchmark test
1 parent ae1f4df commit b4fb361

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ jobs:
1212
cache: 'yarn'
1313
- name: Install dependencies
1414
run: yarn install --frozen-lockfile
15-
- name: Run tests
16-
run: yarn test
15+
- name: Run unit tests
16+
run: yarn run test:unit
17+
- name: Run benchmark tests
18+
run: yarn run test:benchmark
1719
- name: Build application
1820
run: yarn run build

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"postbuild": "node scripts/stamp.js < dest/simple-jekyll-search.min.js > dest/simple-jekyll-search.min.js.tmp && mv dest/simple-jekyll-search.min.js.tmp dest/simple-jekyll-search.min.js && yarn run copy-example-code",
1616
"copy-example-code": "cp dest/simple-jekyll-search.min.js docs/assets/js/",
1717
"test": "vitest run --coverage",
18+
"test:unit": "vitest run --exclude '**/performance/**' --coverage",
19+
"test:benchmark": "NODE_OPTIONS='--max-old-space-size=4096' vitest run tests/performance/",
1820
"test:watch": "vitest",
1921
"start": "cd docs; jekyll serve",
2022
"start:docs": "cd docs && bundle exec jekyll serve"

src/SearchStrategies/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ export class SearchStrategy implements Matcher {
2424
private readonly cache: SearchCache<CachedResult>;
2525

2626
constructor(
27-
findMatchesFunction: (text: string, criteria: string) => MatchInfo[]
27+
findMatchesFunction: (text: string, criteria: string) => MatchInfo[],
28+
cacheOptions?: { maxSize?: number; ttl?: number }
2829
) {
2930
this.findMatchesFunction = findMatchesFunction;
30-
this.cache = new SearchCache<CachedResult>({ maxSize: 500, ttl: 60000 });
31+
this.cache = new SearchCache<CachedResult>({
32+
maxSize: cacheOptions?.maxSize ?? 100,
33+
ttl: cacheOptions?.ttl ?? 60000
34+
});
3135
}
3236

3337
matches(text: string | null, criteria: string): boolean {

tests/SearchStrategies/SearchStrategy.caching.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, beforeEach } from 'vitest';
1+
import { describe, expect, it, beforeEach, afterEach } from 'vitest';
22
import { SearchStrategy } from '../../src/SearchStrategies/types';
33

44
describe('SearchStrategy Caching', () => {
@@ -29,6 +29,12 @@ describe('SearchStrategy Caching', () => {
2929
strategy = new SearchStrategy(findMatchesFunction);
3030
});
3131

32+
afterEach(() => {
33+
if (strategy && strategy.clearCache) {
34+
strategy.clearCache();
35+
}
36+
});
37+
3238
describe('matches() caching', () => {
3339
it('should cache matches() results', () => {
3440
strategy.matches('hello world', 'hello');

tests/performance/benchmark.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeEach } from 'vitest';
1+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
22
import { SearchStrategy } from '../../src/SearchStrategies/types';
33
import { PerformanceMonitor } from '../utils/PerformanceMonitor';
44

@@ -20,10 +20,6 @@ describe('Performance Benchmarks', () => {
2020
let monitor: PerformanceMonitor;
2121

2222
beforeEach(() => {
23-
const matchFunction = (text: string, criteria: string) => {
24-
return text.toLowerCase().includes(criteria.toLowerCase());
25-
};
26-
2723
const findMatchesFunction = (text: string, criteria: string) => {
2824
const lowerText = text.toLowerCase();
2925
const lowerCriteria = criteria.toLowerCase();
@@ -47,10 +43,19 @@ describe('Performance Benchmarks', () => {
4743
return matches;
4844
};
4945

50-
strategy = new SearchStrategy(matchFunction, findMatchesFunction);
46+
strategy = new SearchStrategy(findMatchesFunction);
5147
monitor = new PerformanceMonitor();
5248
});
5349

50+
afterEach(() => {
51+
if (strategy && strategy.clearCache) {
52+
strategy.clearCache();
53+
}
54+
if (monitor) {
55+
monitor.reset();
56+
}
57+
});
58+
5459
describe('Cache Hit Rate', () => {
5560
it('should achieve high cache hit rate on repeated searches', () => {
5661
for (let i = 0; i < 100; i++) {

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default defineConfig({
2525
test: {
2626
coverage: {
2727
provider: 'v8',
28-
reporter: ['text', 'json', 'html', 'lcov'],
28+
reporter: ['text', 'lcov'],
2929
include: ['src/**/*.ts'],
3030
exclude: [
3131
'**/*.d.ts',

0 commit comments

Comments
 (0)