creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126574

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.

Upvotes: 2

Views: 808

Answers (3)

hisoft
hisoft

Reputation: 881

You can record it manually by storing the start time yourself and then just recording the duration. This should work.

Reference: https://firebase.google.com/docs/reference/js/firebase.performance.Trace#record

Upvotes: 2

Visu
Visu

Reputation: 189

Traces are allowed to run in parallel already. Traces are not indexed by trace names. As long the trace object is unique, you should be able to run traces in parallel. Same trace object cannot be re-used.

Eg: (Incorrect way of using trace object)

final Trace trace = performance.newTrace("my_trace");
trace.start();
trace.start(); // This would not work. Fails with the error message that the trace is already started.
// ... (loading that happens asynchronously)
trace.stop();

Eg: Right way using the same trace name multiple times in parallel.

final Trace trace1 = performance.newTrace("my_trace");
final Trace trace2 = performance.newTrace("my_trace");
trace1.start();
trace2.start();
// ... (loading that happens asynchronously)
trace1.stop();
trace2.stop();

Upvotes: -1

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

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.

Upvotes: 0

Related Questions