AllocatingGCMemoryConstraint.cs
2.56 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using UnityEngine.Profiling;
namespace UnityEngine.TestTools.Constraints
{
public class AllocatingGCMemoryConstraint : Constraint
{
private class AllocatingGCMemoryResult : ConstraintResult
{
private readonly int diff;
public AllocatingGCMemoryResult(IConstraint constraint, object actualValue, int diff) : base(constraint, actualValue, diff > 0)
{
this.diff = diff;
}
public override void WriteMessageTo(MessageWriter writer)
{
if (diff == 0)
writer.WriteMessageLine("The provided delegate did not make any GC allocations.");
else
writer.WriteMessageLine("The provided delegate made {0} GC allocation(s).", diff);
}
}
private ConstraintResult ApplyTo(Action action, object original)
{
var recorder = Recorder.Get("GC.Alloc");
// The recorder was created enabled, which means it captured the creation of the Recorder object itself, etc.
// Disabling it flushes its data, so that we can retrieve the sample block count and have it correctly account
// for these initial allocations.
recorder.enabled = false;
#if !UNITY_WEBGL
recorder.FilterToCurrentThread();
#endif
recorder.enabled = true;
try
{
action();
}
finally
{
recorder.enabled = false;
#if !UNITY_WEBGL
recorder.CollectFromAllThreads();
#endif
}
return new AllocatingGCMemoryResult(this, original, recorder.sampleBlockCount);
}
public override ConstraintResult ApplyTo(object obj)
{
if (obj == null)
throw new ArgumentNullException();
TestDelegate d = obj as TestDelegate;
if (d == null)
throw new ArgumentException(string.Format("The actual value must be a TestDelegate but was {0}",
obj.GetType()));
return ApplyTo(() => d.Invoke(), obj);
}
public override ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> del)
{
if (del == null)
throw new ArgumentNullException();
return ApplyTo(() => del.Invoke(), del);
}
public override string Description
{
get { return "allocates GC memory"; }
}
}
}