| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/histogram_samples.h" |
| 7 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
| 8 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
| 9 #include "chrome/test/nacl/nacl_browsertest_util.h" | 11 #include "chrome/test/nacl/nacl_browsertest_util.h" |
| 10 #include "content/public/browser/histogram_fetcher.h" | 12 #include "content/public/browser/histogram_fetcher.h" |
| 11 #include "native_client/src/trusted/service_runtime/nacl_error_code.h" | 13 #include "native_client/src/trusted/service_runtime/nacl_error_code.h" |
| 12 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h" | 14 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h" |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 class HistogramHelper { | 18 class HistogramHelper { |
| 17 public: | 19 public: |
| 18 HistogramHelper(); | 20 HistogramHelper(); |
| 19 | 21 |
| 20 // Each child process may have its own histogram data, make sure this data | 22 // Each child process may have its own histogram data, make sure this data |
| 21 // gets accumulated into the browser process before we examine the histograms. | 23 // gets accumulated into the browser process before we examine the histograms. |
| 22 void Fetch(); | 24 void Fetch(); |
| 23 | 25 |
| 24 // We know the exact number of samples in a bucket, and that no other bucket | 26 // We know the exact number of samples in a bucket, and that no other bucket |
| 25 // should have samples. | 27 // should have samples. |
| 26 void ExpectUniqueSample(const std::string& name, size_t bucket_id, | 28 void ExpectUniqueSample(const std::string& name, |
| 29 base::Histogram::Sample sample, |
| 27 base::Histogram::Count expected_count); | 30 base::Histogram::Count expected_count); |
| 28 | 31 |
| 29 // We don't know the values of the samples, but we know how many there are. | 32 // We don't know the values of the samples, but we know how many there are. |
| 30 void ExpectTotalCount(const std::string& name, base::Histogram::Count count); | 33 void ExpectTotalCount(const std::string& name, base::Histogram::Count count); |
| 31 | 34 |
| 32 private: | 35 private: |
| 33 void FetchCallback(); | 36 void FetchCallback(); |
| 34 | 37 |
| 35 void CheckBucketCount(const std::string& name, size_t bucket_id, | 38 void CheckBucketCount(const std::string& name, |
| 39 base::Histogram::Sample sample, |
| 36 base::Histogram::Count expected_count, | 40 base::Histogram::Count expected_count, |
| 37 base::Histogram::SampleSet& samples); | 41 const base::HistogramSamples& samples); |
| 38 | 42 |
| 39 void CheckTotalCount(const std::string& name, | 43 void CheckTotalCount(const std::string& name, |
| 40 base::Histogram::Count expected_count, | 44 base::Histogram::Count expected_count, |
| 41 base::Histogram::SampleSet& samples); | 45 const base::HistogramSamples& samples); |
| 42 }; | 46 }; |
| 43 | 47 |
| 44 HistogramHelper::HistogramHelper() { | 48 HistogramHelper::HistogramHelper() { |
| 45 } | 49 } |
| 46 | 50 |
| 47 void HistogramHelper::Fetch() { | 51 void HistogramHelper::Fetch() { |
| 48 base::Closure callback = base::Bind(&HistogramHelper::FetchCallback, | 52 base::Closure callback = base::Bind(&HistogramHelper::FetchCallback, |
| 49 base::Unretained(this)); | 53 base::Unretained(this)); |
| 50 | 54 |
| 51 content::FetchHistogramsAsynchronously( | 55 content::FetchHistogramsAsynchronously( |
| 52 MessageLoop::current(), | 56 MessageLoop::current(), |
| 53 callback, | 57 callback, |
| 54 // Give up after 60 seconds, which is longer than the 45 second timeout | 58 // Give up after 60 seconds, which is longer than the 45 second timeout |
| 55 // for browser tests. If this call times out, it means that a child | 59 // for browser tests. If this call times out, it means that a child |
| 56 // process is not responding which is something we should not ignore. | 60 // process is not responding which is something we should not ignore. |
| 57 base::TimeDelta::FromMilliseconds(60000)); | 61 base::TimeDelta::FromMilliseconds(60000)); |
| 58 content::RunMessageLoop(); | 62 content::RunMessageLoop(); |
| 59 } | 63 } |
| 60 | 64 |
| 61 void HistogramHelper::ExpectUniqueSample( | 65 void HistogramHelper::ExpectUniqueSample( |
| 62 const std::string& name, | 66 const std::string& name, |
| 63 size_t bucket_id, | 67 base::Histogram::Sample sample, |
| 64 base::Histogram::Count expected_count) { | 68 base::Histogram::Count expected_count) { |
| 65 base::Histogram* histogram = base::StatisticsRecorder::FindHistogram(name); | 69 base::Histogram* histogram = base::StatisticsRecorder::FindHistogram(name); |
| 66 ASSERT_NE(static_cast<base::Histogram*>(NULL), histogram) << | 70 ASSERT_NE(static_cast<base::Histogram*>(NULL), histogram) << |
| 67 "Histogram \"" << name << "\" does not exist."; | 71 "Histogram \"" << name << "\" does not exist."; |
| 68 | 72 |
| 69 base::Histogram::SampleSet samples; | 73 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
| 70 histogram->SnapshotSample(&samples); | 74 CheckBucketCount(name, sample, expected_count, *samples); |
| 71 CheckBucketCount(name, bucket_id, expected_count, samples); | 75 CheckTotalCount(name, expected_count, *samples); |
| 72 CheckTotalCount(name, expected_count, samples); | |
| 73 } | 76 } |
| 74 | 77 |
| 75 void HistogramHelper::ExpectTotalCount(const std::string& name, | 78 void HistogramHelper::ExpectTotalCount(const std::string& name, |
| 76 base::Histogram::Count count) { | 79 base::Histogram::Count count) { |
| 77 base::Histogram* histogram = base::StatisticsRecorder::FindHistogram(name); | 80 base::Histogram* histogram = base::StatisticsRecorder::FindHistogram(name); |
| 78 ASSERT_NE((base::Histogram*)NULL, histogram) << "Histogram \"" << name << | 81 ASSERT_NE((base::Histogram*)NULL, histogram) << "Histogram \"" << name << |
| 79 "\" does not exist."; | 82 "\" does not exist."; |
| 80 | 83 |
| 81 base::Histogram::SampleSet samples; | 84 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
| 82 histogram->SnapshotSample(&samples); | 85 CheckTotalCount(name, count, *samples); |
| 83 CheckTotalCount(name, count, samples); | |
| 84 } | 86 } |
| 85 | 87 |
| 86 void HistogramHelper::FetchCallback() { | 88 void HistogramHelper::FetchCallback() { |
| 87 MessageLoopForUI::current()->Quit(); | 89 MessageLoopForUI::current()->Quit(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void HistogramHelper::CheckBucketCount(const std::string& name, | 92 void HistogramHelper::CheckBucketCount(const std::string& name, |
| 91 size_t bucket_id, | 93 base::Histogram::Sample sample, |
| 92 base::Histogram::Count expected_count, | 94 base::Histogram::Count expected_count, |
| 93 base::Histogram::SampleSet& samples) { | 95 const base::HistogramSamples& samples) { |
| 94 EXPECT_EQ(expected_count, samples.counts(bucket_id)) << "Histogram \"" << | 96 EXPECT_EQ(expected_count, samples.GetCount(sample)) |
| 95 name << "\" does not have the right number of samples (" << | 97 << "Histogram \"" << name |
| 96 expected_count << ") in the expected bucket (" << bucket_id << ")."; | 98 << "\" does not have the right number of samples (" << expected_count |
| 99 << ") in the expected bucket (" << sample << ")."; |
| 97 } | 100 } |
| 98 | 101 |
| 99 void HistogramHelper::CheckTotalCount(const std::string& name, | 102 void HistogramHelper::CheckTotalCount(const std::string& name, |
| 100 base::Histogram::Count expected_count, | 103 base::Histogram::Count expected_count, |
| 101 base::Histogram::SampleSet& samples) { | 104 const base::HistogramSamples& samples) { |
| 102 EXPECT_EQ(expected_count, samples.TotalCount()) << "Histogram \"" << name << | 105 EXPECT_EQ(expected_count, samples.TotalCount()) |
| 103 "\" does not have the right total number of samples (" << | 106 << "Histogram \"" << name |
| 104 expected_count << ")."; | 107 << "\" does not have the right total number of samples (" |
| 108 << expected_count << ")."; |
| 105 } | 109 } |
| 106 | 110 |
| 107 NACL_BROWSER_TEST_F(NaClBrowserTest, SuccessfulLoadUMA, { | 111 NACL_BROWSER_TEST_F(NaClBrowserTest, SuccessfulLoadUMA, { |
| 108 // Load a NaCl module to generate UMA data. | 112 // Load a NaCl module to generate UMA data. |
| 109 RunLoadTest(FILE_PATH_LITERAL("nacl_load_test.html")); | 113 RunLoadTest(FILE_PATH_LITERAL("nacl_load_test.html")); |
| 110 | 114 |
| 111 // Make sure histograms from child processes have been accumulated in the | 115 // Make sure histograms from child processes have been accumulated in the |
| 112 // browser brocess. | 116 // browser brocess. |
| 113 HistogramHelper histograms; | 117 HistogramHelper histograms; |
| 114 histograms.Fetch(); | 118 histograms.Fetch(); |
| 115 | 119 |
| 116 // Did the plugin report success? | 120 // Did the plugin report success? |
| 117 histograms.ExpectUniqueSample("NaCl.LoadStatus.Plugin", | 121 histograms.ExpectUniqueSample( |
| 118 plugin::ERROR_LOAD_SUCCESS, 1); | 122 "NaCl.LoadStatus.Plugin", plugin::ERROR_LOAD_SUCCESS, 1); |
| 119 | 123 |
| 120 // Did the sel_ldr report success? | 124 // Did the sel_ldr report success? |
| 121 histograms.ExpectUniqueSample("NaCl.LoadStatus.SelLdr", | 125 histograms.ExpectUniqueSample( |
| 122 LOAD_OK, 1); | 126 "NaCl.LoadStatus.SelLdr", LOAD_OK, 1); |
| 123 | 127 |
| 124 // Make sure we have other important histograms. | 128 // Make sure we have other important histograms. |
| 125 histograms.ExpectTotalCount("NaCl.Perf.StartupTime.LoadModule", 1); | 129 histograms.ExpectTotalCount("NaCl.Perf.StartupTime.LoadModule", 1); |
| 126 histograms.ExpectTotalCount("NaCl.Perf.StartupTime.Total", 1); | 130 histograms.ExpectTotalCount("NaCl.Perf.StartupTime.Total", 1); |
| 127 histograms.ExpectTotalCount("NaCl.Perf.Size.Manifest", 1); | 131 histograms.ExpectTotalCount("NaCl.Perf.Size.Manifest", 1); |
| 128 histograms.ExpectTotalCount("NaCl.Perf.Size.Nexe", 1); | 132 histograms.ExpectTotalCount("NaCl.Perf.Size.Nexe", 1); |
| 129 }) | 133 }) |
| 130 | 134 |
| 131 // TODO(ncbray) convert the rest of nacl_uma.py (currently in the NaCl repo.) | 135 // TODO(ncbray) convert the rest of nacl_uma.py (currently in the NaCl repo.) |
| 132 // Test validation failures and crashes. | 136 // Test validation failures and crashes. |
| 133 | 137 |
| 134 } // namespace anonymous | 138 } // namespace anonymous |
| OLD | NEW |