debugInstrumentAction<T> function

Future<T> debugInstrumentAction <T>(String description, FutureOr<T> action())

Runs the specified action, timing how long the action takes in debug builds when debugInstrumentationEnabled is true.

The instrumentation will be printed to the logs using debugPrint. In non-debug builds, or when debugInstrumentationEnabled is false, this will run action without any instrumentation.

Returns the result of running action, wrapped in a Future if the action was synchronous.


Future<T> debugInstrumentAction<T>(String description, FutureOr<T> action()) {
  if (!debugInstrumentationEnabled)
    return new Future<T>.value(action());

  Stopwatch stopwatch;
  assert(() {
    stopwatch = new Stopwatch()..start();
    return true;
  } ());
  void stopStopwatchAndPrintElapsed() {
    assert(() {
      debugPrint('Action "$description" took ${stopwatch.elapsed}');
      return true;

  Future<T> returnResult;
  FutureOr<T> actionResult;
  try {
    actionResult = action();
  } finally {
    if (actionResult is Future<T>) {
      returnResult = actionResult.whenComplete(stopStopwatchAndPrintElapsed);
    } else {
      returnResult = new Future<T>.value(actionResult);
  return returnResult;