-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInsightTimer.cs
More file actions
46 lines (43 loc) · 1.37 KB
/
InsightTimer.cs
File metadata and controls
46 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Diagnostics;
using UnityEngine;
namespace GoogleMobileAds.Common
{
/// <summary>
/// A helper class to time an operation and emit an insight with the duration.
/// </summary>
public class InsightTimer
{
private Insight _insight;
private Stopwatch _stopwatch;
/// <summary>
/// Creates an instance of InsightTimer and starts timing.
/// </summary>
/// <param name="insight">The insight to emit when End() is called.</param>
public InsightTimer(Insight insight)
{
_insight = insight;
if (_insight.Tracing == null)
{
_insight.Tracing = new Insight.TracingActivity();
}
_insight.Tracing.HasEnded = false;
_stopwatch = new Stopwatch();
_stopwatch.Start();
}
/// <summary>
/// Stops the timer, calculates the duration, and emits the insight.
/// </summary>
public void End()
{
if (_insight == null || _insight.Tracing == null || _insight.Tracing.HasEnded)
{
return;
}
_stopwatch.Stop();
_insight.Tracing.DurationMillis = _stopwatch.ElapsedMilliseconds;
_insight.Tracing.HasEnded = true;
InsightsEmitter.Instance.Emit(_insight);
}
}
}