ClickHouse/dbms/programs/performance-test/PerformanceTest.cpp

303 lines
9.4 KiB
C++
Raw Normal View History

2019-01-29 10:05:15 +00:00
#include "PerformanceTest.h"
#include <Core/Types.h>
#include <Common/CpuId.h>
#include <common/getMemoryAmount.h>
2019-03-02 21:41:28 +00:00
#include <IO/ConnectionTimeouts.h>
2017-05-06 14:36:37 +00:00
#include <IO/ReadBufferFromFile.h>
2017-05-05 11:36:55 +00:00
#include <IO/ReadHelpers.h>
2017-05-08 19:25:38 +00:00
#include <IO/WriteBufferFromFile.h>
#include <boost/filesystem.hpp>
#include "executeQuery.h"
2017-05-06 14:36:37 +00:00
2017-01-13 18:26:51 +00:00
namespace DB
{
2017-01-13 18:26:51 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
2017-01-13 18:26:51 +00:00
}
namespace
{
void waitQuery(Connection & connection)
{
bool finished = false;
2019-03-02 21:41:28 +00:00
while (true)
{
if (!connection.poll(1000000))
continue;
Connection::Packet packet = connection.receivePacket();
switch (packet.type)
{
case Protocol::Server::EndOfStream:
finished = true;
break;
case Protocol::Server::Exception:
throw *packet.exception;
}
if (finished)
break;
}
}
}
namespace fs = boost::filesystem;
2017-03-27 18:25:42 +00:00
PerformanceTest::PerformanceTest(
const XMLConfigurationPtr & config_,
Connection & connection_,
2019-03-02 21:41:28 +00:00
const ConnectionTimeouts & timeouts_,
InterruptListener & interrupt_listener_,
2019-01-28 11:20:44 +00:00
const PerformanceTestInfo & test_info_,
2019-02-04 17:37:55 +00:00
Context & context_,
const std::vector<size_t> & queries_to_run_)
: config(config_)
, connection(connection_)
2019-03-02 21:41:28 +00:00
, timeouts(timeouts_)
, interrupt_listener(interrupt_listener_)
, test_info(test_info_)
2019-01-28 11:20:44 +00:00
, context(context_)
2019-02-04 17:37:55 +00:00
, queries_to_run(queries_to_run_)
2019-01-28 11:20:44 +00:00
, log(&Poco::Logger::get("PerformanceTest"))
2017-01-13 18:26:51 +00:00
{
}
2017-05-08 19:25:38 +00:00
bool PerformanceTest::checkPreconditions() const
{
if (!config->has("preconditions"))
2017-05-08 19:25:38 +00:00
return true;
2017-05-05 11:36:55 +00:00
Strings preconditions;
config->keys("preconditions", preconditions);
size_t table_precondition_index = 0;
size_t cpu_precondition_index = 0;
2017-05-05 11:36:55 +00:00
for (const std::string & precondition : preconditions)
2017-05-07 00:29:30 +00:00
{
if (precondition == "flush_disk_cache")
2017-05-07 00:29:30 +00:00
{
if (system(
"(>&2 echo 'Flushing disk cache...') && (sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches') && (>&2 echo 'Flushed.')"))
2017-06-13 06:21:52 +00:00
{
LOG_WARNING(log, "Failed to flush disk cache");
return false;
2017-05-06 14:36:37 +00:00
}
}
2017-05-05 11:36:55 +00:00
if (precondition == "ram_size")
2017-05-05 11:36:55 +00:00
{
size_t ram_size_needed = config->getUInt64("preconditions.ram_size");
size_t actual_ram = getMemoryAmount();
if (!actual_ram)
throw Exception("ram_size precondition not available on this platform", ErrorCodes::NOT_IMPLEMENTED);
2017-05-05 11:36:55 +00:00
if (ram_size_needed > actual_ram)
2018-01-10 00:04:08 +00:00
{
LOG_WARNING(log, "Not enough RAM: need = " << ram_size_needed << ", present = " << actual_ram);
return false;
2017-11-16 18:03:31 +00:00
}
2017-05-05 11:36:55 +00:00
}
if (precondition == "table_exists")
2017-05-08 19:25:38 +00:00
{
std::string precondition_key = "preconditions.table_exists[" + std::to_string(table_precondition_index++) + "]";
std::string table_to_check = config->getString(precondition_key);
std::string query = "EXISTS TABLE " + table_to_check + ";";
2017-05-05 15:58:25 +00:00
size_t exist = 0;
2017-05-05 11:36:55 +00:00
2019-03-02 21:41:28 +00:00
connection.sendQuery(timeouts, query, "", QueryProcessingStage::Complete, &test_info.settings, nullptr, false);
2017-05-05 11:36:55 +00:00
while (true)
2017-05-05 11:36:55 +00:00
{
Connection::Packet packet = connection.receivePacket();
2017-05-05 11:36:55 +00:00
if (packet.type == Protocol::Server::Data)
2017-05-05 11:36:55 +00:00
{
for (const ColumnWithTypeAndName & column : packet.block)
2017-11-16 18:09:28 +00:00
{
if (column.name == "result" && column.column->size() > 0)
{
exist = column.column->get64(0);
if (exist)
break;
}
2017-11-16 18:09:28 +00:00
}
2017-05-05 11:36:55 +00:00
}
if (packet.type == Protocol::Server::Exception
|| packet.type == Protocol::Server::EndOfStream)
break;
2017-11-16 18:09:28 +00:00
}
2017-05-05 11:36:55 +00:00
if (!exist)
2017-05-05 11:36:55 +00:00
{
LOG_WARNING(log, "Table " << table_to_check << " doesn't exist");
return false;
2017-05-05 11:36:55 +00:00
}
}
if (precondition == "cpu")
{
std::string precondition_key = "preconditions.cpu[" + std::to_string(cpu_precondition_index++) + "]";
std::string flag_to_check = config->getString(precondition_key);
#define CHECK_CPU_PRECONDITION(OP) \
if (flag_to_check == #OP) \
{ \
if (!Cpu::CpuFlagsCache::have_##OP) \
{ \
LOG_WARNING(log, "CPU doesn't support " << #OP); \
return false; \
} \
} else
CPU_ID_ENUMERATE(CHECK_CPU_PRECONDITION)
{
LOG_WARNING(log, "CPU doesn't support " << flag_to_check);
return false;
}
#undef CHECK_CPU_PRECONDITION
}
2017-05-05 11:36:55 +00:00
}
return true;
}
2017-05-05 11:36:55 +00:00
UInt64 PerformanceTest::calculateMaxExecTime() const
{
UInt64 result = 0;
for (const auto & stop_conditions : test_info.stop_conditions_by_run)
{
UInt64 condition_max_time = stop_conditions.getMaxExecTime();
if (condition_max_time == 0)
return 0;
result += condition_max_time;
}
return result;
}
2017-05-05 11:36:55 +00:00
void PerformanceTest::prepare() const
{
for (const auto & query : test_info.create_and_fill_queries)
{
LOG_INFO(log, "Executing create or fill query \"" << query << '\"');
2019-03-02 21:41:28 +00:00
connection.sendQuery(timeouts, query, "", QueryProcessingStage::Complete, &test_info.settings, nullptr, false);
waitQuery(connection);
LOG_INFO(log, "Query finished");
}
}
void PerformanceTest::finish() const
{
for (const auto & query : test_info.drop_queries)
{
LOG_INFO(log, "Executing drop query \"" << query << '\"');
2019-03-02 21:41:28 +00:00
connection.sendQuery(timeouts, query, "", QueryProcessingStage::Complete, &test_info.settings, nullptr, false);
waitQuery(connection);
LOG_INFO(log, "Query finished");
}
}
std::vector<TestStats> PerformanceTest::execute()
{
std::vector<TestStats> statistics_by_run;
2019-02-04 17:37:55 +00:00
size_t query_count;
if (queries_to_run.empty())
query_count = test_info.queries.size();
else
query_count = queries_to_run.size();
size_t total_runs = test_info.times_to_run * test_info.queries.size();
statistics_by_run.resize(total_runs);
2019-02-04 17:37:55 +00:00
LOG_INFO(log, "Totally will run cases " << test_info.times_to_run * query_count << " times");
UInt64 max_exec_time = calculateMaxExecTime();
if (max_exec_time != 0)
LOG_INFO(log, "Test will be executed for a maximum of " << max_exec_time / 1000. << " seconds");
else
LOG_INFO(log, "Test execution time cannot be determined");
for (size_t number_of_launch = 0; number_of_launch < test_info.times_to_run; ++number_of_launch)
2017-05-05 11:36:55 +00:00
{
QueriesWithIndexes queries_with_indexes;
2017-05-05 11:36:55 +00:00
for (size_t query_index = 0; query_index < test_info.queries.size(); ++query_index)
2017-05-05 11:36:55 +00:00
{
2019-02-04 17:37:55 +00:00
if (queries_to_run.empty() || std::find(queries_to_run.begin(), queries_to_run.end(), query_index) != queries_to_run.end())
{
size_t statistic_index = number_of_launch * test_info.queries.size() + query_index;
queries_with_indexes.push_back({test_info.queries[query_index], statistic_index});
}
else
LOG_INFO(log, "Will skip query " << test_info.queries[query_index] << " by index");
2017-05-05 11:36:55 +00:00
}
if (got_SIGINT)
break;
2017-05-05 11:36:55 +00:00
runQueries(queries_with_indexes, statistics_by_run);
2017-05-05 11:36:55 +00:00
}
return statistics_by_run;
}
2017-05-05 11:36:55 +00:00
void PerformanceTest::runQueries(
const QueriesWithIndexes & queries_with_indexes,
std::vector<TestStats> & statistics_by_run)
{
for (const auto & [query, run_index] : queries_with_indexes)
2017-05-05 11:36:55 +00:00
{
LOG_INFO(log, "[" << run_index<< "] Run query '" << query << "'");
TestStopConditions & stop_conditions = test_info.stop_conditions_by_run[run_index];
TestStats & statistics = statistics_by_run[run_index];
2019-01-30 11:48:23 +00:00
statistics.startWatches();
try
2017-05-05 11:36:55 +00:00
{
executeQuery(connection, query, statistics, stop_conditions, interrupt_listener, context, test_info.settings);
2017-05-05 11:36:55 +00:00
if (test_info.exec_type == ExecutionType::Loop)
2017-05-05 11:36:55 +00:00
{
LOG_INFO(log, "Will run query in loop");
for (size_t iteration = 1; !statistics.got_SIGINT; ++iteration)
2017-05-05 16:49:14 +00:00
{
stop_conditions.reportIterations(iteration);
if (stop_conditions.areFulfilled())
{
LOG_INFO(log, "Stop conditions fullfilled");
break;
}
2017-05-09 17:17:24 +00:00
executeQuery(connection, query, statistics, stop_conditions, interrupt_listener, context, test_info.settings);
2017-05-05 11:36:55 +00:00
}
}
}
catch (const Exception & e)
2017-05-05 11:36:55 +00:00
{
2019-01-30 11:07:10 +00:00
statistics.exception = "Code: " + std::to_string(e.code()) + ", e.displayText() = " + e.displayText();
LOG_WARNING(log, "Code: " << e.code() << ", e.displayText() = " << e.displayText()
<< ", Stack trace:\n\n" << e.getStackTrace().toString());
2017-05-05 11:36:55 +00:00
}
if (!statistics.got_SIGINT)
statistics.ready = true;
else
{
got_SIGINT = true;
LOG_INFO(log, "Got SIGINT, will terminate as soon as possible");
break;
}
2017-05-05 18:32:38 +00:00
}
}
}