/**
* 缓存性能测试
* 验证缓存的命中率、响应时间等性能指标
*/
/// <reference path="../global.d.ts" />
import { getTasks } from '../../src/tools/getTasks';
import { getProjectById } from '../../src/tools/getProjectById';
describe('Cache Performance', () => {
beforeEach(() => {
global.testCache.clear();
});
test('should improve response time with cache', async () => {
const projectId = 2;
// 第一次请求(无缓存)
const start1 = Date.now();
await getProjectById(global.testClient, global.testCache, projectId);
const time1 = Date.now() - start1;
// 第二次请求(有缓存)
const start2 = Date.now();
await getProjectById(global.testClient, global.testCache, projectId);
const time2 = Date.now() - start2;
// 缓存命中应该显著更快
expect(time2).toBeLessThan(time1 * 0.5);
console.log('Cache hit much faster:', time2, 'ms vs', time1, 'ms');
});
test('should limit cache size', async () => {
// 填充缓存到最大大小
for (let i = 0; i < 150; i++) {
global.testCache.set('test:' + i, 'value:' + i, 5 * 60 * 1000);
}
// 缓存应该限制在最大大小
expect(global.testCache.size()).toBeLessThanOrEqual(100);
console.log('Cache size limited correctly');
});
test('should maintain cache statistics', async () => {
const projectId = 2;
// 第一次请求(缓存未命中)
await getProjectById(global.testClient, global.testCache, projectId);
// 第二次请求(缓存命中)
await getProjectById(global.testClient, global.testCache, projectId);
const stats = global.testCache.getStats();
expect(stats.hits).toBeGreaterThan(0);
expect(stats.hitRate).toBeGreaterThan(0);
console.log('Cache hit rate:', (stats.hitRate * 100).toFixed(2), '%');
});
});