Repository navigation
Expand file tree
/
Copy pathInputMetrics.cs
More file actions
117 lines (104 loc) · 2.59 KB
/
Copy pathInputMetrics.cs
File metadata and controls
117 lines (104 loc) · 2.59 KB
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
using System;
namespace KeyboardDiagnostic
{
public sealed class TypingMetrics
{
private readonly object _sync = new object();
private readonly IMonotonicClock _clock;
private long? _startedAt;
public TypingMetrics(IMonotonicClock clock = null)
{
_clock = clock ?? StopwatchClock.System;
}
public void ObserveCharacterCount(int characterCount)
{
lock (_sync)
{
if (characterCount <= 0)
{
_startedAt = null;
}
else if (!_startedAt.HasValue)
{
_startedAt = _clock.GetTimestamp();
}
}
}
public int CalculateWordsPerMinute(int characterCount)
{
long? startedAt;
lock (_sync)
{
startedAt = _startedAt;
}
if (!startedAt.HasValue || characterCount <= 0)
{
return 0;
}
double elapsedMilliseconds = Math.Max(0, _clock.GetElapsedMilliseconds(startedAt.Value));
return KeyboardInput.CalculateWordsPerMinute(
characterCount,
TimeSpan.FromMilliseconds(elapsedMilliseconds));
}
public void Reset()
{
lock (_sync)
{
_startedAt = null;
}
}
}
public sealed class KeyRateCounter
{
private readonly object _sync = new object();
private int _pending;
private int _lastSample;
private int _peak;
public int LastSample
{
get
{
lock (_sync)
{
return _lastSample;
}
}
}
public int Peak
{
get
{
lock (_sync)
{
return _peak;
}
}
}
public void RecordPress()
{
lock (_sync)
{
_pending++;
}
}
public int Sample()
{
lock (_sync)
{
_lastSample = _pending;
_pending = 0;
_peak = Math.Max(_peak, _lastSample);
return _lastSample;
}
}
public void Reset()
{
lock (_sync)
{
_pending = 0;
_lastSample = 0;
_peak = 0;
}
}
}
}