/**
* 并发性能测试
* 验证系统在高并发情况下的性能
*/
/// <reference path="../global.d.ts" />
import { getTasks } from '../../src/tools/getTasks';
import { getProjectById } from '../../src/tools/getProjectById';
describe('Concurrency Performance', () => {
beforeEach(() => {
global.testCache.clear();
});
test('should handle multiple concurrent requests', async () => {
// 创建并发请求
const requests = Array.from({ length: 20 }, () =>
getTasks(global.testClient, global.testCache, { projectId: 2, page: 1, limit: 10 })
);
const start = Date.now();
await Promise.all(requests);
const duration = Date.now() - start;
// 应该能够在合理时间内处理所有请求
expect(duration).toBeLessThan(10000);
console.log('Handled 20 concurrent requests in', duration, 'ms');
});
test('should maintain cache consistency under concurrency', async () => {
const projectId = 2;
// 并发访问同一资源
const requests = Array.from({ length: 10 }, () =>
getProjectById(global.testClient, global.testCache, projectId)
);
await Promise.all(requests);
// 缓存应该只有一个条目
const stats = global.testCache.getStats();
expect(stats.size).toBe(1);
console.log('Cache maintained consistency with', stats.size, 'entries');
});
test('should handle batch operations concurrently', async () => {
const operations = Array.from({ length: 5 }, (_, i) => ({
projectId: i + 1,
page: 1,
limit: 10
}));
const start = Date.now();
await Promise.all(
operations.map(params => getTasks(global.testClient, global.testCache, params))
);
const duration = Date.now() - start;
expect(duration).toBeLessThan(15000);
console.log('Handled 5 batch operations in', duration, 'ms');
});
});