Debian Codegraph @

unittest/gunit/value_map-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 #include <gtest/gtest.h>
25 #include <climits>
26 #include <cstring> // std::memcmp
27 #include <iterator> // std::next
28 #include <random>
29
30 #include "my_alloc.h" // MEM_ROOT
31 #include "mysql_time.h" // MYSQL_TIME
32 #include "sql/field.h" // my_charset_numeric
33 #include "sql/histograms/value_map.h" // Value_map
34 #include "sql/mem_root_allocator.h"
35 #include "sql/my_decimal.h" // my_decimal
36 #include "sql/sql_time.h" // my_time_compare
37 #include "sql_string.h" // String
38 #include "unittest/gunit/benchmark.h"
39
40 namespace value_map_unittest {
41
42 class ValueMapTest : public ::testing::Test {
43 public:
44 ValueMapTest() = default;
45 };
46
47 TEST_F(ValueMapTest, LongLongValueMap) {
48 histograms::Value_map<longlong> value_map(&my_charset_numeric,
49 histograms::Value_map_type::INT);
50
51 EXPECT_EQ(value_map.size(), 0U);
52
53 value_map.add_values(1LL, 1);
54 EXPECT_EQ(value_map.size(), 1U);
55
56 value_map.add_values(1LL, 1);
57 EXPECT_EQ(value_map.size(), 1U);
58
59 value_map.add_values(std::numeric_limits<longlong>::min(), 1);
60 EXPECT_EQ(value_map.size(), 2U);
61
62 value_map.add_values(0LL, 1);
63 EXPECT_EQ(value_map.size(), 3U);
64
65 value_map.add_values(0LL, 1000);
66 EXPECT_EQ(value_map.size(), 3U);
67
68 // Check that data is sorted
69 EXPECT_EQ(value_map.begin()->first, std::numeric_limits<longlong>::min());
70 EXPECT_EQ(std::next(value_map.begin(), 1)->first, 0LL);
71 EXPECT_EQ(std::next(value_map.begin(), 2)->first, 1LL);
72
73 // Check that the counts are correct
74 EXPECT_EQ(value_map.begin()->second, 1U);
75 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 1001U);
76 EXPECT_EQ(std::next(value_map.begin(), 2)->second, 2U);
77 }
78
79 TEST_F(ValueMapTest, ULongLongValueMap) {
80 histograms::Value_map<ulonglong> value_map(&my_charset_numeric,
81 histograms::Value_map_type::UINT);
82
83 EXPECT_EQ(value_map.size(), 0U);
84
85 value_map.add_values(std::numeric_limits<ulonglong>::max(), 1);
86 EXPECT_EQ(value_map.size(), 1U);
87
88 value_map.add_values(0ULL, 1);
89 EXPECT_EQ(value_map.size(), 2U);
90
91 // Check that data is sorted
92 EXPECT_EQ(value_map.begin()->first, 0ULL);
93 EXPECT_EQ(std::next(value_map.begin(), 1)->first,
94 std::numeric_limits<ulonglong>::max());
95
96 // Check that the counts are correct
97 EXPECT_EQ(value_map.begin()->second, 1U);
98 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 1U);
99 }
100
101 TEST_F(ValueMapTest, DoubleValueMap) {
102 histograms::Value_map<double> value_map(&my_charset_numeric,
103 histograms::Value_map_type::DOUBLE);
104
105 EXPECT_EQ(value_map.size(), 0U);
106
107 value_map.add_values(std::numeric_limits<double>::max(), 1);
108 EXPECT_EQ(value_map.size(), 1U);
109
110 value_map.add_values(std::numeric_limits<double>::lowest(), 42);
111 EXPECT_EQ(value_map.size(), 2U);
112
113 value_map.add_values(0.0, 1);
114 EXPECT_EQ(value_map.size(), 3U);
115
116 // Check that data is sorted
117 EXPECT_EQ(value_map.begin()->first, std::numeric_limits<double>::lowest());
118 EXPECT_EQ(std::next(value_map.begin(), 1)->first, 0.0);
119 EXPECT_EQ(std::next(value_map.begin(), 2)->first,
120 std::numeric_limits<double>::max());
121
122 // Check that the counts are correct
123 EXPECT_EQ(value_map.begin()->second, 42U);
124 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 1U);
125 EXPECT_EQ(std::next(value_map.begin(), 2)->second, 1U);
126 }
127
128 TEST_F(ValueMapTest, DecimalValueMap) {
129 histograms::Value_map<my_decimal> value_map(
130 &my_charset_numeric, histograms::Value_map_type::DECIMAL);
131
132 EXPECT_EQ(value_map.size(), 0U);
133
134 my_decimal val1;
135 double2my_decimal(0, -12.0, &val1);
136 value_map.add_values(val1, 9);
137 EXPECT_EQ(value_map.size(), 1U);
138
139 my_decimal val2;
140 double2my_decimal(0, -100.1, &val2);
141 value_map.add_values(val2, 8);
142 EXPECT_EQ(value_map.size(), 2U);
143
144 my_decimal val3;
145 double2my_decimal(0, 99.9, &val3);
146 value_map.add_values(val3, 7);
147 EXPECT_EQ(value_map.size(), 3U);
148
149 // Check that data is sorted
150 String res1;
151 my_decimal2string(E_DEC_FATAL_ERROR, &value_map.begin()->first, &res1);
152 EXPECT_EQ(strcmp(res1.ptr(), "-100.1"), 0);
153
154 String res2;
155 my_decimal2string(E_DEC_FATAL_ERROR, &std::next(value_map.begin(), 1)->first,
156 &res2);
157 EXPECT_EQ(strcmp(res2.ptr(), "-12"), 0);
158
159 String res3;
160 my_decimal2string(E_DEC_FATAL_ERROR, &std::next(value_map.begin(), 2)->first,
161 &res3);
162 EXPECT_EQ(strcmp(res3.ptr(), "99.9"), 0);
163
164 // Check that the counts are correct
165 EXPECT_EQ(value_map.begin()->second, 8U);
166 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 9U);
167 EXPECT_EQ(std::next(value_map.begin(), 2)->second, 7U);
168 }
169
170 TEST_F(ValueMapTest, MysqlTimeValueMap) {
171 histograms::Value_map<MYSQL_TIME> value_map(
172 &my_charset_numeric, histograms::Value_map_type::DATETIME);
173
174 EXPECT_EQ(value_map.size(), 0U);
175
176 MYSQL_TIME time1;
177 time1.year = 2017;
178 time1.month = 1;
179 time1.day = 1;
180 time1.hour = 10;
181 time1.minute = 0;
182 time1.second = 0;
183 time1.second_part = 0;
184 time1.neg = false;
185 time1.time_type = MYSQL_TIMESTAMP_DATETIME;
186 value_map.add_values(time1, 1);
187 EXPECT_EQ(value_map.size(), 1U);
188
189 MYSQL_TIME time2;
190 time2.year = 2017;
191 time2.month = 1;
192 time2.day = 1;
193 time2.hour = 10;
194 time2.minute = 0;
195 time2.second = 0;
196 time2.second_part = 1;
197 time2.neg = false;
198 time2.time_type = MYSQL_TIMESTAMP_DATETIME;
199 value_map.add_values(time2, 2);
200 EXPECT_EQ(value_map.size(), 2U);
201
202 MYSQL_TIME time3;
203 time3.year = 1000;
204 time3.month = 1;
205 time3.day = 1;
206 time3.hour = 10;
207 time3.minute = 10;
208 time3.second = 11;
209 time3.second_part = 12;
210 time3.neg = false;
211 time3.time_type = MYSQL_TIMESTAMP_DATETIME;
212 value_map.add_values(time3, 3);
213 EXPECT_EQ(value_map.size(), 3U);
214
215 // Same value as time2
216 MYSQL_TIME time4;
217 time4.year = 2017;
218 time4.month = 1;
219 time4.day = 1;
220 time4.hour = 10;
221 time4.minute = 0;
222 time4.second = 0;
223 time4.second_part = 1;
224 time4.neg = false;
225 time4.time_type = MYSQL_TIMESTAMP_DATETIME;
226 value_map.add_values(time4, 2);
227 EXPECT_EQ(value_map.size(), 3U);
228
229 // Check that data is sorted
230 EXPECT_EQ(my_time_compare(time3, value_map.begin()->first), 0);
231 EXPECT_EQ(my_time_compare(time1, std::next(value_map.begin(), 1)->first), 0);
232 EXPECT_EQ(my_time_compare(time2, std::next(value_map.begin(), 2)->first), 0);
233
234 // Check that the counts are correct
235 EXPECT_EQ(value_map.begin()->second, 3U);
236 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 1U);
237 EXPECT_EQ(std::next(value_map.begin(), 2)->second, 4U);
238 }
239
240 TEST_F(ValueMapTest, StringValueMap) {
241 histograms::Value_map<String> value_map(&my_charset_latin1,
242 histograms::Value_map_type::STRING);
243
244 EXPECT_EQ(value_map.size(), 0U);
245
246 value_map.add_values(String("string2", &my_charset_latin1), 1);
247 EXPECT_EQ(value_map.size(), 1U);
248
249 value_map.add_values(String("string2", &my_charset_latin1), 1);
250 EXPECT_EQ(value_map.size(), 1U);
251
252 value_map.add_values(String("string1", &my_charset_latin1), 1);
253 EXPECT_EQ(value_map.size(), 2U);
254
255 value_map.add_values(String("string1", &my_charset_latin1), 1000);
256 EXPECT_EQ(value_map.size(), 2U);
257
258 // Check that data is sorted
259 EXPECT_EQ(strcmp(value_map.begin()->first.ptr(), "string1"), 0);
260 EXPECT_EQ(strcmp(std::next(value_map.begin(), 1)->first.ptr(), "string2"), 0);
261
262 // Check that the counts are correct
263 EXPECT_EQ(value_map.begin()->second, 1001U);
264 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 2U);
265 }
266
267 TEST_F(ValueMapTest, ValueMapWithLongStrings) {
268 histograms::Value_map<String> value_map(&my_charset_latin1,
269 histograms::Value_map_type::STRING);
270
271 EXPECT_EQ(value_map.size(), 0U);
272
273 /*
274 If HISTOGRAM_MAX_COMPARE_LENGTH change to anything else than 42, some of
275 these tests needs to be changed accordingly.
276 */
277 EXPECT_TRUE(histograms::HISTOGRAM_MAX_COMPARE_LENGTH == 42);
278
279 // Exactly 42 characters
280 value_map.add_values(
281 String("abcdefghijklmnopqrstuvwxyz1234567890abcdef", &my_charset_latin1),
282 1);
283 EXPECT_EQ(value_map.size(), 1U);
284
285 // Exactly 42 characters, with a small difference from the first string
286 value_map.add_values(
287 String("abcdefghijklmnopqrstuvwxyz1234567890abcdeg", &my_charset_latin1),
288 1);
289 EXPECT_EQ(value_map.size(), 2U);
290
291 /*
292 Exactly 43 characters. The 42 first are the same as the first string, so no
293 new values should be added.
294 */
295 value_map.add_values(
296 String("abcdefghijklmnopqrstuvwxyz1234567890abcdef1", &my_charset_latin1),
297 1);
298 EXPECT_EQ(value_map.size(), 2U);
299
300 // Check that data is sorted
301 EXPECT_EQ(strcmp(value_map.begin()->first.ptr(),
302 "abcdefghijklmnopqrstuvwxyz1234567890abcdef"),
303 0);
304 EXPECT_EQ(strcmp(std::next(value_map.begin(), 1)->first.ptr(),
305 "abcdefghijklmnopqrstuvwxyz1234567890abcdeg"),
306 0);
307
308 // Check that the counts are correct
309 EXPECT_EQ(value_map.begin()->second, 2U);
310 EXPECT_EQ(std::next(value_map.begin(), 1)->second, 1U);
311 }
312
313 TEST_F(ValueMapTest, LongLongValueMapExtended) {
314 histograms::Value_map<longlong> value_map(&my_charset_latin1,
315 histograms::Value_map_type::INT);
316
317 EXPECT_EQ(value_map.size(), 0U);
318
319 std::random_device rd;
320 std::mt19937 gen(rd());
321 std::uniform_int_distribution<longlong> dis(-1000, 1000);
322
323 for (int i = 0; i < 10000; ++i) value_map.add_values(dis(gen), 1);
324
325 // Check that all values are ordered and unique.
326 auto previous = value_map.begin();
327 for (auto current = std::next(value_map.begin()); current != value_map.end();
328 ++current) {
329 EXPECT_LT(previous->first, current->first);
330 std::advance(previous, 1);
331 }
332 }
333
334 /*
335 A microbenchmark that estimates how long it takes to insert a given number of
336 uniformly distributed integer values into a Value_map.
337 */
338 static void benchmark_insertion_integer(size_t num_iterations,
339 longlong min_value,
340 longlong max_value) {
341 const int values_to_add = 2000;
342 StopBenchmarkTiming();
343
344 std::random_device rd;
345 std::mt19937 gen(rd());
346 std::uniform_int_distribution<longlong> dis(min_value, max_value);
347
348 StartBenchmarkTiming();
349
350 for (size_t i = 0; i < num_iterations; ++i) {
351 histograms::Value_map<longlong> value_map(&my_charset_numeric,
352 histograms::Value_map_type::INT);
353
354 for (int j = 0; j < values_to_add; ++j) {
355 EXPECT_FALSE(value_map.add_values(dis(gen), 1));
356 }
357 }
358
359 StopBenchmarkTiming();
360 }
361
362 /*
363 A microbenchmark that estimates how long it takes to insert a given number of
364 random string values into a Value_map.
365 */
366 static void benchmark_insertion_string(size_t num_iterations,
367 int min_string_length,
368 int max_string_length) {
369 const int values_to_add = 2000;
370 StopBenchmarkTiming();
371
372 std::string characters(
373 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
374
375 std::random_device rd;
376 std::mt19937 gen(rd());
377 std::uniform_int_distribution<size_t> random_character_dis(0,
378 characters.size());
379 std::uniform_int_distribution<int> string_length_dis(min_string_length,
380 max_string_length);
381
382 StartBenchmarkTiming();
383
384 String str;
385 str.set_charset(&my_charset_utf8mb4_0900_ai_ci);
386 str.reserve(max_string_length);
387
388 for (size_t it = 0; it < num_iterations; ++it) {
389 histograms::Value_map<String> value_map(&my_charset_utf8mb4_0900_ai_ci,
390 histograms::Value_map_type::STRING);
391
392 for (int j = 0; j < values_to_add; ++j) {
393 str.length(0);
394
395 const int string_length = string_length_dis(gen);
396 for (int i = 0; i < string_length; ++i) {
397 str.append(characters[random_character_dis(gen)]);
398 }
399
400 EXPECT_FALSE(value_map.add_values(str, 1));
401 }
402 }
403
404 StopBenchmarkTiming();
405 }
406
407 static void BM_ValueMapInsertionIntSmallRange(size_t num_iterations) {
408 // Test a scenario where we have many duplicate values.
409 benchmark_insertion_integer(num_iterations, 0, 20);
410 }
411 BENCHMARK(BM_ValueMapInsertionIntSmallRange)
412
413 static void BM_ValueMapInsertionIntMediumRange(size_t num_iterations) {
414 // Test a scenario where we get a medium amount of duplicate values.
415 // On average, each value has approx 2 duplicate values (int range from 0 to
416 // 1000, and 2000 values are generated).
417 benchmark_insertion_integer(num_iterations, 0, 1000);
418 }
419 BENCHMARK(BM_ValueMapInsertionIntMediumRange)
420
421 static void BM_ValueMapInsertionIntLargeRange(size_t num_iterations) {
422 // Test a scenario where we most likely only have unique values.
423 benchmark_insertion_integer(num_iterations, 0, 100000);
424 }
425 BENCHMARK(BM_ValueMapInsertionIntLargeRange)
426
427 static void BM_ValueMapInsertionString(size_t num_iterations) {
428 // This test will insert 2000 random string values into a Value_map, and we
429 // have 62 characters to choose from. With a string length of 3, we get a
430 // total of approx 238000 possible strings. That means we most likely will
431 // insert only unique values into the Value_map.
432 benchmark_insertion_string(num_iterations, 1, 3);
433 }
434 BENCHMARK(BM_ValueMapInsertionString)
435
436 } // namespace value_map_unittest