summaryrefslogtreecommitdiff
path: root/chromium/ui/latency/histograms_perftest.cc
blob: ab7480fc3fc5932e979c5c793fd224c87031a79d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/latency/histograms.h"

#include <algorithm>

#include "base/metrics/bucket_ranges.h"
#include "base/metrics/sample_vector.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_test.h"
#include "ui/latency/fixed_point.h"
#include "ui/latency/frame_metrics_test_common.h"

namespace ui {
namespace frame_metrics {

constexpr base::TimeDelta kTimeLimit = base::TimeDelta::FromSeconds(2);

// A version of RatioHistogram based on the default implementations
// of base::BucketRanges and base::SampleVector.
class RatioHistogramBaseline : public Histogram {
 public:
  RatioHistogramBaseline()
      : ratio_boundaries_(),
        bucket_ranges_(ratio_boundaries_.size()),
        sample_vector_(&bucket_ranges_) {
    size_t i = 0;
    for (const auto& b : ratio_boundaries_.boundaries) {
      bucket_ranges_.set_range(i++, std::min<uint64_t>(b, INT_MAX));
    }
  }

  ~RatioHistogramBaseline() override = default;

  void AddSample(uint32_t microseconds, uint32_t weight) override {
    sample_vector_.Accumulate(microseconds, weight);
  }

  PercentileResults ComputePercentiles() const override {
    return PercentileResults();
  }
  void Reset() override {}

 private:
  TestRatioBoundaries ratio_boundaries_;
  base::BucketRanges bucket_ranges_;
  base::SampleVector sample_vector_;

  DISALLOW_COPY_AND_ASSIGN(RatioHistogramBaseline);
};

TEST(FrameMetricsHistogramsPerfTest, RatioEntireRange) {
  const int kStride = 0x1000;

  RatioHistogramBaseline vh_base;
  RatioHistogram vh_impl;

  base::TimeDelta impl_time;
  base::TimeDelta base_time;

  base::TimeTicks finish_time = base::TimeTicks::Now() + kTimeLimit;
  while (base::TimeTicks::Now() < finish_time) {
    // Impl then Base
    for (int i = 0; i < INT_MAX - kStride; i += kStride) {
      int value = (i * 37) & 0x3FFFFFFF;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      impl_time += t1 - t0 - (t3 - t2);
      base_time += t2 - t1 - (t3 - t2);
    }

    // Base then Impl
    for (int i = 0; i < INT_MAX - kStride; i += kStride) {
      int value = (i * 37) & 0x3FFFFFFF;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      base_time += t1 - t0 - (t3 - t2);
      impl_time += t2 - t1 - (t3 - t2);
    }
  }

  double X = base_time.InSecondsF() / impl_time.InSecondsF();
  perf_test::PrintResult(__FUNCTION__, "", __FUNCTION__, X, "x", true);
}

TEST(FrameMetricsHistogramsPerfTest, RatioCommonRange) {
  const int kStride = 0x100;

  RatioHistogramBaseline vh_base;
  RatioHistogram vh_impl;

  base::TimeDelta impl_time;
  base::TimeDelta base_time;

  base::TimeTicks finish_time = base::TimeTicks::Now() + kTimeLimit;
  while (base::TimeTicks::Now() < finish_time) {
    // Impl then Base
    for (int i = 0; i < 4 * kFixedPointMultiplier; i += kStride) {
      int value = i;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      impl_time += t1 - t0 - (t3 - t2);
      base_time += t2 - t1 - (t3 - t2);
    }

    // Base then Impl
    for (int i = 0; i < 4 * kFixedPointMultiplier; i += kStride) {
      int value = i;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      base_time += t1 - t0 - (t3 - t2);
      impl_time += t2 - t1 - (t3 - t2);
    }
  }

  double X = base_time.InSecondsF() / impl_time.InSecondsF();
  perf_test::PrintResult(__FUNCTION__, "", __FUNCTION__, X, "x", true);
}

// A version of VSyncHistogram based on the default implementations
// of base::BucketRanges and base::SampleVector.
class VSyncHistogramBaseline : public Histogram {
 public:
  VSyncHistogramBaseline()
      : bucket_ranges_(kTestVSyncBoundries.size() + 1),
        sample_vector_(&bucket_ranges_) {
    size_t i = 0;
    for (const auto& b : kTestVSyncBoundries) {
      bucket_ranges_.set_range(i++, b);
    }
    // BucketRanges needs the last element set to INT_MAX.
    bucket_ranges_.set_range(i++, INT_MAX);
  }

  ~VSyncHistogramBaseline() override = default;

  void AddSample(uint32_t microseconds, uint32_t weight) override {
    sample_vector_.Accumulate(microseconds, weight);
  }

  PercentileResults ComputePercentiles() const override {
    return PercentileResults();
  }
  void Reset() override {}

 private:
  base::BucketRanges bucket_ranges_;
  base::SampleVector sample_vector_;

  DISALLOW_COPY_AND_ASSIGN(VSyncHistogramBaseline);
};

TEST(FrameMetricsHistogramsPerfTest, VSyncEntireRange) {
  const int kStride = 0x1000;

  VSyncHistogramBaseline vh_base;
  VSyncHistogram vh_impl;

  base::TimeDelta impl_time;
  base::TimeDelta base_time;

  base::TimeTicks finish_time = base::TimeTicks::Now() + kTimeLimit;
  while (base::TimeTicks::Now() < finish_time) {
    // Impl then Base
    for (int i = 0; i < INT_MAX - kStride; i += kStride) {
      int value = (i * 37) % 64000000;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      impl_time += t1 - t0 - (t3 - t2);
      base_time += t2 - t1 - (t3 - t2);
    }

    // Base then Impl
    for (int i = 0; i < INT_MAX - kStride; i += kStride) {
      int value = (i * 37) % 64000000;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      base_time += t1 - t0 - (t3 - t2);
      impl_time += t2 - t1 - (t3 - t2);
    }
  }

  double X = base_time.InSecondsF() / impl_time.InSecondsF();
  perf_test::PrintResult(__FUNCTION__, "", __FUNCTION__, X, "x", true);
}

TEST(FrameMetricsHistogramsPerfTest, VSyncCommonRange) {
  const int kStride = 0x100;

  VSyncHistogramBaseline vh_base;
  VSyncHistogram vh_impl;

  base::TimeDelta impl_time;
  base::TimeDelta base_time;

  base::TimeTicks finish_time = base::TimeTicks::Now() + kTimeLimit;
  while (base::TimeTicks::Now() < finish_time) {
    // Impl then Base
    for (int i = 0; i < 100000; i += kStride) {
      int value = i;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      impl_time += t1 - t0 - (t3 - t2);
      base_time += t2 - t1 - (t3 - t2);
    }

    // Base then Impl
    for (int i = 0; i < 100000; i += kStride) {
      int value = i;
      base::TimeTicks t0 = base::TimeTicks::Now();
      vh_base.AddSample(value, 1);
      base::TimeTicks t1 = base::TimeTicks::Now();
      vh_impl.AddSample(value, 1);
      base::TimeTicks t2 = base::TimeTicks::Now();
      base::TimeTicks t3 = base::TimeTicks::Now();
      base_time += t1 - t0 - (t3 - t2);
      impl_time += t2 - t1 - (t3 - t2);
    }
  }

  double X = base_time.InSecondsF() / impl_time.InSecondsF();
  perf_test::PrintResult(__FUNCTION__, "", __FUNCTION__, X, "x", true);
}

}  // namespace frame_metrics
}  // namespace ui