import { AuthService } from './AuthService';

// 性能测试：AuthService 性能测试
async function performanceTest() {
  console.log('🚀 AuthService Performance Test');
  console.log('================================');

  // 测试 1: JWT 令牌生成性能
  console.log('\n--- Test 1: JWT Token Generation ---');
  const payload = {
    userId: 'user1',
    username: 'testuser',
    role: 'ADMIN' as any,
    tenantId: 'tenant1',
    shopId: 'shop1'
  };

  const startTime = Date.now();
  const iterations = 1000;
  for (let i = 0; i < iterations; i++) {
    AuthService.generateToken(payload);
  }
  const endTime = Date.now();
  const duration = endTime - startTime;
  console.log(`Generated ${iterations} tokens in ${duration}ms`);
  console.log(`Average: ${(duration / iterations).toFixed(3)}ms per token`);

  // 测试 2: 密码哈希性能
  console.log('\n--- Test 2: Password Hashing ---');
  const password = 'password123';
  const hashStartTime = Date.now();
  for (let i = 0; i < 100; i++) {
    AuthService.hashPassword(password);
  }
  const hashEndTime = Date.now();
  const hashDuration = hashEndTime - hashStartTime;
  console.log(`Hashed ${100} passwords in ${hashDuration}ms`);
  console.log(`Average: ${(hashDuration / 100).toFixed(3)}ms per hash`);

  // 测试 3: 密码验证性能
  console.log('\n--- Test 3: Password Verification ---');
  const hashedPassword = AuthService.hashPassword(password);
  const verifyStartTime = Date.now();
  for (let i = 0; i < 1000; i++) {
    AuthService.verifyPassword(password, hashedPassword);
  }
  const verifyEndTime = Date.now();
  const verifyDuration = verifyEndTime - verifyStartTime;
  console.log(`Verified ${1000} passwords in ${verifyDuration}ms`);
  console.log(`Average: ${(verifyDuration / 1000).toFixed(3)}ms per verification`);

  // 测试 4: TOTP 密钥生成性能
  console.log('\n--- Test 4: TOTP Secret Generation ---');
  const totpStartTime = Date.now();
  for (let i = 0; i < 1000; i++) {
    AuthService.generateTOTPSecret();
  }
  const totpEndTime = Date.now();
  const totpDuration = totpEndTime - totpStartTime;
  console.log(`Generated ${1000} TOTP secrets in ${totpDuration}ms`);
  console.log(`Average: ${(totpDuration / 1000).toFixed(3)}ms per secret`);

  console.log('\n✅ Performance test completed');
}

performanceTest().catch(console.error);
