如何并行多次 运行 性能跟踪?

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

我有一个名为 my_trace 的 Firebase 性能监控跟踪。

现在,我在加载图像时开始跟踪:

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

当我尝试加载单个图像时,这工作正常,但是,在我的应用程序中,我需要并行加载许多图像。
这意味着当我加载我的图像时报告以下错误:

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

当我想记录每个加载过程的性能时,如何正确地并行多次启动跟踪?

注意:我不能使用HTTPMetric,因为加载轨迹还包含图像转换,而不仅仅是下载。

如错误消息所述,任何时候都只能有一个具有唯一名称的跟踪处于活动状态。因此,您要么必须等待第一个 my_trace 完成,然后再开始第二个(运行 按顺序而不是并行排列它们),或者您必须为每个跟踪生成一个唯一的名称.

考虑到 API 的结构,应该可以允许多个同名跟踪并行到 运行。如果您认为 Firebase 应该考虑允许这样做,我建议您 file a feature request.

跟踪已经允许 运行 并行。跟踪不按跟踪名称索引。只要跟踪对象是唯一的,您就应该能够 运行 并行跟踪。不能重复使用相同的跟踪对象。

例如:(使用跟踪对象的方式不正确)

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();

例如:并行多次使用相同跟踪名称的正确方法。

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();

您可以通过自己存储开始时间然后只记录持续时间来手动记录它。这应该有效。

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