runBenchmarks method

Future<void> runBenchmarks()

Run benchmark tests

Implementation

Future<void> runBenchmarks() async {
  final results = <String, dynamic>{};

  // Benchmark 1: Signal creation speed
  final createStopwatch = Stopwatch()..start();
  for (int i = 0; i < 1000; i++) {
    final s = Signal<int>(i);
    s.val; // Access to ensure it's created
  }
  createStopwatch.stop();
  results['signalCreation'] = {
    'count': 1000,
    'totalMs': createStopwatch.elapsedMilliseconds,
    'avgMs': createStopwatch.elapsedMilliseconds / 1000,
  };

  // Benchmark 2: Signal update speed
  final updateStopwatch = Stopwatch()..start();
  final testSignal = Signal<int>(0);
  for (int i = 0; i < 10000; i++) {
    testSignal.emit(i);
  }
  updateStopwatch.stop();
  results['signalUpdates'] = {
    'count': 10000,
    'totalMs': updateStopwatch.elapsedMilliseconds,
    'avgMs': updateStopwatch.elapsedMilliseconds / 10000,
  };

  // Benchmark 3: Computed signal performance
  final computedStopwatch = Stopwatch()..start();
  final source = Signal<int>(0);
  final computed = Computed(() => source.val * 2);
  for (int i = 0; i < 5000; i++) {
    source.emit(i);
    computed.value; // Force recomputation
  }
  computedStopwatch.stop();
  results['computedSignals'] = {
    'count': 5000,
    'totalMs': computedStopwatch.elapsedMilliseconds,
    'avgMs': computedStopwatch.elapsedMilliseconds / 5000,
  };

  // Benchmark 4: Effect execution speed (skip if no Effect available)
  try {
    final effectStopwatch = Stopwatch()..start();
    final effectSource = Signal<int>(0);
    // Effects are in neuron_effects.dart - just benchmark signal changes
    for (int i = 0; i < 5000; i++) {
      effectSource.emit(i);
    }
    effectStopwatch.stop();
    results['effects'] = {
      'count': 5000,
      'totalMs': effectStopwatch.elapsedMilliseconds,
      'avgMs': effectStopwatch.elapsedMilliseconds / 5000,
    };
  } catch (e) {
    // Skip effect benchmark if not available
  }

  // Benchmark 5: Memory stress test
  final memoryBefore = ProcessInfoProxy.currentRss;
  final signals = <Signal<int>>[];
  for (int i = 0; i < 10000; i++) {
    signals.add(Signal<int>(i));
  }
  await Future.delayed(const Duration(milliseconds: 100));
  final memoryAfter = ProcessInfoProxy.currentRss;
  results['memoryStress'] = {
    'signalsCreated': 10000,
    'memoryUsedMB': (memoryAfter - memoryBefore) / (1024 * 1024),
    'avgPerSignalBytes': (memoryAfter - memoryBefore) / 10000,
  };

  _benchmarkResults.addAll(results);
}