run method

  1. @override
Future<BenchmarkResult> run()
override

Ejecuta el benchmark y retorna métricas

Implementation

@override
Future<BenchmarkResult> run() async {
  final nativeMeasurements = <Duration>[];
  final interceptorMeasurements = <Duration>[];
  final uri = Uri.parse(endpoint);

  // Test con cliente nativo
  for (int i = 0; i < iterations; i++) {
    final stopwatch = Stopwatch()..start();

    try {
      final client = http.Client();
      final response = await client.get(uri);
      stopwatch.stop();

      if (response.statusCode == 200) {
        nativeMeasurements.add(stopwatch.elapsed);
      }

      client.close();
      await Future.delayed(const Duration(milliseconds: 10));

    } catch (e) {
      stopwatch.stop();
      nativeMeasurements.add(stopwatch.elapsed);
    }
  }

  // Test con interceptor Obsly
  for (int i = 0; i < iterations; i++) {
    final stopwatch = Stopwatch()..start();

    try {
      // ✅ USAR HTTP NATIVO - El interceptor global los captura automáticamente
      final client = http.Client();
      final response = await client.get(uri);
      stopwatch.stop();

      if (response.statusCode == 200) {
        interceptorMeasurements.add(stopwatch.elapsed);
      }

      client.close();
      await Future.delayed(const Duration(milliseconds: 10));

    } catch (e) {
      stopwatch.stop();
      interceptorMeasurements.add(stopwatch.elapsed);
    }
  }

  // Calcular overhead
  final nativeAvg = nativeMeasurements.isEmpty ? Duration.zero : Duration(
    microseconds: nativeMeasurements.map((d) => d.inMicroseconds).reduce((a, b) => a + b) ~/ nativeMeasurements.length
  );

  final interceptorAvg = interceptorMeasurements.isEmpty ? Duration.zero : Duration(
    microseconds: interceptorMeasurements.map((d) => d.inMicroseconds).reduce((a, b) => a + b) ~/ interceptorMeasurements.length
  );

  final overhead = interceptorAvg - nativeAvg;
  final overheadPercent = nativeAvg.inMicroseconds > 0
    ? (overhead.inMicroseconds / nativeAvg.inMicroseconds * 100).round()
    : 0;

  return BenchmarkResult(
    testName: name,
    iterations: iterations,
    measurements: interceptorMeasurements,
    metadata: {
      'endpoint': endpoint,
      'nativeAvgMs': nativeAvg.inMilliseconds,
      'interceptorAvgMs': interceptorAvg.inMilliseconds,
      'overheadMs': overhead.inMilliseconds,
      'overheadMicros': overhead.inMicroseconds,
      'overheadPercent': overheadPercent,
      'nativeSuccessful': nativeMeasurements.length,
      'interceptorSuccessful': interceptorMeasurements.length,
    },
  );
}