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

183 lines
5.6 KiB
C++
Raw Normal View History

#include "PerformanceTest.h"
2019-01-25 11:03:02 +00:00
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 <common/getMemoryAmount.h>
#include <Core/Types.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;
extern const int LOGICAL_ERROR;
extern const int BAD_ARGUMENTS;
extern const int FILE_DOESNT_EXIST;
2017-01-13 18:26:51 +00:00
}
namespace fs = boost::filesystem;
2017-03-27 18:25:42 +00:00
PerformanceTest::PerformanceTest(
const XMLConfigurationPtr & config_,
Connection & connection_,
InterruptListener & interrupt_listener_,
2019-01-28 11:20:44 +00:00
const PerformanceTestInfo & test_info_,
Context & context_)
: config(config_)
, connection(connection_)
, interrupt_listener(interrupt_listener_)
, test_info(test_info_)
2019-01-28 11:20:44 +00:00
, context(context_)
, 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
2019-01-28 11:20:44 +00:00
LOG_INFO(log, "Checking preconditions");
std::vector<std::string> preconditions;
config->keys("preconditions", preconditions);
size_t table_precondition_index = 0;
2017-05-05 11:36:55 +00:00
for (const 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
{
std::cerr << "Failed to flush disk cache" << std::endl;
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 DB::Exception("ram_size precondition not available on this platform", DB::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
{
2019-01-28 11:20:44 +00:00
LOG_ERROR(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
{
String precondition_key = "preconditions.table_exists[" + std::to_string(table_precondition_index++) + "]";
String table_to_check = config->getString(precondition_key);
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
connection.sendQuery(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
{
std::cerr << "Table " << table_to_check << " doesn't exist" << std::endl;
return false;
2017-05-05 11:36:55 +00:00
}
}
}
return true;
}
2017-05-05 11:36:55 +00:00
2017-05-05 11:36:55 +00:00
std::vector<TestStats> PerformanceTest::execute()
{
std::vector<TestStats> statistics_by_run;
statistics_by_run.resize(test_info.times_to_run * test_info.queries.size());
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
{
size_t statistic_index = number_of_launch * test_info.queries.size() + query_index;
test_info.stop_conditions_by_run[statistic_index].reset();
2017-05-05 11:36:55 +00:00
queries_with_indexes.push_back({test_info.queries[query_index], statistic_index});
2017-05-05 11:36:55 +00:00
}
if (interrupt_listener.check())
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
2017-02-24 22:02:08 +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
{
TestStopConditions & stop_conditions = test_info.stop_conditions_by_run[run_index];
TestStats & statistics = statistics_by_run[run_index];
2017-05-05 11:36:55 +00:00
statistics.clear();
try
2017-05-05 11:36:55 +00:00
{
2019-01-28 11:20:44 +00:00
executeQuery(connection, query, statistics, stop_conditions, interrupt_listener, context);
2017-05-05 11:36:55 +00:00
if (test_info.exec_type == ExecutionType::Loop)
2017-05-05 11:36:55 +00:00
{
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())
break;
2017-05-09 17:17:24 +00:00
2019-01-28 11:20:44 +00:00
executeQuery(connection, query, statistics, stop_conditions, interrupt_listener, context);
2017-05-05 11:36:55 +00:00
}
}
}
catch (const DB::Exception & e)
2017-05-05 11:36:55 +00:00
{
statistics.exception = e.what() + String(", ") + e.displayText();
2017-05-05 11:36:55 +00:00
}
if (!statistics.got_SIGINT)
statistics.ready = true;
2017-05-05 18:32:38 +00:00
}
}
}