0
|
1 using System;
|
|
2 using System.Reflection;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Aspects;
|
|
7 using BLToolkit.Reflection;
|
|
8
|
|
9 namespace HowTo.Aspects
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class CounterAspectTest
|
|
13 {
|
|
14 public /*[a]*/abstract/*[/a]*/ class TestClass
|
|
15 {
|
|
16 // This is a method we collect statistic for.
|
|
17 // Actually the entire class or even a base class
|
|
18 // can be decorated with the attribute.
|
|
19 //
|
|
20 [/*[a]*/Counter/*[/a]*/]
|
|
21 public /*[a]*/virtual/*[/a]*/ void TestMethod()
|
|
22 {
|
|
23 }
|
|
24 }
|
|
25
|
|
26 [Test]
|
|
27 public void Test()
|
|
28 {
|
|
29 TestClass t = TypeAccessor<TestClass>.CreateInstance();
|
|
30
|
|
31 for (int i = 0; i < 10; i++)
|
|
32 t.TestMethod();
|
|
33
|
|
34 MethodInfo methodInfo = typeof(TestClass).GetMethod("TestMethod");
|
|
35 MethodCallCounter counter = CounterAspect.GetCounter(methodInfo);
|
|
36
|
|
37 Assert.AreEqual(10, counter.TotalCount);
|
|
38
|
|
39 Console.WriteLine(@"
|
|
40 Method : {0}.{1}
|
|
41 TotalCount : {2}
|
|
42 ExceptionCount : {3}
|
|
43 CachedCount : {4}
|
|
44 CurrentCalls : {5}
|
|
45 TotalTime : {6}
|
|
46 MinTime : {7}
|
|
47 MaxTime : {8}
|
|
48 AverageTime : {9}
|
|
49 ",
|
|
50 counter.MethodInfo.DeclaringType.Name,
|
|
51 counter.MethodInfo.Name,
|
|
52 counter.TotalCount, // total actual calls (no cached calls)
|
|
53 counter.ExceptionCount, // calls with exceptions
|
|
54 counter.CachedCount, // cached calls
|
|
55 counter.CurrentCalls.Count, // current calls (make sense for multithreading)
|
|
56 counter.TotalTime, // total work time
|
|
57 counter.MinTime, // min call time
|
|
58 counter.MaxTime, // max call time
|
|
59 counter.AverageTime); // average call time
|
|
60 }
|
|
61 }
|
|
62 }
|