import { performance } from 'perf_hooks';

export const measurePerformance = (fn: () => void, description: string) => {
  const start = performance.now();
  fn();
  const end = performance.now();
  console.log(`${description}: ${(end - start).toFixed(2)}ms`);
  return end - start;
};

export const generateMockData = (count: number) => {
  return Array.from({ length: count }, (_, i) => ({
    id: `item-${i}`,
    name: `Item ${i}`,
    description: `This is a mock item with id ${i}`,
    price: Math.floor(Math.random() * 1000) + 1,
    stock: Math.floor(Math.random() * 100) + 1,
    category: `Category ${Math.floor(Math.random() * 10)}`,
    status: Math.random() > 0.5 ? 'active' : 'inactive',
  }));
};

export const simulateNetworkDelay = (ms: number) => {
  return new Promise(resolve => setTimeout(resolve, ms));
};

export const testVirtualListPerformance = (itemCount: number) => {
  console.log(`Testing virtual list performance with ${itemCount} items...`);
  const mockData = generateMockData(itemCount);
  
  const renderTime = measurePerformance(() => {
    // 模拟虚拟列表渲染
    console.log(`Rendering ${mockData.length} items with virtual list`);
  }, 'Virtual list render time');
  
  return renderTime;
};

export const testLazyLoadPerformance = (imageCount: number) => {
  console.log(`Testing lazy load performance with ${imageCount} images...`);
  
  const loadTime = measurePerformance(() => {
    // 模拟图片懒加载
    console.log(`Lazy loading ${imageCount} images`);
  }, 'Lazy load time');
  
  return loadTime;
};