xref: /aosp_15_r20/external/cronet/base/strings/string_util_perftest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
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/strings/string_util.h"
6 
7 #include <cinttypes>
8 
9 #include "base/time/time.h"
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 
15 template <typename String>
MeasureIsStringASCII(size_t str_length,size_t non_ascii_pos)16 void MeasureIsStringASCII(size_t str_length, size_t non_ascii_pos) {
17   String str(str_length, 'A');
18   if (non_ascii_pos < str_length)
19     str[non_ascii_pos] = '\xAF';
20 
21   TimeTicks t0 = TimeTicks::Now();
22   for (size_t i = 0; i < 10000000; ++i)
23     IsStringASCII(str);
24   TimeDelta time = TimeTicks::Now() - t0;
25   printf(
26       "char-size:\t%zu\tlength:\t%zu\tnon-ascii-pos:\t%zu\ttime-ms:\t%" PRIu64
27       "\n",
28       sizeof(typename String::value_type), str_length, non_ascii_pos,
29       time.InMilliseconds());
30 }
31 
TEST(StringUtilTest,DISABLED_IsStringASCIIPerf)32 TEST(StringUtilTest, DISABLED_IsStringASCIIPerf) {
33   for (size_t str_length = 4; str_length <= 1024; str_length *= 2) {
34     for (size_t non_ascii_loc = 0; non_ascii_loc < 3; ++non_ascii_loc) {
35       size_t non_ascii_pos = str_length * non_ascii_loc / 2 + 2;
36       MeasureIsStringASCII<std::string>(str_length, non_ascii_pos);
37       MeasureIsStringASCII<std::u16string>(str_length, non_ascii_pos);
38 #if defined(WCHAR_T_IS_32_BIT)
39       MeasureIsStringASCII<std::basic_string<wchar_t>>(str_length,
40                                                        non_ascii_pos);
41 #endif
42     }
43   }
44 }
45 
46 }  // namespace base
47