androidfirebaseflutterfirebase-performance

How do I run a Performance Trace multiple times in Parallel?


I have a Firebase Performance Monitoring trace called my_trace.

Now, I start this trace when I load an image:

void loadImage() {
  final Trace trace = performance.newTrace("my_trace");
  trace.start();
  // ... (loading that happens asynchronously)
  trace.stop();
}

This works fine when I try to load a single image, however, in my application I need to load many images in parallel.
This means that the following error is reported when I load my images:

Trace 'my_trace' has already started, should not start again!

How do I correctly start a trace multiple times in parallel as I want to record the performance of every single loading process?

Note: I cannot use HTTPMetric as the loading trace also contains image conversion and not just downloading.


Solution

  • As the error message says, there can only be a single trace with a unique name active at any time. So you'll either have to wait for the first my_trace to finish before starting the second (running them sequentially instead of in parallel), or you'll have to generate a unique name for each trace.

    Given how the API is structured it should be possible to allow multiple traces of the same name to run in parallel. If you think Firebase should consider allowing that, I recommend you file a feature request.