| 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 <string> |
| 26 | #include "sql/dd/impl/tables/tables.h" |
| 27 | #include "sql/dd/impl/types/weak_object_impl.h" |
| 28 | #include "sql/dd/string_type.h" |
| 29 | #include "unittest/gunit/benchmark.h" |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | /** |
| 34 | A dummy class that is instantiated with or without PFS instrumentation |
| 35 | depending on the template parameter. We can also set the number of children |
| 36 | using a template parameter. The children will be allocated dynamically, and |
| 37 | get the same PFS instrumentation as their parent. Note that the children |
| 38 | will not get their own children; there is only one level. |
| 39 | |
| 40 | @tparam with_pfs Whether to use PFS instrumented memory management or not. |
| 41 | @tparam n_children Number of children to be allocated. |
| 42 | */ |
| 43 | template <bool with_pfs, size_t n_children> |
| 44 | class Dummy_object : public dd::Weak_object_impl_<with_pfs> { |
| 45 | private: |
| 46 | // Some compilers reject zero sized arrays, so make room for at least one. |
| 47 | Dummy_object<with_pfs, 0> *m_children[n_children ? n_children : 1]; |
| 48 | |
| 49 | public: |
| 50 | Dummy_object() { |
| 51 | // Skip allocating children if n_children is 0 - in this case, the single |
| 52 | // element in the array is left uninitialized. |
| 53 | if (n_children > 0) |
| 54 | for (auto &child : m_children) child = new Dummy_object<with_pfs, 0>(); |
| 55 | } |
| 56 | ~Dummy_object() { |
| 57 | if (n_children > 0) |
| 58 | for (auto &child : m_children) delete child; |
| 59 | } |
| 60 | |
| 61 | // Dummy definitions needed to make the class non-abstract. |
| 62 | virtual void debug_print(dd::String_type &) const {} |
| 63 | virtual const dd::Object_table &object_table() const { |
| 64 | return dd::tables::Tables::instance(); |
| 65 | } |
| 66 | virtual bool validate() const { return false; } |
| 67 | virtual bool restore_attributes(const dd::Raw_record &) { return false; } |
| 68 | virtual bool store_attributes(dd::Raw_record *) { return false; } |
| 69 | virtual bool has_new_primary_key() const { return false; } |
| 70 | virtual dd::Object_key *create_primary_key() const { return nullptr; } |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | Do a number of iterations where we allocate and free an instance of the |
| 75 | dummy class to measure and compare the time used. A sequence of alloc/free |
| 76 | with PFS instrumentation seems to typically take around 30% more time than |
| 77 | without such instrumentation. |
| 78 | |
| 79 | @tparam with_pfs Whether to use PFS instrumented memory management or not. |
| 80 | @tparam n_children Number of children to be allocated. |
| 81 | */ |
| 82 | template <bool with_pfs, size_t n_children> |
| 83 | void BM_DD_pfs(size_t num_iterations) { |
| 84 | StopBenchmarkTiming(); |
| 85 | StartBenchmarkTiming(); |
| 86 | for (size_t n = 0; n < num_iterations; n++) { |
| 87 | Dummy_object<with_pfs, n_children> *dummy = |
| 88 | new Dummy_object<with_pfs, n_children>(); |
| 89 | delete dummy; |
| 90 | } |
| 91 | StopBenchmarkTiming(); |
| 92 | } |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | /** |
| 97 | Provide some closures instantiating the template function above to |
| 98 | associate somewhat meaningful names with the test cases. |
| 99 | */ |
| 100 | auto W_PFS_0 = [](size_t num_iterations) { |
| 101 | BM_DD_pfs<true, 0>(num_iterations); |
| 102 | }; |
| 103 | |
| 104 | auto WO_PFS_0 = [](size_t num_iterations) { |
| 105 | BM_DD_pfs<false, 0>(num_iterations); |
| 106 | }; |
| 107 | |
| 108 | auto W_PFS_10 = [](size_t num_iterations) { |
| 109 | BM_DD_pfs<true, 10>(num_iterations); |
| 110 | }; |
| 111 | |
| 112 | auto WO_PFS_10 = [](size_t num_iterations) { |
| 113 | BM_DD_pfs<false, 10>(num_iterations); |
| 114 | }; |
| 115 | |
| 116 | auto W_PFS_100 = [](size_t num_iterations) { |
| 117 | BM_DD_pfs<true, 100>(num_iterations); |
| 118 | }; |
| 119 | |
| 120 | auto WO_PFS_100 = [](size_t num_iterations) { |
| 121 | BM_DD_pfs<false, 100>(num_iterations); |
| 122 | }; |
| 123 | |
| 124 | BENCHMARK(W_PFS_0) |
| 125 | BENCHMARK(WO_PFS_0) |
| 126 | |
| 127 | BENCHMARK(W_PFS_10) |
| 128 | BENCHMARK(WO_PFS_10) |
| 129 | |
| 130 | BENCHMARK(W_PFS_100) |
| 131 | BENCHMARK(WO_PFS_100) |