Debian Codegraph @

unittest/gunit/collation_loader-t.cc

raw symbols SCIP
1 /* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 /*
25 We do benchmark test of character set loader below.
26 1. Load a collation. This happens in query like 'SELECT a FROM tbl ORDER BY
27 a COLLATE coll_name'. 'coll_name' is the name of collation which is used
28 to do ordering, like 'latin1_swedish_ci'.
29 2. Load a character set. In MySQL, when you ask for a 'character set', you
30 actually get a collation. For example, when executing 'SELECT _utf16
31 0x0100'. MySQL uses the pointer to collation 'utf16_general_ci' to handle
32 this character.
33 */
34
35 #include <gtest/gtest.h>
36 #include <thread>
37 #include <vector>
38
39 #include "m_ctype.h"
40 #include "my_sys.h"
41 #include "unittest/gunit/benchmark.h"
42
43 namespace collation_loader_unittest {
44
45 namespace {
46
47 CHARSET_INFO *lookup_collation(const char *name) {
48 MY_CHARSET_LOADER loader;
49 return my_collation_get_by_name(&loader, name, MYF(0));
50 }
51
52 CHARSET_INFO *lookup_charset(const char *name, int flag) {
53 MY_CHARSET_LOADER loader;
54 return my_charset_get_by_name(&loader, name, flag, MYF(0));
55 }
56
57 } // namespace
58
59 static void BM_LookupAllCollations(size_t num_iterations) {
60 StopBenchmarkTiming();
61
62 // Look up one collation to initialize the all_charsets array.
63 lookup_collation("latin1_swedish_ci");
64 size_t num_charsets = array_elements(all_charsets);
65
66 StartBenchmarkTiming();
67 for (size_t i = 0; i < num_iterations; i++) {
68 const CHARSET_INFO *cs = all_charsets[i % num_charsets];
69 if (cs) {
70 EXPECT_NE(lookup_collation(cs->m_coll_name), nullptr);
71 }
72 }
73 }
74 BENCHMARK(BM_LookupAllCollations)
75
76 static std::vector<std::string> charsets = {
77 "armscii8", "ascii", "big5", "binary", "cp1250", "cp1251",
78 "cp1256", "cp1257", "cp850", "cp852", "cp866", "cp932",
79 "dec8", "eucjpms", "euckr", "gb18030", "gb2312", "gbk",
80 "geostd8", "greek", "hebrew", "hp8", "keybcs2", "koi8r",
81 "koi8u", "latin1", "latin2", "latin5", "latin7", "macce",
82 "macroman", "sjis", "swe7", "tis620", "ucs2", "ujis",
83 "utf16", "utf16le", "utf32", "utf8", "utf8mb4",
84 };
85
86 static void BM_LookupAllCharsets(size_t num_iterations) {
87 StopBenchmarkTiming();
88
89 // Look up one collation to initialize the all_charsets array.
90 lookup_collation("latin1_swedish_ci");
91
92 StartBenchmarkTiming();
93 for (size_t i = 0; i < num_iterations; i++) {
94 const std::string &charset = charsets[i % charsets.size()];
95 EXPECT_NE(lookup_charset(charset.c_str(), MY_CS_PRIMARY), nullptr);
96 EXPECT_NE(lookup_charset(charset.c_str(), MY_CS_BINSORT), nullptr);
97 }
98 }
99 BENCHMARK(BM_LookupAllCharsets)
100
101 void TestRandomCollation() {
102 char coll_name[65];
103 for (int ix = 0; ix < 100000; ix++) {
104 memset(coll_name, 0, 65);
105 // Generate a random collation name whose length is in range [1, 64].
106 size_t random_coll_name_len = 1 + rand() % 64;
107 for (size_t i = 0; i < random_coll_name_len; i++) {
108 // Use ASCII latin letters to generate the name.
109 coll_name[i] = 'a' + rand() % 26;
110 }
111 int rc = get_collation_number(coll_name);
112 // Random collation that doesn't exist should return 0.
113 EXPECT_EQ(0, rc);
114 }
115 }
116
117 TEST(CollationLoaderTest, RandomCollation) {
118 // Look up one collation to initialize the all_charsets array.
119 lookup_collation("latin1_swedish_ci");
120 std::thread test_thd[4];
121 for (int i = 0; i < 4; i++) test_thd[i] = std::thread(TestRandomCollation);
122 for (int i = 0; i < 4; i++) test_thd[i].join();
123 }
124 } // namespace collation_loader_unittest