/** * Performance Test: Queue Service Throughput * * Benchmarks the queue service's pure functions and validation logic * under load to verify performance characteristics. * * Covers issue #229 (ORCH-128) */ import { describe, it, expect, beforeEach, vi } from "vitest"; import { QueueService } from "../../src/queue/queue.service"; import { ConfigService } from "@nestjs/config"; describe("Performance: Queue Service", () => { let service: QueueService; const mockValkeyService = { getConnection: vi.fn().mockReturnValue({ host: "localhost", port: 6379, }), updateTaskStatus: vi.fn().mockResolvedValue(undefined), publishEvent: vi.fn().mockResolvedValue(undefined), }; const mockConfigService = { get: vi.fn((key: string, defaultValue?: unknown) => { const config: Record = { "orchestrator.queue.name": "perf-test-queue", "orchestrator.queue.maxRetries": 3, "orchestrator.queue.baseDelay": 1000, "orchestrator.queue.maxDelay": 60000, }; return config[key] ?? defaultValue; }), }; beforeEach(() => { vi.clearAllMocks(); service = new QueueService( mockValkeyService as never, mockConfigService as unknown as ConfigService ); }); describe("Backoff calculation performance", () => { it("should calculate 10,000 backoff delays in under 10ms", () => { const start = performance.now(); for (let i = 0; i < 10000; i++) { service.calculateBackoffDelay(i % 20, 1000, 60000); } const duration = performance.now() - start; expect(duration).toBeLessThan(10); }); it("should produce consistent results under rapid invocation", () => { const results: number[] = []; for (let attempt = 0; attempt <= 10; attempt++) { const delay = service.calculateBackoffDelay(attempt, 1000, 60000); results.push(delay); } // Verify expected exponential pattern expect(results[0]).toBe(1000); // 1000 * 2^0 expect(results[1]).toBe(2000); // 1000 * 2^1 expect(results[2]).toBe(4000); // 1000 * 2^2 expect(results[3]).toBe(8000); // 1000 * 2^3 // After attempt 6 (64000), should be capped at 60000 expect(results[6]).toBe(60000); expect(results[10]).toBe(60000); }); }); describe("Validation performance", () => { it("should validate 1000 task contexts rapidly", () => { const contexts = Array.from({ length: 1000 }, (_, i) => ({ repository: `https://git.example.com/repo-${String(i)}.git`, branch: `feature/task-${String(i)}`, workItems: [`US-${String(i).padStart(3, "0")}`], skills: ["typescript", "nestjs"], })); const start = performance.now(); for (const context of contexts) { // Validate context fields (simulates what addTask validates) expect(context.repository).toBeTruthy(); expect(context.branch).toBeTruthy(); expect(context.workItems.length).toBeGreaterThan(0); } const duration = performance.now() - start; expect(duration).toBeLessThan(100); }); }); });