## Test suite for performance benchmarking
## Task 43: Performance benchmarking

import unittest, os, times, strutils
import ../src/nimpak/benchmark
import ../src/nimpak/cas

suite "Performance Benchmarking Tests":

  var testDir: string
  var casManager: CasManager
  
  setup:
    testDir = getTempDir() / "nip_bench_test_" & $epochTime().int
    createDir(testDir)
    casManager = initCasManager(testDir / "cas", testDir / "cas" / "system")

  teardown:
    removeDir(testDir)

  test "CAS hash benchmark":
    let result = benchmarkCasHash(1024, 10)
    
    check result.name.contains("Hash")
    check result.iterations == 10
    check result.bytesProcessed == 1024 * 10
    check result.avgTime >= 0

  test "CAS store benchmark":
    let result = benchmarkCasStore(casManager, 1024, 5)
    
    check result.name.contains("Store")
    check result.iterations == 5
    check result.bytesProcessed == 1024 * 5

  test "CAS retrieve benchmark":
    let result = benchmarkCasRetrieve(casManager, 1024, 5)
    
    check result.name.contains("Retrieve")
    check result.iterations == 5

  test "CAS exists benchmark":
    let result = benchmarkCasExists(casManager, 10)
    
    check result.name.contains("Exists")
    check result.iterations == 10
    check result.opsPerSec >= 0

  test "Deduplication benchmark":
    let result = benchmarkDeduplication(casManager, 1024, 0.5, 10)
    
    check result.name.contains("Deduplication")
    check result.iterations == 10

  test "Benchmark result formatting":
    let result = BenchmarkResult(
      name: "Test",
      iterations: 100,
      totalTime: 1.5,
      avgTime: 15.0,
      minTime: 10.0,
      maxTime: 25.0,
      stdDev: 3.0,
      opsPerSec: 66.67,
      bytesProcessed: 102400,
      throughputMBps: 0.065
    )
    
    let formatted = formatBenchmarkResult(result)
    
    check formatted.contains("Test:")
    check formatted.contains("Iterations: 100")
    check formatted.contains("Avg time:")
    check formatted.contains("Ops/sec:")
    check formatted.contains("Throughput:")

  test "Benchmark suite runner (quick mode)":
    # Run in quick mode for testing
    let suite = runCasBenchmarks(testDir / "bench_cas", quick = true)
    
    check suite.name == "CAS Performance Benchmarks"
    check suite.results.len > 0
    check suite.endTime >= suite.startTime

  test "JSON report generation":
    let suite = BenchmarkSuite(
      name: "Test Suite",
      startTime: now(),
      endTime: now(),
      results: @[
        BenchmarkResult(name: "Test1", iterations: 10, totalTime: 1.0, 
                        avgTime: 100.0, minTime: 90.0, maxTime: 110.0,
                        stdDev: 5.0, opsPerSec: 10.0)
      ]
    )
    
    let report = generateBenchmarkReport(suite)
    
    check report.contains("\"suite\": \"Test Suite\"")
    check report.contains("\"name\": \"Test1\"")
    check report.contains("\"iterations\": 10")

  test "System comparison placeholder":
    let nipResult = BenchmarkResult(
      name: "Store 1KB",
      avgTime: 0.5
    )
    
    let comparison = compareBenchmarks(nipResult, 1.0, 1.5, 0.8)
    
    check comparison.contains("Store 1KB")
    check comparison.contains("NIP")
    check comparison.contains("Flatpak")
    check comparison.contains("Snap")
    check comparison.contains("Docker")
