Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions source/plugin/Assets/GoogleMobileAds/Common/InsightTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MobileAdsClient : AndroidJavaProxy, IMobileAdsClient
private readonly IInsightsEmitter _insightsEmitter = InsightsEmitter.Instance;
private readonly ITracer _tracer;
private Action<IInitializationStatusClient> _initCompleteAction;
private InsightTimer _initializationInsightTimer;

private MobileAdsClient() : base(Utils.OnInitializationCompleteListenerClassName) {
_mobileAdsClass = new AndroidJavaClass(Utils.UnityMobileAdsClassName);
Expand All @@ -51,6 +52,10 @@ public void Initialize(Action<IInitializationStatusClient> initCompleteAction)
using (_tracer.StartTrace("MobileAdsClient.Initialize"))
{
_initCompleteAction = initCompleteAction;
_initializationInsightTimer = new InsightTimer(new Insight()
{
Name = Insight.CuiName.SdkInitialized
});

Task.Run(() => {
using (_tracer.StartTrace("AttachCurrentThread"))
Expand All @@ -71,10 +76,6 @@ public void Initialize(Action<IInitializationStatusClient> initCompleteAction)
}
});
}
_insightsEmitter.Emit(new Insight()
{
Name = Insight.CuiName.SdkInitialized
});
}

public void SetApplicationVolume(float volume)
Expand Down Expand Up @@ -175,6 +176,10 @@ public Version GetSDKVersion()

public void onInitializationComplete(AndroidJavaObject initStatus)
{
if (_initializationInsightTimer != null)
{
_initializationInsightTimer.End();
}
if (_initCompleteAction != null) {
IInitializationStatusClient statusClient = new InitializationStatusClient(initStatus);
_initCompleteAction(statusClient);
Expand Down