Skip to content

Commit 3b521e9

Browse files
authored
DeviceModel set lazily (~ -20ms Sentry init); tests (#256)
1 parent a2dbcd3 commit 3b521e9

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

src/Sentry.Unity/MainThreadData.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading;
23

34
namespace Sentry.Unity
45
{
@@ -20,7 +21,7 @@ internal sealed class MainThreadData
2021

2122
public string? DeviceUniqueIdentifier { get; set; }
2223

23-
public string? DeviceModel { get; set; }
24+
public Lazy<string>? DeviceModel { get; set; }
2425

2526
public int? SystemMemorySize { get; set; }
2627

src/Sentry.Unity/SystemInfoAdapter.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading;
23
using UnityEngine;
34

45
namespace Sentry.Unity
@@ -13,7 +14,7 @@ internal interface ISentrySystemInfo
1314
string? CpuDescription { get; }
1415
string? DeviceName { get; }
1516
string? DeviceUniqueIdentifier { get; }
16-
string? DeviceModel { get; }
17+
Lazy<string>? DeviceModel { get; }
1718
int? SystemMemorySize { get; }
1819
int? GraphicsDeviceId { get; }
1920
string? GraphicsDeviceName { get; }
@@ -48,7 +49,7 @@ private SentrySystemInfoAdapter()
4849
public string? CpuDescription => SystemInfo.processorType;
4950
public string? DeviceName => SystemInfo.deviceName;
5051
public string? DeviceUniqueIdentifier => SystemInfo.deviceUniqueIdentifier;
51-
public string? DeviceModel => SystemInfo.deviceModel;
52+
public Lazy<string> DeviceModel => new(() => SystemInfo.deviceModel);
5253
/// <summary>
5354
/// System memory size in megabytes.
5455
/// </summary>

src/Sentry.Unity/UnityEventProcessor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Sentry.Extensibility;
34
using Sentry.Protocol;
45
using Sentry.Reflection;
@@ -94,7 +95,7 @@ private void PopulateDevice(Device device)
9495
device.Simulator = _application.IsEditor ? true : null;
9596
device.DeviceUniqueIdentifier = _sentryOptions.SendDefaultPii ? _mainThreadData.DeviceUniqueIdentifier : null;
9697

97-
var model = _mainThreadData.DeviceModel;
98+
var model = _mainThreadData.DeviceModel?.Value;
9899
if (model != SystemInfo.unsupportedIdentifier
99100
// Returned by the editor
100101
&& model != "System Product Name (System manufacturer)")

test/Sentry.Unity.Tests/UnityEventProcessorTests.cs

+30-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,35 @@ public IEnumerator Process_OperatingSystemProtocol_Assigned()
183183
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.OperatingSystem, sentryEvent.Contexts.OperatingSystem.Name);
184184
}
185185

186+
[UnityTest]
187+
public IEnumerator Process_DeviceProtocol_Assigned()
188+
{
189+
const long toByte = 1048576L; // in `UnityEventProcessor.PopulateDevice`
190+
_sentryMonoBehaviour.SentrySystemInfo = new TestSentrySystemInfo
191+
{
192+
ProcessorCount = 1,
193+
DeviceType = "Console",
194+
CpuDescription = "Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz",
195+
SupportsVibration = true,
196+
DeviceName = "hostname",
197+
DeviceModel = new Lazy<string>(() => "Samsung Galaxy S3"),
198+
SystemMemorySize = 16000
199+
};
200+
var sut = new UnityEventProcessor(_sentryOptions, _sentryMonoBehaviourGenerator, _testApplication);
201+
var sentryEvent = new SentryEvent();
202+
203+
yield return _sentryMonoBehaviour.CollectData();
204+
sut.Process(sentryEvent);
205+
206+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.ProcessorCount, sentryEvent.Contexts.Device.ProcessorCount);
207+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.DeviceType, sentryEvent.Contexts.Device.DeviceType);
208+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.CpuDescription, sentryEvent.Contexts.Device.CpuDescription);
209+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.SupportsVibration, sentryEvent.Contexts.Device.SupportsVibration);
210+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.DeviceName, sentryEvent.Contexts.Device.Name);
211+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.DeviceModel?.Value, sentryEvent.Contexts.Device.Model);
212+
Assert.AreEqual(_sentryMonoBehaviour.SentrySystemInfo.SystemMemorySize * toByte, sentryEvent.Contexts.Device.MemorySize);
213+
}
214+
186215
[UnityTest]
187216
public IEnumerator Process_GpuProtocol_Assigned()
188217
{
@@ -292,7 +321,7 @@ internal sealed class TestSentrySystemInfo : ISentrySystemInfo
292321
public string? CpuDescription { get; set; }
293322
public string? DeviceName { get; set; }
294323
public string? DeviceUniqueIdentifier { get; set; }
295-
public string? DeviceModel { get; set; }
324+
public Lazy<string>? DeviceModel { get; set; }
296325
public int? SystemMemorySize { get; set; }
297326
public int? GraphicsDeviceId { get; set; }
298327
public string? GraphicsDeviceName { get; set; }

0 commit comments

Comments
 (0)