-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathTest01.cs
622 lines (554 loc) · 19.3 KB
/
Test01.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
using ILRuntime.Runtime;
using ILRuntimeTest;
using ILRuntimeTest.TestFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestCases
{
class Test01
{
public static Dictionary<EnumTest.TestEnum, EnumTest.TestEnum[]> dicAttrConver5 = new Dictionary<EnumTest.TestEnum, EnumTest.TestEnum[]>()
{
{
EnumTest.TestEnum.Enum1,new EnumTest.TestEnum[]{ EnumTest.TestEnum.Enum2}
},
};
public static int foo(int init)
{
int b = init;
for (int i = 0; i < 10000; i++)
{
b += i;
}
return b;
}
public static int foo()
{
int b = 0;
for (int i = 0; i < 50; i++)
{
b += foo(b);
}
return b;
}
public static void UnitTest_ValueType()
{
string a = 11.ToString();
}
[ILRuntimeJIT(ILRuntimeJITFlags.JITOnDemand)]
static int Add(int a, int b)
{
if (b < 0)
return a;
return a + b;
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance2()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
int cnt = 0;
for (int i = 0; i < 1000000; i++)
{
cnt += Add(i, 2);
}
sw.Stop();
Console.WriteLine(string.Format("time: {0:0}ms cps:{1:0}, result={2}", sw.ElapsedMilliseconds, (1000000 * 1000 / sw.ElapsedMilliseconds), cnt));
sw.Restart();
cnt = 0;
for (int i = 0; i < 1000000; i++)
{
cnt += ILRuntimeTest.TestFramework.TestClass2.Add(i, 2);
}
sw.Stop();
Console.WriteLine(string.Format("time: {0:0}ms cps:{1:0}, result={2}", sw.ElapsedMilliseconds, (1000000 * 1000 / sw.ElapsedMilliseconds), cnt));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static int UnitTest_PerformanceSimple()
{
int cnt = 0;
for (int i = 0; i < 5000000; i++)
{
cnt += i;
}
return cnt;
}
/// <summary>
/// 性能测试
/// </summary>
/// <returns></returns>
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance()
{
Console.WriteLine("UnitTest_Performance");
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
int cnt = 0;
for (int i = 0; i < 500000; i++)
{
cnt += i;
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, cnt));
sw.Reset();
sw.Start();
cnt = foo();
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, cnt));
sw.Reset();
sw.Start();
cnt = 0;
for (int i = 0; i < 1000000; i++)
{
FuncCallResult(ref cnt, i);
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, cnt));
PerfTest test = new PerfTest();
sw.Reset();
sw.Start();
cnt = 0;
for (int i = 0; i < 1000000; i++)
{
test.FuncCallResult(ref cnt, i);
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, cnt));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance3()
{
Console.WriteLine("UnitTest_Performance3");
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
int[] arr = new int[500000];
for (int i = 0; i < 500000; i++)
{
arr[i] = i;
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms, result = {1}", sw.ElapsedMilliseconds, arr.Length));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance5()
{
PerformanceTestCls obj = new PerformanceTestCls();
int cnt = 0;
int a = 0;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
a = obj.A;
}
sw.Stop();
Console.WriteLine(string.Format("res=" + a + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance9()
{
int[] array = new int[1024];
for (int i = 0; i < 1024; ++i)
{
array[i] = i;
}
int total = 0;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int j = 0; j < 1000; ++j)
{
for (int i = 0; i < 1024; ++i)
{
total = total + array[i];
}
}
sw.Stop();
Console.WriteLine(string.Format("res=" + total + ", time:{0:0}", sw.ElapsedMilliseconds));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance6()
{
PerformanceTestCls obj = new PerformanceTestCls();
int cnt = 0;
int a = 0;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
obj.A = a;
}
sw.Stop();
Console.WriteLine(string.Format("res=" + a + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance7()
{
PerformanceTestCls obj = new PerformanceTestCls();
int cnt = 0;
int a = 0;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
a = obj.B;
}
sw.Stop();
Console.WriteLine(string.Format("res=" + a + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance8()
{
PerformanceTestCls obj = new PerformanceTestCls();
int cnt = 0;
int a = 0;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
obj.B = a;
}
sw.Stop();
Console.WriteLine(string.Format("res=" + a + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance10()
{
TestVectorClass a = new TestVectorClass();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
TestVectorClass.ValueTypePerfTest(i, "b", a.Vector2, a);
}
sw.Stop();
Console.WriteLine(string.Format("time=" + sw.ElapsedMilliseconds + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance10_2()
{
TestVectorClass a = new TestVectorClass();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
a.Vector2 = a.Vector2;
}
sw.Stop();
Console.WriteLine(string.Format("time=" + sw.ElapsedMilliseconds + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance11()
{
TestVectorClass a = new TestVectorClass();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
TestVectorClass.ValueTypePerfTest2(i, "b", a.Obj, a);
}
sw.Stop();
Console.WriteLine(string.Format("time=" + sw.ElapsedMilliseconds + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
const int T12_NOBJS = 100;
const int T12_NLOOP = 10000000;
int A;
string B;
float C;
// 构造10W次脚本对象
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance12()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Test01[] arr = new Test01[T12_NOBJS];
for (int i = 0; i < T12_NLOOP; i++)
{
arr[i % T12_NOBJS] = new Test01();
}
sw.Stop();
Console.WriteLine("time:" + sw.ElapsedMilliseconds);
}
// 循环修改脚本短字段10W次
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance12_1()
{
Test01[] arr = new Test01[T12_NOBJS];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new Test01();
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < T12_NLOOP; i++)
{
var obj = arr[i % T12_NOBJS];
obj.A = i;
}
sw.Stop();
Console.WriteLine("time:" + sw.ElapsedMilliseconds);
}
// 循环修改脚本对象多个字段10W次
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance12_4()
{
Test01[] arr = new Test01[T12_NOBJS];
for (int i = 0; i < arr.Length; i++)
{
var obj = new Test01();
arr[i] = obj;
obj.A = i;
obj.B = (i + 1).ToString();
obj.C = 2 * i;
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < T12_NLOOP; ++i)
{
var obj = arr[i % T12_NOBJS];
var obj2 = arr[(i + 1) % T12_NOBJS];
obj.A = obj2.A;
obj.B = obj2.B;
obj.C = obj2.C;
}
sw.Stop();
Console.WriteLine("time:" + sw.ElapsedMilliseconds);
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance13()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
TestVector3 vec = default;
TestVector3 vec2 = TestVector3.One2;
for (int i = 0; i < 1000000; i++)
{
vec += vec2 * 2 + TestVector3.One2;
}
sw.Stop();
Console.WriteLine(string.Format("res="+ vec + ",time = " + sw.ElapsedMilliseconds + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance14()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
TestVector3NoBinding vec = default;
TestVector3NoBinding vec2 = TestVector3NoBinding.one;
for (int i = 0; i < 1000000; i++)
{
vec += vec2 * 2 + TestVector3NoBinding.one;
}
sw.Stop();
Console.WriteLine(string.Format("res=" + vec + ",time = " + sw.ElapsedMilliseconds + ", cps:{0:0}", (1000000 * 1000 / sw.ElapsedMilliseconds)));
}
class PerformanceTestCls
{
public int A = 1;
public int B { get; set; } = 2;
}
[ILRuntimeTest(IsPerformanceTest = true)]
public static void UnitTest_Performance4()
{
Func<int, float, short, double> func = (a, b, c) =>
{
return a + b + c;
};
ILRuntimeTest.TestFramework.DelegateTest.DelegatePerformanceTest = (a, b, c) =>
{
return a + b + c;
};
var func2 = ILRuntimeTest.TestFramework.DelegateTest.DelegatePerformanceTest;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 100000; i++)
{
func(1, 3.6f, 4);
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time:{0:0}ms", sw.ElapsedMilliseconds));
sw.Restart();
for (int i = 0; i < 100000; i++)
{
func2(1, 3.6f, 4);
}
sw.Stop();
Console.WriteLine(string.Format("Elapsed time2:{0:0}ms", sw.ElapsedMilliseconds));
}
public static void UnitTest_Cls()
{
object obj = new object();
Console.WriteLine("UnitTest_Cls");
Test1098Cls cls = new Test1098Cls();
Test1098Sub(cls);
Console.WriteLine(string.Format("A={0} B={1}", cls.A, cls.B));
}
public static void UnitTest_Generics()
{
Console.WriteLine("UnitTest_Generixs");
//如果一个类继承一个泛型参数为这个类本身的泛型类,就没法正确找到该类型了
SingletonTest.Inst.Test = "bar";
Console.WriteLine(SingletonTest.Inst.foo());
SingletonTest2.Inst.Test = 2;
Console.WriteLine(SingletonTest2.Inst.foo());
Console.WriteLine(SingletonTest2.Inst.GetString<SingletonTest>(SingletonTest.Inst));
Console.WriteLine(SingletonTest2.IsSingletonInstance(SingletonTest2.Inst).ToString());
}
public static void UnitTest_Generics2()
{
Console.WriteLine("UnitTest_Generics2");
SingletonTest.Inst.Test = "bar";
Console.WriteLine(SingletonTest.Inst.foo());
SingletonTest2.Inst.Test = 2;
Console.WriteLine(SingletonTest2.Inst.foo());
Console.WriteLine(SingletonTest2.Inst.GetString<SingletonTest>(SingletonTest.Inst));
Console.WriteLine(SingletonTest2.IsSingletonInstance(new SingletonTest2()).ToString());
Console.WriteLine(SingletonTest2.IsSingletonInstance(SingletonTest2.Inst).ToString());
}
public static void UnitTest_Generics3()
{
Console.WriteLine("UnitTest_Generics3");
Console.WriteLine(new List<NestedTest>().ToString());
}
public static void UnitTest_Generics4()
{
object r = TestGeneric<object>();
Console.WriteLine("Result = " + r);
}
static T TestGeneric<T>() where T : new()
{
T obj = new T();
return obj;
}
public static void UnitTest_NestedGenerics()
{
Console.WriteLine("UnitTest_NestedGenerics");
//如果一个嵌套的类是泛型类参数,则这个类无法被找到
Console.WriteLine(new NestedTestBase<NestedTest>().ToString());
}
class Test1098Cls
{
public int A { get; set; }
public string B { get; set; }
}
static void Test1098Sub(Test1098Cls cls)
{
cls.A = 2;
cls.B = "ok";
}
class PerfTest
{
public void FuncCallResult(ref int cnt, int i)
{
cnt++;
}
}
static void FuncCallResult(ref int cnt, int i)
{
cnt++;
}
class NestedTest
{
}
class NestedTestBase<T>
{
}
}
class SingletonTest : Singleton<SingletonTest>
{
public string Test { get; set; }
public float testFloat;
public static int TestStaticField;
public string foo()
{
return Inst.Test;
}
}
class SingletonTest2 : Singleton<SingletonTest2>
{
public int Test { get; set; }
public string foo()
{
return Inst.Test.ToString();
}
}
class Singleton<T> where T : class, new()
{
private static T _inst;
public int testField;
public Singleton()
{
}
public static T Inst
{
[ILRuntimeTest(Ignored = true)]
get
{
if (_inst == null)
{
_inst = new T();
}
return _inst;
}
}
public string GetString<K>(K obj)
{
return obj.ToString();
}
public static bool IsSingletonInstance(T inst)
{
return _inst == inst;
}
}
public class DictionaryEnumeratorTest<TKey, TValue>
{
Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
public void Add(TKey key, TValue value)
{
dic.Add(key, value);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return dic.GetEnumerator(); //报错
}
public void GetEnumeratorTest()
{
var e = dic.GetEnumerator(); //正常
while (e.MoveNext())
{
Console.WriteLine(e.Current.Key + " " + e.Current.Value);
}
}
public void GetEnumeratorTest2()
{
IEnumerator<KeyValuePair<TKey, TValue>> e = dic.GetEnumerator(); //报错
while (e.MoveNext())
{
Console.WriteLine(e.Current.Key + " " + e.Current.Value);
}
}
}
public class MyTest
{
public static Dictionary<string, MyTest[]> data = new Dictionary<string, MyTest[]>();
public static void UnitTest_Test1()
{
var arr = new MyTest[] { new MyTest(), null, null };
data["test"] = arr;
Console.WriteLine(data["test"][0]);
}
public static void Test()
{
DictionaryEnumeratorTest<int, int> t = new DictionaryEnumeratorTest<int, int>();
t.Add(1, 1);
t.GetEnumeratorTest();
t.GetEnumeratorTest2();
var e = t.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine(e.Current.Key + " " + e.Current.Value);
}
}
}
}