diff options
author | jbates@chromium.org <jbates@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-01 00:39:23 +0000 |
---|---|---|
committer | jbates@chromium.org <jbates@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-01 00:39:23 +0000 |
commit | d482596c39895074f33e2154b60794e9dc8dd066 (patch) | |
tree | f239fbc2a7d710e7214a6805dcf3894f112c1663 /base | |
parent | bd5d6cfd0e09e2123b948083bdaa39b2637b5143 (diff) | |
download | chromium_src-d482596c39895074f33e2154b60794e9dc8dd066.zip chromium_src-d482596c39895074f33e2154b60794e9dc8dd066.tar.gz chromium_src-d482596c39895074f33e2154b60794e9dc8dd066.tar.bz2 |
Add TraceAnalyzer support for calculating common event rate statistics
Review URL: http://codereview.chromium.org/8678035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112354 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/test/trace_event_analyzer.cc | 35 | ||||
-rw-r--r-- | base/test/trace_event_analyzer.h | 11 | ||||
-rw-r--r-- | base/test/trace_event_analyzer_unittest.cc | 43 |
3 files changed, 89 insertions, 0 deletions
diff --git a/base/test/trace_event_analyzer.cc b/base/test/trace_event_analyzer.cc index 0a2a15a..0832b86 100644 --- a/base/test/trace_event_analyzer.cc +++ b/base/test/trace_event_analyzer.cc @@ -713,6 +713,41 @@ const std::string& TraceAnalyzer::GetThreadName( return thread_names_[thread]; } +bool TraceAnalyzer::GetRateStats(const TraceEventVector& events, Stats* stats) { + // Need at least 3 events to calculate rate stats. + if (events.size() < 3) { + LOG(ERROR) << "Not enough events: " << events.size(); + return false; + } + + std::vector<double> deltas; + double delta_sum = 0.0; + size_t num_deltas = events.size() - 1; + for (size_t i = 0; i < num_deltas; ++i) { + double delta = events[i + 1]->timestamp - events[i]->timestamp; + if (delta < 0.0) { + LOG(ERROR) << "Events are out of order"; + return false; + } + deltas.push_back(delta); + delta_sum += delta; + } + + stats->min_us = *std::min_element(deltas.begin(), deltas.end()); + stats->max_us = *std::max_element(deltas.begin(), deltas.end()); + stats->mean_us = delta_sum / static_cast<double>(num_deltas); + + double sum_mean_offsets_squared = 0.0; + for (size_t i = 0; i < num_deltas; ++i) { + double offset = fabs(deltas[i] - stats->mean_us); + sum_mean_offsets_squared += offset * offset; + } + stats->standard_deviation_us = + sum_mean_offsets_squared / static_cast<double>(num_deltas - 1); + + return true; +} + void TraceAnalyzer::ParseMetadata() { for (size_t i = 0; i < raw_events_.size(); ++i) { TraceEvent& this_event = raw_events_[i]; diff --git a/base/test/trace_event_analyzer.h b/base/test/trace_event_analyzer.h index 577b185..e20be61 100644 --- a/base/test/trace_event_analyzer.h +++ b/base/test/trace_event_analyzer.h @@ -411,6 +411,13 @@ class TraceAnalyzer { public: typedef std::vector<const TraceEvent*> TraceEventVector; + struct Stats { + double min_us; + double max_us; + double mean_us; + double standard_deviation_us; + }; + ~TraceAnalyzer(); // Use trace events from JSON string generated by tracing API. @@ -461,6 +468,10 @@ class TraceAnalyzer { const std::string& GetThreadName(const TraceEvent::ProcessThreadID& thread); + // Calculate min/max/mean and standard deviation from the times between + // adjacent events. + static bool GetRateStats(const TraceEventVector& events, Stats* stats); + private: TraceAnalyzer(); diff --git a/base/test/trace_event_analyzer_unittest.cc b/base/test/trace_event_analyzer_unittest.cc index a95413f..0ab8960 100644 --- a/base/test/trace_event_analyzer_unittest.cc +++ b/base/test/trace_event_analyzer_unittest.cc @@ -527,6 +527,49 @@ TEST_F(TraceEventAnalyzerTest, Literals) { EXPECT_TRUE((Query::Bool(false) == Query::Double(0.0f)).Evaluate(dummy)); } +// Test GetRateStats. +TEST_F(TraceEventAnalyzerTest, RateStats) { + using namespace trace_analyzer; + + std::vector<TraceEvent> events; + events.reserve(100); + TraceAnalyzer::TraceEventVector event_ptrs; + TraceEvent event; + event.timestamp = 0.0; + double little_delta = 1.0; + double big_delta = 10.0; + TraceAnalyzer::Stats stats; + + for (int i = 0; i < 10; ++i) { + event.timestamp += little_delta; + events.push_back(event); + event_ptrs.push_back(&events.back()); + } + + ASSERT_TRUE(TraceAnalyzer::GetRateStats(event_ptrs, &stats)); + EXPECT_EQ(little_delta, stats.mean_us); + EXPECT_EQ(little_delta, stats.min_us); + EXPECT_EQ(little_delta, stats.max_us); + EXPECT_EQ(0.0, stats.standard_deviation_us); + + event.timestamp += big_delta; + events.push_back(event); + event_ptrs.push_back(&events.back()); + + ASSERT_TRUE(TraceAnalyzer::GetRateStats(event_ptrs, &stats)); + EXPECT_LT(little_delta, stats.mean_us); + EXPECT_EQ(little_delta, stats.min_us); + EXPECT_EQ(big_delta, stats.max_us); + EXPECT_LT(0.0, stats.standard_deviation_us); + + TraceAnalyzer::TraceEventVector few_event_ptrs; + few_event_ptrs.push_back(&event); + few_event_ptrs.push_back(&event); + ASSERT_FALSE(TraceAnalyzer::GetRateStats(few_event_ptrs, &stats)); + few_event_ptrs.push_back(&event); + ASSERT_TRUE(TraceAnalyzer::GetRateStats(few_event_ptrs, &stats)); +} + } // namespace trace_analyzer |