OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #import "ios/chrome/browser/web/chrome_web_client.h" |
| 8 #include "ios/chrome/test/base/perf_test_ios.h" |
| 9 |
| 10 PerfTest::PerfTest(std::string testGroup) |
| 11 : BlockCleanupTest(), |
| 12 testGroup_(testGroup), |
| 13 firstLabel_("1st"), |
| 14 averageLabel_("2nd+"), |
| 15 isWaterfall_(false), |
| 16 verbose_(true), |
| 17 repeatCount_(10), |
| 18 web_client_(base::MakeUnique<ChromeWebClient>()) {} |
| 19 PerfTest::PerfTest(std::string testGroup, |
| 20 std::string firstLabel, |
| 21 std::string averageLabel, |
| 22 bool isWaterfall, |
| 23 bool verbose, |
| 24 int repeat) |
| 25 : BlockCleanupTest(), |
| 26 testGroup_(testGroup), |
| 27 firstLabel_(firstLabel), |
| 28 averageLabel_(averageLabel), |
| 29 isWaterfall_(isWaterfall), |
| 30 verbose_(verbose), |
| 31 repeatCount_(repeat), |
| 32 web_client_(base::MakeUnique<ChromeWebClient>()) {} |
| 33 |
| 34 PerfTest::~PerfTest() {} |
| 35 |
| 36 void PerfTest::LogPerfTiming(std::string testName, base::TimeDelta elapsed) { |
| 37 LogPerfValue(testName, elapsed.InMillisecondsF(), "ms"); |
| 38 } |
| 39 |
| 40 void PerfTest::LogPerfValue(std::string testName, |
| 41 double value, |
| 42 std::string unit) { |
| 43 NSLog(@"%sRESULT %s: %s= %.3f %s\n", isWaterfall_ ? "*" : "", |
| 44 testGroup_.c_str(), testName.c_str(), value, unit.c_str()); |
| 45 } |
| 46 |
| 47 void PerfTest::RepeatTimedRuns(std::string testName, |
| 48 TimedActionBlock timedAction, |
| 49 ProceduralBlock postAction) { |
| 50 RepeatTimedRuns(testName, timedAction, postAction, repeatCount_); |
| 51 } |
| 52 |
| 53 void PerfTest::RepeatTimedRuns(std::string testName, |
| 54 TimedActionBlock timedAction, |
| 55 ProceduralBlock postAction, |
| 56 int repeat) { |
| 57 base::TimeDelta firstElapsed; |
| 58 base::TimeDelta totalElapsed; |
| 59 base::TimeDelta maxElapsed; |
| 60 base::TimeDelta minElapsed = base::TimeDelta::FromSeconds(1000); |
| 61 for (int i = 0; i < repeat + 1; ++i) { |
| 62 base::TimeDelta elapsed = timedAction(i); |
| 63 if (i == 0) { |
| 64 std::string label = |
| 65 firstLabel_.length() ? testName + " " + firstLabel_ : testName; |
| 66 LogPerfTiming(label, elapsed); |
| 67 firstElapsed = elapsed; |
| 68 } else { |
| 69 if (verbose_) |
| 70 NSLog(@"%2d: %.3f ms", i, elapsed.InMillisecondsF()); |
| 71 totalElapsed += elapsed; |
| 72 if (elapsed > maxElapsed) |
| 73 maxElapsed = elapsed; |
| 74 if (elapsed < minElapsed) |
| 75 minElapsed = elapsed; |
| 76 } |
| 77 if (postAction) |
| 78 postAction(); |
| 79 } |
| 80 if (repeat > 2) { |
| 81 base::TimeDelta average = |
| 82 (totalElapsed - maxElapsed - minElapsed) / (repeat - 2); |
| 83 std::string label = |
| 84 averageLabel_.length() ? testName + " " + averageLabel_ : testName; |
| 85 LogPerfTiming(label, average); |
| 86 } |
| 87 } |
| 88 |
| 89 // TODO(leng): Replace this with RepeatTimedRuns when we have figured out |
| 90 // the best way to combine various logging requirements in repeated runs. |
| 91 base::TimeDelta PerfTest::CalculateAverage(base::TimeDelta* times, |
| 92 int count, |
| 93 base::TimeDelta* min_time, |
| 94 base::TimeDelta* max_time) { |
| 95 DCHECK(times); |
| 96 base::TimeDelta min = times[0]; |
| 97 base::TimeDelta max = times[0]; |
| 98 base::TimeDelta sum = times[0]; |
| 99 |
| 100 for (int i = 1; i < count; i++) { |
| 101 sum += times[i]; |
| 102 if (times[i] > max) |
| 103 max = times[i]; |
| 104 if (times[i] < min) |
| 105 min = times[i]; |
| 106 } |
| 107 if (max_time) |
| 108 *max_time = max; |
| 109 if (min_time) |
| 110 *min_time = min; |
| 111 return sum / count; |
| 112 } |
OLD | NEW |