| 1 | /* |
| 2 | Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include "lex_string.h" |
| 29 | #include "sql/opt_costconstantcache.h" |
| 30 | #include "sql/opt_costmodel.h" |
| 31 | #include "unittest/gunit/fake_table.h" |
| 32 | #include "unittest/gunit/test_utils.h" |
| 33 | |
| 34 | namespace costmodel_unittest { |
| 35 | |
| 36 | using my_testing::Server_initializer; |
| 37 | |
| 38 | class CostModelTest : public ::testing::Test { |
| 39 | protected: |
| 40 | void SetUp() override { |
| 41 | // Add a storage engine to the hton2plugin array. |
| 42 | // This is needed for the cost model to add cost constants |
| 43 | // for the storage engine |
| 44 | LEX_CSTRING engine_name = {STRING_WITH_LEN("InnoDB")}; |
| 45 | |
| 46 | insert_hton2plugin(0, new st_plugin_int())->name = engine_name; |
| 47 | initializer.SetUp(); |
| 48 | } |
| 49 | void TearDown() override { |
| 50 | initializer.TearDown(); |
| 51 | delete remove_hton2plugin(0); |
| 52 | } |
| 53 | |
| 54 | THD *thd() { return initializer.thd(); } |
| 55 | |
| 56 | Server_initializer initializer; |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | Tests for temporary tables that are not dependent on hard coded cost |
| 61 | constants. |
| 62 | */ |
| 63 | static void test_tmptable_cost( |
| 64 | const Cost_model_server *cm, |
| 65 | Cost_model_server::enum_tmptable_type tmp_table_type) { |
| 66 | const uint rows = 3; |
| 67 | |
| 68 | // Cost of inserting and reading data in a temporary table |
| 69 | EXPECT_EQ(cm->tmptable_readwrite_cost(tmp_table_type, rows, rows), |
| 70 | rows * cm->tmptable_readwrite_cost(tmp_table_type, 1.0, 1.0)); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | Test the Cost_model_server interface. |
| 75 | */ |
| 76 | TEST_F(CostModelTest, CostModelServer) { |
| 77 | const uint rows = 3; |
| 78 | |
| 79 | // Create and initialize the server cost model |
| 80 | Cost_model_server cm; |
| 81 | cm.init(); |
| 82 | |
| 83 | // Create and initialize a cost constant object that will be used |
| 84 | // for verifying default values for cost constants |
| 85 | const Server_cost_constants default_server_cost; |
| 86 | |
| 87 | // Test row evaluate cost |
| 88 | EXPECT_EQ(cm.row_evaluate_cost(1.0), default_server_cost.row_evaluate_cost()); |
| 89 | EXPECT_EQ(cm.row_evaluate_cost(rows), rows * cm.row_evaluate_cost(1.0)); |
| 90 | |
| 91 | // Test key compare cost |
| 92 | EXPECT_EQ(cm.key_compare_cost(1.0), default_server_cost.key_compare_cost()); |
| 93 | EXPECT_EQ(cm.key_compare_cost(rows), rows * cm.key_compare_cost(1.0)); |
| 94 | |
| 95 | // Cost of creating a tempoary table without inserting data into it |
| 96 | EXPECT_EQ(cm.tmptable_create_cost(Cost_model_server::MEMORY_TMPTABLE), |
| 97 | default_server_cost.memory_temptable_create_cost()); |
| 98 | EXPECT_EQ(cm.tmptable_create_cost(Cost_model_server::DISK_TMPTABLE), |
| 99 | default_server_cost.disk_temptable_create_cost()); |
| 100 | |
| 101 | // Cost of inserting one row in a temporary table |
| 102 | EXPECT_EQ( |
| 103 | cm.tmptable_readwrite_cost(Cost_model_server::MEMORY_TMPTABLE, 1.0, 0.0), |
| 104 | default_server_cost.memory_temptable_row_cost()); |
| 105 | EXPECT_EQ( |
| 106 | cm.tmptable_readwrite_cost(Cost_model_server::DISK_TMPTABLE, 1.0, 0.0), |
| 107 | default_server_cost.disk_temptable_row_cost()); |
| 108 | |
| 109 | // Cost of reading one row in a temporary table |
| 110 | EXPECT_EQ( |
| 111 | cm.tmptable_readwrite_cost(Cost_model_server::MEMORY_TMPTABLE, 0.0, 1.0), |
| 112 | default_server_cost.memory_temptable_row_cost()); |
| 113 | EXPECT_EQ( |
| 114 | cm.tmptable_readwrite_cost(Cost_model_server::DISK_TMPTABLE, 0.0, 1.0), |
| 115 | default_server_cost.disk_temptable_row_cost()); |
| 116 | |
| 117 | // Tests for temporary tables that are independent of cost constants |
| 118 | test_tmptable_cost(&cm, Cost_model_server::MEMORY_TMPTABLE); |
| 119 | test_tmptable_cost(&cm, Cost_model_server::DISK_TMPTABLE); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | Test the Cost_model_table interface. |
| 124 | */ |
| 125 | TEST_F(CostModelTest, CostModelTable) { |
| 126 | const uint rows = 3; |
| 127 | const double blocks = 4.0; |
| 128 | const uint key = 0; |
| 129 | |
| 130 | // A table is needed in order to initialize the table cost model |
| 131 | Fake_TABLE table(1, false); |
| 132 | |
| 133 | // Create and initialize a cost model table object |
| 134 | Cost_model_server cost_model_server; |
| 135 | cost_model_server.init(); |
| 136 | Cost_model_table cm; |
| 137 | cm.init(&cost_model_server, &table); |
| 138 | |
| 139 | // Create and initialize a cost constant object that will be used |
| 140 | // for verifying default values for cost constants |
| 141 | const Server_cost_constants default_server_cost; |
| 142 | const SE_cost_constants default_engine_cost; |
| 143 | |
| 144 | // Test row evaluate cost |
| 145 | EXPECT_EQ(cm.row_evaluate_cost(1.0), default_server_cost.row_evaluate_cost()); |
| 146 | EXPECT_EQ(cm.row_evaluate_cost(rows), rows * cm.row_evaluate_cost(1.0)); |
| 147 | |
| 148 | // Test key compare cost |
| 149 | EXPECT_EQ(cm.key_compare_cost(1.0), default_server_cost.key_compare_cost()); |
| 150 | EXPECT_EQ(cm.key_compare_cost(rows), rows * cm.key_compare_cost(1.0)); |
| 151 | |
| 152 | // Test io block read cost |
| 153 | EXPECT_EQ(cm.io_block_read_cost(1.0), |
| 154 | default_engine_cost.io_block_read_cost()); |
| 155 | EXPECT_EQ(cm.io_block_read_cost(blocks), blocks * cm.io_block_read_cost(1.0)); |
| 156 | |
| 157 | // Test page_read_cost() with table in memory buffer |
| 158 | EXPECT_EQ(cm.page_read_cost(1.0), |
| 159 | default_engine_cost.memory_block_read_cost()); |
| 160 | EXPECT_EQ(cm.page_read_cost(blocks), blocks * cm.page_read_cost(1.0)); |
| 161 | |
| 162 | // Test page_read_cost() with table data on disk |
| 163 | table.file->stats.table_in_mem_estimate = 0.0; // Table is on disk |
| 164 | EXPECT_EQ(cm.page_read_cost(1.0), default_engine_cost.io_block_read_cost()); |
| 165 | EXPECT_EQ(cm.page_read_cost(blocks), blocks * cm.page_read_cost(1.0)); |
| 166 | |
| 167 | // Test page_read_cost_index() with index data in memory |
| 168 | table.key_info[key].set_in_memory_estimate(1.0); // Index is in memory |
| 169 | EXPECT_EQ(cm.page_read_cost_index(key, 1.0), |
| 170 | default_engine_cost.memory_block_read_cost()); |
| 171 | EXPECT_EQ(cm.page_read_cost_index(key, blocks), |
| 172 | blocks * cm.page_read_cost_index(key, 1.0)); |
| 173 | |
| 174 | // Test page_read_oost_index() with index data on disk |
| 175 | table.key_info[key].set_in_memory_estimate(0.0); // Index is on disk |
| 176 | EXPECT_EQ(cm.page_read_cost_index(key, 1.0), |
| 177 | default_engine_cost.io_block_read_cost()); |
| 178 | EXPECT_EQ(cm.page_read_cost_index(key, blocks), |
| 179 | blocks * cm.page_read_cost_index(key, 1.0)); |
| 180 | |
| 181 | // Test disk seek base cost |
| 182 | EXPECT_EQ(cm.disk_seek_base_cost(), |
| 183 | DISK_SEEK_BASE_COST * cm.io_block_read_cost(1.0)); |
| 184 | |
| 185 | // Test disk seek cost |
| 186 | EXPECT_GT(cm.disk_seek_cost(2.0), cm.disk_seek_cost(1.0)); |
| 187 | } |
| 188 | |
| 189 | } // namespace costmodel_unittest |