comparison Source/Aspects/MethodCallCounter.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Reflection;
4
5 namespace BLToolkit.Aspects
6 {
7 [System.Diagnostics.DebuggerStepThrough]
8 public class MethodCallCounter
9 {
10 public MethodCallCounter(CallMethodInfo methodInfo)
11 {
12 _callMethodInfo = methodInfo;
13 _methodInfo = methodInfo.MethodInfo;
14 }
15
16 #region Public Members
17
18 private MethodInfo _methodInfo;
19 public MethodInfo MethodInfo
20 {
21 get { return _methodInfo; }
22 set { _methodInfo = value; }
23 }
24
25 private CallMethodInfo _callMethodInfo;
26 public CallMethodInfo CallMethodInfo
27 {
28 get { return _callMethodInfo; }
29 set { _callMethodInfo = value; }
30 }
31
32 private int _totalCount;
33 public int TotalCount
34 {
35 get { return _totalCount; }
36 set { _totalCount = value; }
37 }
38
39 private int _exceptionCount;
40 public int ExceptionCount
41 {
42 get { return _exceptionCount; }
43 set { _exceptionCount = value; }
44 }
45
46 private int _cachedCount;
47 public int CachedCount
48 {
49 get { return _cachedCount; }
50 set { _cachedCount = value; }
51 }
52
53 private TimeSpan _totalTime;
54 public TimeSpan TotalTime
55 {
56 get { return _totalTime; }
57 set { _totalTime = value; }
58 }
59
60 private TimeSpan _minTime = TimeSpan.MaxValue;
61 public TimeSpan MinTime
62 {
63 get { return _minTime; }
64 set { _minTime = value; }
65 }
66
67 private TimeSpan _maxTime;
68 public TimeSpan MaxTime
69 {
70 get { return _maxTime; }
71 set { _maxTime = value; }
72 }
73
74 private readonly ArrayList _currentCalls = ArrayList.Synchronized(new ArrayList());
75 public ArrayList CurrentCalls
76 {
77 get { return _currentCalls; }
78 }
79
80 public TimeSpan AverageTime
81 {
82 get
83 {
84 if (_totalCount == 0)
85 return TimeSpan.MinValue;
86
87 return new TimeSpan(TotalTime.Ticks / TotalCount);
88 }
89 }
90
91 #endregion
92
93 #region Protected Members
94
95 public virtual void RegisterCall(InterceptCallInfo info)
96 {
97 lock (_currentCalls.SyncRoot)
98 _currentCalls.Add(info);
99 }
100
101 public virtual void UnregisterCall(InterceptCallInfo info)
102 {
103 AddCall(DateTime.Now - info.BeginCallTime, info.Exception != null, info.Cached);
104
105 lock (_currentCalls.SyncRoot)
106 _currentCalls.Remove(info);
107 }
108
109 protected void AddCall(TimeSpan time, bool withException, bool cached)
110 {
111 if (cached)
112 {
113 _cachedCount++;
114 }
115 else
116 {
117 _totalTime += time;
118 _totalCount++;
119
120 if (_minTime > time) _minTime = time;
121 if (_maxTime < time) _maxTime = time;
122 }
123
124 if (withException) _exceptionCount++;
125 }
126
127 #endregion
128 }
129 }