-
Notifications
You must be signed in to change notification settings - Fork 2
/
PerformanceAspect.cs
51 lines (46 loc) · 1.97 KB
/
PerformanceAspect.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
using CarRental.Core.Utilities.Interceptors;
using CarRental.Core.Utilities.IoC;
using Castle.DynamicProxy;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
namespace CarRental.Core.Aspects.Autofac.Performance
{
/// <summary>
/// Returns information about the performance of the related method.
///If there is a performance weakness in the system, especially in intensive query operations, the system should warn us, for that pupose the PerfomanceAspect can be implemented.
/// </summary>
public class PerformanceAspect : MethodInterception
{
private int _interval;
private Stopwatch _stopwatch;
/// <summary>
/// Calculates the time interval for the operation from start to end. If it exceeds the given time interval it writes the message on the console.
/// </summary>
/// <param name="interval">Time period must be given in type of seconds.</param>
public PerformanceAspect(int interval)
{
_interval = interval;
_stopwatch = ServiceTool.ServiceProvider.GetService<Stopwatch>();
}
/// <summary>
/// Stopwatch starts before the related invocation(operation).
/// </summary>
/// <param name="invocation">Related method.</param>
protected override void OnBefore(IInvocation invocation)
{
_stopwatch.Start();
}
/// <summary>
/// If the total minutes of the created stop watch exceeded the given interval, it writes a message on the console window.
/// </summary>
/// <param name="invocation"></param>
protected override void OnAfter(IInvocation invocation)
{
if (_stopwatch.Elapsed.TotalSeconds > _interval)
{
Debug.WriteLine($"Performance : {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}-->{_stopwatch.Elapsed.TotalSeconds}");
}
_stopwatch.Reset();
}
}
}