measure<T> method

Future<T> measure<T>(
  1. String label,
  2. Future<T> task()
)

Implementation

Future<T> measure<T>(
  String label,
  Future<T> Function() task,
) async {
  if (!kDebugMode) {
    return task();
  }

  final stopwatch = Stopwatch()..start();

  try {
    log('$label started.');

    final result = await task();

    stopwatch.stop();

    log('$label completed in ${stopwatch.elapsedMilliseconds}ms.');

    return result;
  } catch (e) {
    stopwatch.stop();

    log('$label failed in ${stopwatch.elapsedMilliseconds}ms.');

    rethrow;
  }
}