| OLD | NEW |
| (Empty) |
| 1 // Copyright 2011 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #ifndef OMAHA_COMMON_PROGRESS_SAMPLER_H_ | |
| 17 #define OMAHA_COMMON_PROGRESS_SAMPLER_H_ | |
| 18 | |
| 19 #include <windows.h> | |
| 20 #include <algorithm> | |
| 21 #include <queue> | |
| 22 #include "omaha/base/debug.h" | |
| 23 #include "omaha/base/time.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 // This class keeps track of the input data (samples) and helps to calculate | |
| 28 // the average progress based on that. | |
| 29 // | |
| 30 // Example usage: | |
| 31 // // Create a sampler that keep samples within last 500ms and calculates | |
| 32 // // average progress only if minimum time range 100ms is reached. | |
| 33 // ProgressSampler<int> progress_sampler(500, 100); | |
| 34 // ASSERT1(!progress_sampler.HasEnoughSamples()); | |
| 35 // | |
| 36 // progress_sampler.AddSample(0, 100); | |
| 37 // ASSERT1(!progress_sampler.HasEnoughSamples()); | |
| 38 // | |
| 39 // progress_sampler.AddSample(20, 200); | |
| 40 // // Minimum time range 100ms has not been reached yet. | |
| 41 // ASSERT1(!progress_sampler.HasEnoughSamples()); | |
| 42 // | |
| 43 // progress_sampler.AddSample(200, 300); | |
| 44 // ASSERT1(progress_sampler.HasEnoughSamples()); | |
| 45 // // Samples in queue: [(0, 100), (20, 200), (200, 300)]. | |
| 46 // // Average speed: (300-100) / (200-0) = 1. | |
| 47 // ASSERT1(1 == progress_sampler.GetAverageProgressPerMs()); | |
| 48 // | |
| 49 // progress_sampler.AddSample(520, 450); | |
| 50 // // The first sample value was added at timeline 0 is now out of | |
| 51 // // range (500ms) and thus discarded. | |
| 52 // // Samples in queue now: [(20, 200), (200, 300), [520, 450)]. | |
| 53 // // Average speed: (520-20) / (450-200) = 2. | |
| 54 // ASSERT1(2 == progress_sampler.GetAverageProgressPerMs()); | |
| 55 template<typename T> class ProgressSampler { | |
| 56 public: | |
| 57 ProgressSampler(int sample_time_range_ms, int minimum_range_required_ms) | |
| 58 : sample_time_range_ms_(sample_time_range_ms), | |
| 59 minimum_range_required_ms_(minimum_range_required_ms) { | |
| 60 ASSERT1(minimum_range_required_ms > 0); | |
| 61 } | |
| 62 | |
| 63 void AddSampleWithCurrentTimeStamp(T sample_value) { | |
| 64 AddSample(GetCurrent100NSTime() / kMillisecsTo100ns, sample_value); | |
| 65 } | |
| 66 | |
| 67 void AddSample(uint64 timestamp_in_ms, T sample_value) { | |
| 68 if (!samples_.empty() && | |
| 69 (sample_value < samples_.back().value || // Value regression. | |
| 70 timestamp_in_ms < samples_.back().timestamp)) { // Clock regression. | |
| 71 Reset(); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 samples_.push(Sample(timestamp_in_ms, sample_value)); | |
| 76 | |
| 77 // Discard old data that is out of range. | |
| 78 while (samples_.back().timestamp - samples_.front().timestamp > | |
| 79 sample_time_range_ms_ && samples_.size() > 2) { | |
| 80 samples_.pop(); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 bool HasEnoughSamples() const { | |
| 85 if (samples_.size() < 2) { | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 ASSERT1(samples_.back().timestamp >= samples_.front().timestamp); | |
| 90 return (samples_.back().timestamp - samples_.front().timestamp > | |
| 91 minimum_range_required_ms_); | |
| 92 } | |
| 93 | |
| 94 T GetAverageProgressPerMs() const { | |
| 95 if (!HasEnoughSamples()) { | |
| 96 return kUnknownProgressPerMs; | |
| 97 } | |
| 98 | |
| 99 uint64 time_diff = samples_.back().timestamp - samples_.front().timestamp; | |
| 100 ASSERT1(time_diff > 0); | |
| 101 return (samples_.back().value - samples_.front().value) / | |
| 102 static_cast<T>(time_diff); | |
| 103 } | |
| 104 | |
| 105 void Reset() { | |
| 106 std::queue<Sample> empty_queue; | |
| 107 std::swap(samples_, empty_queue); | |
| 108 } | |
| 109 | |
| 110 static const T kUnknownProgressPerMs = static_cast<T>(-1); | |
| 111 | |
| 112 private: | |
| 113 const uint64 sample_time_range_ms_; | |
| 114 const uint64 minimum_range_required_ms_; | |
| 115 | |
| 116 struct Sample { | |
| 117 Sample(uint64 local_timestamp, T local_value) | |
| 118 : timestamp(local_timestamp), value(local_value) { | |
| 119 } | |
| 120 | |
| 121 uint64 timestamp; | |
| 122 T value; | |
| 123 }; | |
| 124 std::queue<Sample> samples_; | |
| 125 }; | |
| 126 | |
| 127 } // namespace omaha | |
| 128 | |
| 129 #endif // OMAHA_COMMON_PROGRESS_SAMPLER_H_ | |
| OLD | NEW |