-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDelegateTests.cs
153 lines (122 loc) · 4.53 KB
/
DelegateTests.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.Profiling;
namespace MH.GCTests
{
public class DelegateTests
{
const int ELEM_COUNT = 10;
const int LOOP_COUNT = 100000; //don't know why, but if the mem allocation is too small, the profiler.GetMonoUsedSizeLong cannot detect it
[OneTimeSetUp]
public void Init()
{
GC.Collect();
Profiler.enabled = true;
}
[OneTimeTearDown]
public void Fini()
{
Profiler.enabled = false;
}
[Test]
public void OK_LocalLambda()
{
var cont = new List<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(new DummyObjA(i));
GC.Collect();
long startMem = Profiler.GetMonoUsedSizeLong();
//------------------//
for(int i=0; i<LOOP_COUNT; ++i)
for(int j=0; j<ELEM_COUNT; ++j)
cont.ForEach(x => x.v += 1);
long mem1 = Profiler.GetMonoUsedSizeLong();
Assert.That(mem1, Is.EqualTo(startMem));
//------------------//
Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
}
[Test]
public void BAD_MemberMethod() //Member method is indeed same as closure
{
var cont = new List<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(new DummyObjA(i));
GC.Collect();
long startMem = Profiler.GetMonoUsedSizeLong();
//------------------//
for(int i=0; i<LOOP_COUNT; ++i)
for(int j=0; j<ELEM_COUNT; ++j)
cont.ForEach( x => _ProcessDummyObj(x) );
long mem1 = Profiler.GetMonoUsedSizeLong();
Assert.That(mem1, Is.GreaterThan(startMem));
//------------------//
Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
}
private void _ProcessDummyObj(DummyObjA o)
{
o.v ++;
}
[Test]
public void OK_StaticMethod()
{
var cont = new List<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(new DummyObjA(i));
GC.Collect();
long startMem = Profiler.GetMonoUsedSizeLong();
//------------------//
for(int i=0; i<LOOP_COUNT; ++i)
for(int j=0; j<ELEM_COUNT; ++j)
cont.ForEach( x => _StaticProcessDummyObj(x) );
long mem1 = Profiler.GetMonoUsedSizeLong();
Assert.That(mem1, Is.EqualTo(startMem));
//------------------//
Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
}
private static void _StaticProcessDummyObj(DummyObjA x)
{
x.v ++;
}
[Test]
public void BAD_ClosureLambda()
{
var cont = new List<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(new DummyObjA(i));
GC.Collect();
long startMem = Profiler.GetMonoUsedSizeLong();
//------------------//
for(int i=0; i<LOOP_COUNT; ++i)
for(int j=0; j<ELEM_COUNT; ++j)
cont.ForEach(x => x.v += j);
long mem1 = Profiler.GetMonoUsedSizeLong();
Assert.That(mem1, Is.GreaterThan(startMem));
//------------------//
Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
}
private int _cacheDelegate_var = 0;
[Test]
public void OK_CachedDelegate()
{
var cont = new List<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(new DummyObjA(i));
GC.Collect();
long startMem = Profiler.GetMonoUsedSizeLong();
Action<DummyObjA> del = (o) => o.v += _cacheDelegate_var;
//------------------//
for(int i=0; i<LOOP_COUNT; ++i)
for(int j=0; j<ELEM_COUNT; ++j){
_cacheDelegate_var = j;
cont.ForEach(del);
}
long mem1 = Profiler.GetMonoUsedSizeLong();
Assert.That(mem1, Is.EqualTo(startMem));
//------------------//
Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
}
}
}