File: shape_result_perf_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (206 lines) | stat: -rw-r--r-- 6,945 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/timer/lap_timer.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_result_reporter.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/fonts/font_description.h"
#include "third_party/blink/renderer/platform/testing/font_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

using blink::test::CreateTestFont;

namespace blink {

static const int kTimeLimitMillis = 3000;
static const int kWarmupRuns = 10000;
static const int kTimeCheckInterval = 1000000;

namespace {

constexpr char kMetricPrefixOffsetForPosition[] = "OffsetForPosition.";
constexpr char kMetricPrefixCharacterRange[] = "CharacterRange.";
constexpr char kMetricThroughput[] = "throughput";

}  // namespace

class ShapeResultPerfTest {
  USING_FAST_MALLOC(ShapeResultPerfTest);

 public:
  enum FontName {
    kAhem,
    kAmiri,
    kMegalopolis,
    kRoboto,
  };

  ShapeResultPerfTest()
      : timer(kWarmupRuns,
              base::Milliseconds(kTimeLimitMillis),
              kTimeCheckInterval) {}

 protected:
  Font* CreateFont(FontName font_name) {
    FontDescription::VariantLigatures ligatures(
        FontDescription::kEnabledLigaturesState);
    return CreateTestFont(
        AtomicString("TestFont"),
        test::PlatformTestDataPath(font_path.find(font_name)->value), 100,
        &ligatures);
  }

  TextRun CreateRun(const String& text, bool ltr) {
    return TextRun(text, ltr ? TextDirection::kLtr : TextDirection::kRtl,
                   false);
  }

  void ReportResult(const std::string& metric_prefix,
                    const std::string& story_prefix) {
    std::string story = story_prefix + "_" + param_string;
    perf_test::PerfResultReporter reporter(metric_prefix, story);
    reporter.RegisterImportantMetric(kMetricThroughput, "runs/s");
    reporter.AddResult(kMetricThroughput, timer.LapsPerSecond());
  }

  HashMap<FontName, String> font_path = {
      {kAhem, "Ahem.woff"},
      {kAmiri, "third_party/Amiri/amiri_arabic.woff2"},
      {kMegalopolis, "third_party/MEgalopolis/MEgalopolisExtra.woff"},
      {kRoboto, "third_party/Roboto/roboto-regular.woff2"},
  };

  base::LapTimer timer;
  std::string param_string;
};

class OffsetForPositionPerfTest : public ShapeResultPerfTest,
                                  public testing::TestWithParam<float> {
 public:
  void OffsetForPosition(const Font& font,
                         TextRun& run,
                         IncludePartialGlyphsOption partial,
                         BreakGlyphsOption breakopt) {
    timer.Reset();
    float position = GetParam();
    param_string = base::NumberToString(position);
    do {
      font.DeprecatedOffsetForPosition(run, position, partial, breakopt);
      timer.NextLap();
    } while (!timer.HasTimeLimitExpired());
  }

 protected:
  void ReportResult(const std::string& story_prefix) {
    ShapeResultPerfTest::ReportResult(kMetricPrefixOffsetForPosition,
                                      story_prefix);
  }
};

class CharacterRangePerfTest : public ShapeResultPerfTest,
                               public testing::TestWithParam<int> {
 public:
  void GetCharacter(const Font& font, TextRun& run) {
    timer.Reset();
    int endpos = GetParam();
    param_string = base::NumberToString(endpos);
    do {
      font.DeprecatedSelectionRectForText(run, gfx::PointF(), 100, 0, endpos);
      timer.NextLap();
    } while (!timer.HasTimeLimitExpired());
  }

 protected:
  void ReportResult(const std::string& story_prefix) {
    ShapeResultPerfTest::ReportResult(kMetricPrefixCharacterRange,
                                      story_prefix);
  }
};

TEST_P(OffsetForPositionPerfTest, LTROffsetForPositionFullBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("FURACOLO", true);
  OffsetForPosition(*font, run, kOnlyFullGlyphs, BreakGlyphsOption(true));
  ReportResult("LTR_full_break");
}

TEST_P(OffsetForPositionPerfTest, LTROffsetForPositionFullDontBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("FURACOLO", true);
  OffsetForPosition(*font, run, kOnlyFullGlyphs, BreakGlyphsOption(false));
  ReportResult("LTR_full");
}

TEST_P(OffsetForPositionPerfTest, LTROffsetForPositionIncludePartialBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("FURACOLO", true);
  OffsetForPosition(*font, run, kIncludePartialGlyphs, BreakGlyphsOption(true));
  ReportResult("LTR_partial_break");
}

TEST_P(OffsetForPositionPerfTest, LTROffsetForPositionIncludePartialDontBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("FURACOLO", true);
  OffsetForPosition(*font, run, kIncludePartialGlyphs,
                    BreakGlyphsOption(false));
  ReportResult("LTR_partial");
}

TEST_P(OffsetForPositionPerfTest, RTLOffsetForPositionFullBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("OLOCARUF", false);
  OffsetForPosition(*font, run, kOnlyFullGlyphs, BreakGlyphsOption(true));
  ReportResult("RTL_full_break");
}

TEST_P(OffsetForPositionPerfTest, RTLOffsetForPositionFullDontBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("OLOCARUF", false);
  OffsetForPosition(*font, run, kOnlyFullGlyphs, BreakGlyphsOption(false));
  ReportResult("RTL_full");
}

TEST_P(OffsetForPositionPerfTest, RTLOffsetForPositionIncludePartialBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("OLOCARUF", false);
  OffsetForPosition(*font, run, kIncludePartialGlyphs, BreakGlyphsOption(true));
  ReportResult("RTL_partial_break");
}

TEST_P(OffsetForPositionPerfTest, RTLOffsetForPositionIncludePartialDontBreak) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("OLOCARUF", false);
  OffsetForPosition(*font, run, kIncludePartialGlyphs,
                    BreakGlyphsOption(false));
  ReportResult("RTL_partial");
}

INSTANTIATE_TEST_SUITE_P(OffsetForPosition,
                         OffsetForPositionPerfTest,
                         testing::Values(0, 10, 60, 100, 200, 350));

TEST_P(CharacterRangePerfTest, LTRCharacterForPosition) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("FURACOLO", true);
  GetCharacter(*font, run);
  ReportResult("LTR");
}

TEST_P(CharacterRangePerfTest, RTLCharacterForPosition) {
  Font* font = CreateFont(kAhem);
  TextRun run = CreateRun("OLOCARUF", false);
  GetCharacter(*font, run);
  ReportResult("RTL");
}

INSTANTIATE_TEST_SUITE_P(CharacterRange,
                         CharacterRangePerfTest,
                         testing::Values(0, 1, 2, 4, 8));

}  // namespace blink