1 | | | import 'perf_counter_snapshot.dart'; |
2 | | |
|
3 | | | /// Simple performance counter to consolidate statistics about woker tast execution: total number of calls, total |
4 | | | /// number of errors, total elapsed time, and max elapsed time. |
5 | | | class PerfCounter implements PerfCounterSnapshot { |
6 | | | /// Creates a performance counter. |
7 | | 1 | PerfCounter(this.name) |
8 | | | : _maxTimeInMicroseconds = 0, |
9 | | | _totalTimeInMicroseconds = 0, |
10 | | | _totalCount = 0, |
11 | | | _totalErrors = 0; |
12 | | |
|
13 | | | /// The counter's name or label. |
14 | | | @override |
15 | | | final String name; |
16 | | |
|
17 | | | /// Maximum elapsed time for a single call, in microseconds. |
18 | | 1 | @override |
19 | | 1 | int get maxTimeInMicroseconds => _maxTimeInMicroseconds; |
20 | | | int _maxTimeInMicroseconds; |
21 | | |
|
22 | | | /// Total elapsed time, in microseconds. |
23 | | 1 | @override |
24 | | 1 | int get totalTimeInMicroseconds => _totalTimeInMicroseconds; |
25 | | | int _totalTimeInMicroseconds; |
26 | | |
|
27 | | | /// Total number of calls. |
28 | | 1 | @override |
29 | | 1 | int get totalCount => _totalCount; |
30 | | | int _totalCount; |
31 | | |
|
32 | | | /// Total number of errors. |
33 | | 1 | @override |
34 | | 1 | int get totalErrors => _totalErrors; |
35 | | | int _totalErrors; |
36 | | |
|
37 | | | /// Returns a snapshot of the [PerfCounter]'s values. |
38 | | 2 | PerfCounterSnapshot get snapshot => PerfCounterSnapshot(this); |
39 | | | } |
40 | | |
|
41 | | | // private implementation internal to Squadron |
42 | | | extension PerfCounterExt on PerfCounter { |
43 | | | /// Updates counter value with the duration indicated by [timeInMicroseconds]. |
44 | | | /// 1. update the maximum elapsed time if required. |
45 | | | /// 2. add specified time to the total elapsed time. |
46 | | | /// 3. depending on [success], increment the total number of calls or errors by 1. |
47 | | 1 | void update(int timeInMicroseconds, bool success) { |
48 | | 2 | if (timeInMicroseconds > _maxTimeInMicroseconds) { |
49 | | 1 | _maxTimeInMicroseconds = timeInMicroseconds; |
50 | | | } |
51 | | 2 | _totalTimeInMicroseconds += timeInMicroseconds; |
52 | | | if (success) { |
53 | | 2 | _totalCount++; |
54 | | | } else { |
55 | | 0 | _totalErrors++; |
56 | | | } |
57 | | | } |
58 | | | } |