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