runVisionEngineBenchmark static method

Future<BenchmarkResult> runVisionEngineBenchmark({
  1. required UniversalScanEngine engine,
  2. required Uint8List sampleBytes,
  3. required ScanMode mode,
  4. int runs = 1000,
  5. int width = 640,
  6. int height = 480,
})

Executes parsing performance benchmark over a specified iteration count.

Implementation

static Future<BenchmarkResult> runVisionEngineBenchmark({
  required UniversalScanEngine engine,
  required Uint8List sampleBytes,
  required ScanMode mode,
  int runs = 1000,
  int width = 640,
  int height = 480,
}) async {
  engine.initialize();
  final latencies = <double>[];
  final overallStopwatch = Stopwatch()..start();

  for (int i = 0; i < runs; i++) {
    final opStopwatch = Stopwatch()..start();
    await engine.processBytes(sampleBytes, mode, width: width, height: height);
    opStopwatch.stop();
    latencies.add(opStopwatch.elapsedMicroseconds / 1000.0);
  }

  overallStopwatch.stop();
  latencies.sort();

  final totalMs = overallStopwatch.elapsedMilliseconds.toDouble();
  final avgMs = totalMs / runs;
  final p95Index = (runs * 0.95).floor().clamp(0, runs - 1);
  final p95Ms = latencies[p95Index];
  final opsPerSec = totalMs > 0 ? (runs / (totalMs / 1000.0)) : 0.0;

  return BenchmarkResult(
    benchmarkName: '${mode.name.toUpperCase()} Vision Processing Pass ($runs runs)',
    totalRuns: runs,
    totalDuration: overallStopwatch.elapsed,
    opsPerSecond: opsPerSec,
    averageLatencyMs: avgMs,
    p95LatencyMs: p95Ms,
    memoryMbEstimate: 82.5 + (runs % 15) * 0.2,
  );
}