如何在Parallel中多次运行性能跟踪?

时间:2019-10-13 09:12:55

标签: android firebase flutter firebase-performance

我有一个名为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,因为加载跟踪还包含图像转换,而不仅仅是下载。

3 个答案:

答案 0 :(得分:2)

您可以通过自己存储开始时间然后仅记录持续时间来手动记录它。这应该起作用。

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

答案 1 :(得分:0)

如错误消息所述,在任何时候都只能有一条具有唯一名称的跟踪。因此,您要么必须等待第一个my_trace完成,然后再开始第二个<%= link_to "Buy", new_buyer_path(plan: 'item_D78387628dd', cost:'$45.00'), class: "btn btn-pink", role: "button" %> (顺序而不是并行运行它们),否则您必须为每个跟踪生成一个唯一的名称。

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

答案 2 :(得分:-1)

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

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

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