forked from 47-studio-org/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetricData.cs
78 lines (62 loc) · 2.8 KB
/
MetricData.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
using System;
using System.Diagnostics;
namespace PostSharp.Samples.Profiling
{
internal struct MetricData
{
public long CpuTime;
public long ThreadTime;
public long AsyncTime;
public long ExcludedThreadTime;
public long ExcludedCpuTime;
public int ExecutionCount;
public int ExceptionCount;
public long Timestamp;
public MetricData(long cpuTime, long threadTime, long asyncTime, long excludedCpuTime, long excludedThreadTime, int executionCount, int exceptionCount, long timestamp)
{
this.CpuTime = cpuTime;
this.ThreadTime = threadTime;
this.AsyncTime = asyncTime;
this.ExcludedThreadTime = excludedThreadTime;
this.ExcludedCpuTime = excludedCpuTime;
this.ExecutionCount = executionCount;
this.ExceptionCount = exceptionCount;
this.Timestamp = timestamp;
}
public TimeSpan AsyncTimeSpan => TimeSpan.FromSeconds((double) this.AsyncTime / Stopwatch.Frequency);
public TimeSpan ThreadTimeSpan => TimeSpan.FromSeconds((double) this.ThreadTime / Stopwatch.Frequency);
public TimeSpan CpuTimeSpan => TimeSpan.FromSeconds(this.CpuTime / Win32.GetThreadTimesFrequency);
public TimeSpan ExclusiveThreadTimeSpan => TimeSpan.FromSeconds((double) (this.ThreadTime - this.ExcludedThreadTime) / Stopwatch.Frequency);
public TimeSpan ExclusiveCpuTimeSpan => TimeSpan.FromSeconds((this.CpuTime - this.ExcludedCpuTime) / Win32.GetThreadTimesFrequency);
public TimeSpan SampleTimeSpan => TimeSpan.FromSeconds((double) this.Timestamp / Stopwatch.Frequency);
public void AddData(in MetricData data)
{
this.CpuTime += data.CpuTime;
this.ThreadTime += data.ThreadTime;
this.AsyncTime += data.AsyncTime;
this.ExcludedCpuTime += data.ExcludedCpuTime;
this.ExcludedThreadTime += data.ExcludedThreadTime;
this.ExecutionCount += data.ExecutionCount;
this.ExceptionCount += data.ExceptionCount;
}
public void AddExclusion(in ExcludedTimeData data)
{
this.ExcludedCpuTime += data.CpuTime;
this.ExcludedThreadTime += data.ThreadTime;
}
public void GetDifference(in MetricData reference, out MetricData difference)
{
difference = new MetricData(
this.CpuTime - reference.CpuTime,
this.ThreadTime - reference.ThreadTime,
this.AsyncTime - reference.AsyncTime,
this.ExcludedCpuTime - reference.ExcludedCpuTime,
this.ExcludedThreadTime - reference.ExcludedThreadTime,
this.ExecutionCount - reference.ExecutionCount,
this.ExceptionCount - reference.ExceptionCount,
this.Timestamp - reference.Timestamp);
}
internal void SetInvalid() => this.CpuTime = -1;
public bool IsInvalid => this.CpuTime < 0;
}
}