| 1 | /* Copyright (c) 2020, 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 <random> |
| 26 | #include <string> |
| 27 | #include "unhex.h" |
| 28 | #include "unittest/gunit/benchmark.h" |
| 29 | |
| 30 | namespace { |
| 31 | std::string random_string(std::string::size_type length) { |
| 32 | const std::string alphanum = "0123456789abcdefABCDEF"; |
| 33 | std::mt19937 rng{42}; |
| 34 | std::uniform_int_distribution<std::string::size_type> dist( |
| 35 | 0, alphanum.size() - 1); |
| 36 | |
| 37 | std::string result; |
| 38 | result.reserve(length); |
| 39 | for (std::string::size_type i = 0; i < length; i++) { |
| 40 | result += alphanum[dist(rng)]; |
| 41 | } |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | void BM_Unhex(size_t num_iterations, size_t string_size) { |
| 46 | StopBenchmarkTiming(); |
| 47 | auto s = random_string(string_size); |
| 48 | std::string output(string_size * 2, '\0'); |
| 49 | |
| 50 | StartBenchmarkTiming(); |
| 51 | for (size_t n = 0; n < num_iterations; n++) { |
| 52 | (void)unhex(s.data(), s.data() + s.size(), &output[0]); |
| 53 | } |
| 54 | StopBenchmarkTiming(); |
| 55 | SetBytesProcessed(num_iterations * s.size()); |
| 56 | } |
| 57 | } // namespace |
| 58 | |
| 59 | auto UnhexLookup4k = [](size_t num_iterations) { |
| 60 | BM_Unhex(num_iterations, 4 * 1024); |
| 61 | }; |
| 62 | |
| 63 | auto UnhexLookup512k = [](size_t num_iterations) { |
| 64 | BM_Unhex(num_iterations, 512 * 1024); |
| 65 | }; |
| 66 | |
| 67 | auto UnhexLookup1M = [](size_t num_iterations) { |
| 68 | BM_Unhex(num_iterations, 1024 * 1024); |
| 69 | }; |
| 70 | |
| 71 | auto UnhexLookup4M = [](size_t num_iterations) { |
| 72 | BM_Unhex(num_iterations, 4 * 1024 * 1024); |
| 73 | }; |
| 74 | |
| 75 | BENCHMARK(UnhexLookup4k) |
| 76 | BENCHMARK(UnhexLookup512k) |
| 77 | BENCHMARK(UnhexLookup1M) |
| 78 | BENCHMARK(UnhexLookup4M) |