ClickHouse/dbms/src/Client/PerformanceTest.cpp

1432 lines
48 KiB
C++
Raw Normal View History

2017-01-13 18:26:51 +00:00
#include <iostream>
#include <limits>
2017-02-24 22:02:08 +00:00
#include <unistd.h>
2017-01-13 18:26:51 +00:00
2017-01-20 12:36:16 +00:00
#include <boost/program_options.hpp>
2017-05-05 18:32:38 +00:00
#include <boost/filesystem.hpp>
2017-05-05 11:36:55 +00:00
#include <sys/stat.h>
2017-01-20 12:36:16 +00:00
2017-05-05 11:36:55 +00:00
#include <AggregateFunctions/ReservoirSampler.h>
2017-05-05 12:41:18 +00:00
#include <Client/Connection.h>
2017-05-05 11:36:55 +00:00
#include <Common/ConcurrentBoundedQueue.h>
#include <Common/Stopwatch.h>
#include <Common/ThreadPool.h>
#include <Core/Types.h>
#include <DataStreams/RemoteBlockInputStream.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>
#include <Interpreters/Settings.h>
2017-01-20 12:36:16 +00:00
#include <Poco/AutoPtr.h>
2017-05-05 11:36:55 +00:00
#include <Poco/Exception.h>
2017-01-20 12:36:16 +00:00
#include <Poco/SAX/InputSource.h>
#include <Poco/Util/XMLConfiguration.h>
2017-05-05 11:36:55 +00:00
#include <Poco/XML/XMLStream.h>
2017-01-20 12:36:16 +00:00
2017-02-24 22:02:08 +00:00
#include "InterruptListener.h"
2017-01-20 12:36:16 +00:00
2017-01-13 18:26:51 +00:00
/** Tests launcher for ClickHouse.
* The tool walks through given or default folder in order to find files with
2017-04-07 19:01:41 +00:00
* tests' descriptions and launches it.
2017-01-13 18:26:51 +00:00
*/
2017-05-06 14:36:37 +00:00
namespace FS = boost::filesystem;
2017-01-13 18:26:51 +00:00
namespace DB
{
namespace ErrorCodes
{
2017-05-05 11:36:55 +00:00
extern const int POCO_EXCEPTION;
extern const int STD_EXCEPTION;
extern const int UNKNOWN_EXCEPTION;
2017-01-13 18:26:51 +00:00
}
2017-05-05 17:48:11 +00:00
const std::string FOUR_SPACES = " ";
2017-05-05 12:41:18 +00:00
2017-05-05 11:36:55 +00:00
bool isNumber(const std::string & str)
{
if (str.empty())
{
return false;
}
2017-03-13 16:24:50 +00:00
2017-05-05 17:48:11 +00:00
size_t dots_counter = 0;
2017-03-13 16:24:50 +00:00
2017-05-05 11:36:55 +00:00
if (str[0] == '.' || str[str.size() - 1] == '.')
{
2017-03-13 16:24:50 +00:00
return false;
}
2017-05-05 11:36:55 +00:00
for (char chr : str)
{
if (chr == '.')
{
2017-05-05 17:48:11 +00:00
if (dots_counter)
2017-03-13 16:24:50 +00:00
return false;
else
2017-05-05 17:48:11 +00:00
++dots_counter;
2017-03-13 16:24:50 +00:00
continue;
}
2017-05-05 11:36:55 +00:00
if (chr < '0' || chr > '9')
{
2017-03-13 16:24:50 +00:00
return false;
}
}
return true;
}
2017-05-05 11:36:55 +00:00
class JSONString
{
private:
2017-03-13 16:24:50 +00:00
std::map<std::string, std::string> content;
std::string current_key;
size_t _padding = 1;
2017-05-05 11:36:55 +00:00
public:
JSONString(){};
JSONString(size_t padding) : _padding(padding){};
2017-03-13 16:24:50 +00:00
JSONString & operator[](const std::string & key)
{
current_key = key;
return *this;
}
template <typename T>
2017-05-05 11:36:55 +00:00
typename std::enable_if<std::is_arithmetic<T>::value, JSONString &>::type operator[](const T key)
2017-03-13 16:24:50 +00:00
{
current_key = std::to_string(key);
return *this;
}
void set(std::string value)
{
2017-05-05 11:36:55 +00:00
if (current_key.empty())
{
2017-03-13 16:24:50 +00:00
throw "cannot use set without key";
}
2017-05-05 11:36:55 +00:00
if (value.empty())
{
2017-03-13 16:24:50 +00:00
value = "null";
}
bool reserved = (value[0] == '[' || value[0] == '{' || value == "null");
2017-05-05 11:36:55 +00:00
if (!reserved && !isNumber(value))
{
2017-03-13 16:24:50 +00:00
value = '\"' + value + '\"';
}
content[current_key] = value;
current_key = "";
}
2017-05-05 17:48:11 +00:00
void set(const JSONString & inner_json)
2017-03-13 16:24:50 +00:00
{
2017-05-05 17:48:11 +00:00
set(inner_json.constructOutput());
2017-03-13 16:24:50 +00:00
}
2017-05-05 17:48:11 +00:00
void set(const std::vector<JSONString> & run_infos)
2017-03-13 16:24:50 +00:00
{
2017-05-05 11:36:55 +00:00
if (current_key.empty())
{
2017-03-13 16:24:50 +00:00
throw "cannot use set without key";
}
content[current_key] = "[\n";
2017-05-05 17:48:11 +00:00
for (size_t i = 0; i < run_infos.size(); ++i)
2017-05-05 11:36:55 +00:00
{
for (size_t i = 0; i < _padding + 1; ++i)
{
2017-05-05 17:48:11 +00:00
content[current_key] += FOUR_SPACES;
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
content[current_key] += run_infos[i].constructOutput(_padding + 2);
2017-03-13 16:24:50 +00:00
2017-05-05 17:48:11 +00:00
if (i != run_infos.size() - 1)
2017-05-05 11:36:55 +00:00
{
content[current_key] += ',';
}
2017-03-13 16:24:50 +00:00
2017-05-05 11:36:55 +00:00
content[current_key] += "\n";
2017-03-13 16:24:50 +00:00
}
2017-05-05 11:36:55 +00:00
for (size_t i = 0; i < _padding; ++i)
{
2017-05-05 17:48:11 +00:00
content[current_key] += FOUR_SPACES;
2017-03-13 16:24:50 +00:00
}
content[current_key] += ']';
current_key = "";
}
template <typename T>
typename std::enable_if<std::is_arithmetic<T>::value, void>::type set(T value)
{
set(std::to_string(value));
}
std::string constructOutput() const
{
2017-05-05 11:36:55 +00:00
return constructOutput(_padding);
2017-03-13 16:24:50 +00:00
}
std::string constructOutput(size_t padding) const
{
std::string output = "{";
bool first = true;
2017-05-05 11:36:55 +00:00
for (auto it = content.begin(); it != content.end(); ++it)
{
if (!first)
{
2017-03-13 16:24:50 +00:00
output += ',';
2017-05-05 11:36:55 +00:00
}
else
{
2017-03-13 16:24:50 +00:00
first = false;
}
output += "\n";
2017-05-05 11:36:55 +00:00
for (size_t i = 0; i < padding; ++i)
{
2017-05-05 17:48:11 +00:00
output += FOUR_SPACES;
2017-03-13 16:24:50 +00:00
}
2017-05-05 11:36:55 +00:00
std::string key = '\"' + it->first + '\"';
2017-03-13 16:24:50 +00:00
std::string value = it->second;
output += key + ": " + value;
}
output += "\n";
2017-05-05 11:36:55 +00:00
for (size_t i = 0; i < padding - 1; ++i)
{
2017-05-05 17:48:11 +00:00
output += FOUR_SPACES;
2017-03-13 16:24:50 +00:00
}
output += "}";
return output;
}
};
2017-05-05 17:48:11 +00:00
std::ostream & operator<<(std::ostream & stream, const JSONString & json_obj)
2017-03-13 16:24:50 +00:00
{
2017-05-05 17:48:11 +00:00
stream << json_obj.constructOutput();
2017-03-13 16:24:50 +00:00
return stream;
}
2017-05-05 11:36:55 +00:00
enum PriorityType
{
min,
max
};
2017-03-27 18:25:42 +00:00
2017-05-05 11:36:55 +00:00
struct CriterionWithPriority
{
PriorityType priority;
size_t value;
bool fulfilled;
2017-03-27 18:25:42 +00:00
2017-05-05 11:36:55 +00:00
CriterionWithPriority() : value(0), fulfilled(false)
{
}
CriterionWithPriority(const CriterionWithPriority &) = default;
};
/// Termination criterions. The running test will be terminated in either of two conditions:
/// 1. All criterions marked 'min' are fulfilled
/// or
/// 2. Any criterion marked 'max' is fulfilled
2017-05-05 11:36:55 +00:00
class StopCriterions
{
private:
2017-05-05 11:36:55 +00:00
using AbstractConfiguration = Poco::AutoPtr<Poco::Util::AbstractConfiguration>;
using Keys = std::vector<std::string>;
2017-05-05 17:48:11 +00:00
void initializeStruct(const std::string & priority, const AbstractConfiguration & stop_criterions_view)
2017-05-05 11:36:55 +00:00
{
Keys keys;
2017-05-05 17:48:11 +00:00
stop_criterions_view->keys(priority, keys);
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
PriorityType priority_type = (priority == "min" ? min : max);
2017-05-05 11:36:55 +00:00
for (const std::string & key : keys)
{
if (key == "timeout_ms")
{
2017-05-05 17:48:11 +00:00
timeout_ms.value = stop_criterions_view->getUInt64(priority + ".timeout_ms");
timeout_ms.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else if (key == "rows_read")
{
2017-05-05 17:48:11 +00:00
rows_read.value = stop_criterions_view->getUInt64(priority + ".rows_read");
rows_read.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else if (key == "bytes_read_uncompressed")
{
2017-05-05 17:48:11 +00:00
bytes_read_uncompressed.value = stop_criterions_view->getUInt64(priority + ".bytes_read_uncompressed");
bytes_read_uncompressed.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else if (key == "iterations")
{
2017-05-05 17:48:11 +00:00
iterations.value = stop_criterions_view->getUInt64(priority + ".iterations");
iterations.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else if (key == "min_time_not_changing_for_ms")
{
2017-05-05 17:48:11 +00:00
min_time_not_changing_for_ms.value = stop_criterions_view->getUInt64(priority + ".min_time_not_changing_for_ms");
min_time_not_changing_for_ms.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else if (key == "max_speed_not_changing_for_ms")
{
2017-05-05 17:48:11 +00:00
max_speed_not_changing_for_ms.value = stop_criterions_view->getUInt64(priority + ".max_speed_not_changing_for_ms");
max_speed_not_changing_for_ms.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else if (key == "average_speed_not_changing_for_ms")
{
2017-05-05 17:48:11 +00:00
average_speed_not_changing_for_ms.value = stop_criterions_view->getUInt64(priority + ".average_speed_not_changing_for_ms");
average_speed_not_changing_for_ms.priority = priority_type;
2017-05-05 11:36:55 +00:00
}
else
{
throw Poco::Exception("Met unkown stop criterion: " + key, 1);
}
if (priority == "min")
{
++number_of_initialized_min;
};
if (priority == "max")
{
++number_of_initialized_max;
};
}
}
public:
2017-05-05 11:36:55 +00:00
StopCriterions() : number_of_initialized_min(0), number_of_initialized_max(0), fulfilled_criterions_min(0), fulfilled_criterions_max(0)
{
}
2017-05-05 17:48:11 +00:00
StopCriterions(const StopCriterions & another_criterions)
: timeout_ms(another_criterions.timeout_ms),
rows_read(another_criterions.rows_read),
bytes_read_uncompressed(another_criterions.bytes_read_uncompressed),
iterations(another_criterions.iterations),
min_time_not_changing_for_ms(another_criterions.min_time_not_changing_for_ms),
max_speed_not_changing_for_ms(another_criterions.max_speed_not_changing_for_ms),
average_speed_not_changing_for_ms(another_criterions.average_speed_not_changing_for_ms),
number_of_initialized_min(another_criterions.number_of_initialized_min),
number_of_initialized_max(another_criterions.number_of_initialized_max),
fulfilled_criterions_min(another_criterions.fulfilled_criterions_min),
fulfilled_criterions_max(another_criterions.fulfilled_criterions_max)
2017-05-05 11:36:55 +00:00
{
}
2017-05-05 17:48:11 +00:00
void loadFromConfig(const AbstractConfiguration & stop_criterions_view)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
if (stop_criterions_view->has("min"))
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
initializeStruct("min", stop_criterions_view);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (stop_criterions_view->has("max"))
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
initializeStruct("max", stop_criterions_view);
2017-05-05 11:36:55 +00:00
}
}
void reset()
{
timeout_ms.fulfilled = false;
rows_read.fulfilled = false;
bytes_read_uncompressed.fulfilled = false;
iterations.fulfilled = false;
min_time_not_changing_for_ms.fulfilled = false;
max_speed_not_changing_for_ms.fulfilled = false;
average_speed_not_changing_for_ms.fulfilled = false;
fulfilled_criterions_min = 0;
fulfilled_criterions_max = 0;
}
CriterionWithPriority timeout_ms;
CriterionWithPriority rows_read;
CriterionWithPriority bytes_read_uncompressed;
CriterionWithPriority iterations;
CriterionWithPriority min_time_not_changing_for_ms;
CriterionWithPriority max_speed_not_changing_for_ms;
CriterionWithPriority average_speed_not_changing_for_ms;
/// Hereafter 'min' and 'max', in context of critetions, mean a level of importance
2017-05-05 11:36:55 +00:00
/// Number of initialized properties met in configuration
2017-05-05 12:41:18 +00:00
size_t number_of_initialized_min;
size_t number_of_initialized_max;
2017-05-05 12:41:18 +00:00
size_t fulfilled_criterions_min;
size_t fulfilled_criterions_max;
};
struct Stats
{
2017-05-05 11:36:55 +00:00
Stopwatch watch;
Stopwatch watch_per_query;
Stopwatch min_time_watch;
Stopwatch max_rows_speed_watch;
Stopwatch max_bytes_speed_watch;
Stopwatch avg_rows_speed_watch;
Stopwatch avg_bytes_speed_watch;
size_t queries;
size_t rows_read;
size_t bytes_read;
using Sampler = ReservoirSampler<double>;
Sampler sampler{1 << 16};
/// min_time in ms
UInt64 min_time = std::numeric_limits<UInt64>::max();
double total_time = 0;
double max_rows_speed = 0;
double max_bytes_speed = 0;
double avg_rows_speed_value = 0;
double avg_rows_speed_first = 0;
static double avg_rows_speed_precision;
double avg_bytes_speed_value = 0;
double avg_bytes_speed_first = 0;
static double avg_bytes_speed_precision;
size_t number_of_rows_speed_info_batches = 0;
size_t number_of_bytes_speed_info_batches = 0;
2017-05-05 12:41:18 +00:00
bool ready = false; // check if a query wasn't interrupted by SIGINT
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
std::string getStatisticByName(const std::string & statistic_name)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
if (statistic_name == "min_time")
2017-05-05 11:36:55 +00:00
{
return std::to_string(min_time) + "ms";
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "quantiles")
2017-05-05 11:36:55 +00:00
{
std::string result = "\n";
for (double percent = 10; percent <= 90; percent += 10)
{
2017-05-05 17:48:11 +00:00
result += FOUR_SPACES + std::to_string((percent / 100));
2017-05-05 11:36:55 +00:00
result += ": " + std::to_string(sampler.quantileInterpolated(percent / 100.0));
result += "\n";
}
2017-05-05 17:48:11 +00:00
result += FOUR_SPACES + "0.95: " + std::to_string(sampler.quantileInterpolated(95 / 100.0)) + "\n";
result += FOUR_SPACES + "0.99: " + std::to_string(sampler.quantileInterpolated(99 / 100.0)) + "\n";
result += FOUR_SPACES + "0.999: " + std::to_string(sampler.quantileInterpolated(99.9 / 100.)) + "\n";
result += FOUR_SPACES + "0.9999: " + std::to_string(sampler.quantileInterpolated(99.99 / 100.));
2017-05-05 11:36:55 +00:00
return result;
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "total_time")
2017-05-05 11:36:55 +00:00
{
return std::to_string(total_time) + "s";
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "queries_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(queries / total_time);
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "rows_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(rows_read / total_time);
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "bytes_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(bytes_read / total_time);
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "max_rows_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(max_rows_speed);
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "max_bytes_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(max_bytes_speed);
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "avg_rows_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(avg_rows_speed_value);
}
2017-05-05 17:48:11 +00:00
if (statistic_name == "avg_bytes_per_second")
2017-05-05 11:36:55 +00:00
{
return std::to_string(avg_bytes_speed_value);
}
return "";
}
void update_min_time(const UInt64 min_time_candidate)
{
if (min_time_candidate < min_time)
{
min_time = min_time_candidate;
min_time_watch.restart();
}
}
void update_average_speed(const double new_speed_info,
Stopwatch & avg_speed_watch,
size_t & number_of_info_batches,
double precision,
double & avg_speed_first,
double & avg_speed_value)
{
avg_speed_value = ((avg_speed_value * number_of_info_batches) + new_speed_info);
avg_speed_value /= (++number_of_info_batches);
if (avg_speed_first == 0)
{
avg_speed_first = avg_speed_value;
}
if (abs(avg_speed_value - avg_speed_first) >= precision)
{
avg_speed_first = avg_speed_value;
avg_speed_watch.restart();
}
}
void update_max_speed(const size_t max_speed_candidate, Stopwatch & max_speed_watch, double & max_speed)
{
if (max_speed_candidate > max_speed)
{
max_speed = max_speed_candidate;
max_speed_watch.restart();
}
}
void add(size_t rows_read_inc, size_t bytes_read_inc)
{
rows_read += rows_read_inc;
bytes_read += bytes_read_inc;
double new_rows_speed = rows_read_inc / watch_per_query.elapsedSeconds();
double new_bytes_speed = bytes_read_inc / watch_per_query.elapsedSeconds();
/// Update rows speed
update_max_speed(new_rows_speed, max_rows_speed_watch, max_rows_speed);
update_average_speed(new_rows_speed,
avg_rows_speed_watch,
number_of_rows_speed_info_batches,
avg_rows_speed_precision,
avg_rows_speed_first,
avg_rows_speed_value);
/// Update bytes speed
update_max_speed(new_bytes_speed, max_bytes_speed_watch, max_bytes_speed);
update_average_speed(new_bytes_speed,
avg_bytes_speed_watch,
number_of_bytes_speed_info_batches,
avg_bytes_speed_precision,
avg_bytes_speed_first,
avg_bytes_speed_value);
}
void updateQueryInfo()
{
++queries;
sampler.insert(watch_per_query.elapsedSeconds());
update_min_time(watch_per_query.elapsed() / (1000 * 1000)); /// ns to ms
}
void setTotalTime()
{
total_time = watch.elapsedSeconds();
}
void clear()
{
watch.restart();
watch_per_query.restart();
min_time_watch.restart();
max_rows_speed_watch.restart();
max_bytes_speed_watch.restart();
avg_rows_speed_watch.restart();
avg_bytes_speed_watch.restart();
sampler.clear();
queries = 0;
rows_read = 0;
bytes_read = 0;
min_time = std::numeric_limits<UInt64>::max();
total_time = 0;
max_rows_speed = 0;
max_bytes_speed = 0;
avg_rows_speed_value = 0;
avg_bytes_speed_value = 0;
avg_rows_speed_first = 0;
avg_bytes_speed_first = 0;
avg_rows_speed_precision = 0.001;
avg_bytes_speed_precision = 0.001;
number_of_rows_speed_info_batches = 0;
number_of_bytes_speed_info_batches = 0;
}
};
2017-05-05 11:36:55 +00:00
double Stats::avg_rows_speed_precision = 0.001;
2017-03-27 18:25:42 +00:00
double Stats::avg_bytes_speed_precision = 0.001;
2017-01-13 18:26:51 +00:00
class PerformanceTest
{
public:
2017-05-05 12:41:18 +00:00
PerformanceTest(
2017-05-05 11:36:55 +00:00
const String & host_,
const UInt16 port_,
const String & default_database_,
const String & user_,
const String & password_,
2017-05-05 15:58:25 +00:00
const bool & lite_output_,
2017-05-05 11:36:55 +00:00
const std::vector<std::string> & input_files,
2017-01-20 12:36:16 +00:00
const std::vector<std::string> & tags,
const std::vector<std::string> & without_tags,
const std::vector<std::string> & names,
const std::vector<std::string> & without_names,
const std::vector<std::string> & names_regexp,
2017-05-05 11:36:55 +00:00
const std::vector<std::string> & without_names_regexp)
2017-05-05 12:41:18 +00:00
: connection(host_, port_, default_database_, user_, password_),
2017-05-05 17:48:11 +00:00
tests_configurations(input_files.size()),
2017-05-05 15:58:25 +00:00
gotSIGINT(false),
lite_output(lite_output_)
2017-05-05 11:36:55 +00:00
{
if (input_files.size() < 1)
{
throw Poco::Exception("No tests were specified", 0);
}
std::cerr << std::fixed << std::setprecision(3);
std::cout << std::fixed << std::setprecision(3);
readTestsConfiguration(input_files);
}
2017-01-13 18:26:51 +00:00
private:
2017-05-05 11:36:55 +00:00
unsigned concurrency;
2017-05-05 17:48:11 +00:00
std::string test_name;
2017-05-05 11:36:55 +00:00
using Query = std::string;
using Queries = std::vector<Query>;
using QueriesWithIndexes = std::vector<std::pair<Query, size_t>>;
Queries queries;
2017-05-05 12:41:18 +00:00
Connection connection;
2017-05-05 11:36:55 +00:00
using Keys = std::vector<std::string>;
Settings settings;
InterruptListener interrupt_listener;
double average_speed_precision = 0.001;
using XMLConfiguration = Poco::Util::XMLConfiguration;
using AbstractConfig = Poco::AutoPtr<Poco::Util::AbstractConfiguration>;
using Config = Poco::AutoPtr<XMLConfiguration>;
using Paths = std::vector<std::string>;
using StringToVector = std::map<std::string, std::vector<std::string>>;
StringToVector substitutions;
2017-05-05 17:48:11 +00:00
std::vector<Config> tests_configurations;
2017-05-05 11:36:55 +00:00
using StringKeyValue = std::map<std::string, std::string>;
2017-05-05 17:48:11 +00:00
std::vector<StringKeyValue> substitutions_maps;
2017-05-05 11:36:55 +00:00
2017-05-05 12:41:18 +00:00
bool gotSIGINT;
2017-05-05 17:48:11 +00:00
std::vector<StopCriterions> stop_criterions;
2017-05-05 12:41:18 +00:00
std::string main_metric;
2017-05-05 15:58:25 +00:00
bool lite_output;
2017-05-05 11:36:55 +00:00
// TODO: create enum class instead of string
#define incFulfilledCriterions(index, CRITERION) \
2017-05-05 17:48:11 +00:00
if (!stop_criterions[index].CRITERION.fulfilled) \
2017-05-05 11:36:55 +00:00
{ \
2017-05-05 17:48:11 +00:00
stop_criterions[index].CRITERION.priority == min ? ++stop_criterions[index].fulfilled_criterions_min \
: ++stop_criterions[index].fulfilled_criterions_max; \
stop_criterions[index].CRITERION.fulfilled = true; \
2017-05-05 11:36:55 +00:00
}
enum ExecutionType
{
loop,
once
};
2017-05-05 17:48:11 +00:00
ExecutionType exec_type;
2017-05-05 11:36:55 +00:00
2017-05-05 16:49:14 +00:00
size_t times_to_run = 1;
2017-05-05 11:36:55 +00:00
std::vector<Stats> statistics;
void readTestsConfiguration(const Paths & input_files)
{
2017-05-05 17:48:11 +00:00
tests_configurations.resize(input_files.size());
2017-05-05 11:36:55 +00:00
for (size_t i = 0; i != input_files.size(); ++i)
{
const std::string path = input_files[i];
2017-05-05 17:48:11 +00:00
tests_configurations[i] = Config(new XMLConfiguration(path));
2017-05-05 11:36:55 +00:00
}
// TODO: here will be tests filter on tags, names, regexp matching, etc.
// { ... }
// for now let's launch one test only
2017-05-05 17:48:11 +00:00
if (tests_configurations.size())
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
for (auto & test_config : tests_configurations)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
runTest(test_config);
2017-05-05 11:36:55 +00:00
}
}
}
2017-05-05 17:48:11 +00:00
void runTest(Config & test_config)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
test_name = test_config->getString("name");
std::cout << "Running: " << test_name << "\n";
2017-05-05 11:36:55 +00:00
/// Preprocess configuration file
2017-05-05 17:48:11 +00:00
if (test_config->has("settings"))
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
Keys config_settings;
test_config->keys("settings", config_settings);
2017-05-05 11:36:55 +00:00
/// This macro goes through all settings in the Settings.h
/// and, if found any settings in test's xml configuration
/// with the same name, sets its value to settings
std::vector<std::string>::iterator it;
2017-05-05 18:32:38 +00:00
#define EXTRACT_SETTING(TYPE, NAME, DEFAULT) \
it = std::find(config_settings.begin(), config_settings.end(), #NAME); \
if (it != config_settings.end()) \
settings.set(#NAME, test_config->getString("settings." #NAME));
APPLY_FOR_SETTINGS(EXTRACT_SETTING)
APPLY_FOR_LIMITS(EXTRACT_SETTING)
#undef EXTRACT_SETTING
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (std::find(config_settings.begin(), config_settings.end(), "profile") != config_settings.end())
2017-05-05 11:36:55 +00:00
{
// TODO: proceed profile settings in a proper way
}
2017-05-05 17:48:11 +00:00
if (std::find(config_settings.begin(), config_settings.end(), "average_rows_speed_precision") != config_settings.end())
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
Stats::avg_rows_speed_precision = test_config->getDouble("settings.average_rows_speed_precision");
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (std::find(config_settings.begin(), config_settings.end(), "average_bytes_speed_precision") != config_settings.end())
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
Stats::avg_bytes_speed_precision = test_config->getDouble("settings.average_bytes_speed_precision");
2017-05-05 11:36:55 +00:00
}
}
Query query;
2017-05-06 14:36:37 +00:00
if (!test_config->has("query") && !test_config->has("query_file"))
{
throw Poco::Exception("Missing query fields in test's config: " + test_name, 1);
}
if (test_config->has("query") && test_config->has("query_file"))
2017-05-05 11:36:55 +00:00
{
2017-05-06 14:36:37 +00:00
throw Poco::Exception("Found both query and query_file fields. Choose only one", 1);
2017-05-05 11:36:55 +00:00
}
2017-05-06 14:36:37 +00:00
if (test_config->has("query"))
queries.push_back(test_config->getString("query"));
if (test_config->has("query_file"))
{
const std::string filename = test_config->getString("query_file");
if (filename.empty())
throw Poco::Exception("Empty file name", 1);
bool tsv = FS::path(filename).extension().string() == ".tsv";
ReadBufferFromFile query_file(filename);
while (!query_file.eof())
{
2017-05-06 22:25:18 +00:00
tsv ? readEscapedString(query, query_file, true)
: readString(query, query_file, true);
if (!query.empty())
queries.push_back(query);
2017-05-06 14:36:37 +00:00
}
}
2017-05-05 11:36:55 +00:00
2017-05-06 14:36:37 +00:00
if (queries.empty())
2017-05-05 11:36:55 +00:00
{
2017-05-06 14:36:37 +00:00
throw Poco::Exception("Did not find any query to execute: " + test_name, 1);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (test_config->has("substitutions"))
2017-05-05 11:36:55 +00:00
{
/// Make "subconfig" of inner xml block
2017-05-05 17:48:11 +00:00
AbstractConfig substitutions_view(test_config->createView("substitutions"));
constructSubstitutions(substitutions_view, substitutions);
2017-05-05 11:36:55 +00:00
queries = formatQueries(query, substitutions);
}
else
{
// TODO: probably it will be a good practice to check if
// query string has {substitution pattern}, but no substitution field
// was found in xml configuration
queries.push_back(query);
}
2017-05-05 17:48:11 +00:00
if (!test_config->has("type"))
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
throw Poco::Exception("Missing type property in config: " + test_name, 1);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
std::string config_exec_type = test_config->getString("type");
if (config_exec_type == "loop")
exec_type = loop;
else if (config_exec_type == "once")
exec_type = once;
2017-05-05 11:36:55 +00:00
else
2017-05-05 17:48:11 +00:00
throw Poco::Exception("Unknown type " + config_exec_type + " in :" + test_name, 1);
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (test_config->has("times_to_run"))
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
times_to_run = test_config->getUInt("times_to_run");
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
stop_criterions.resize(times_to_run * queries.size());
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (test_config->has("stop"))
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
AbstractConfig stop_criterions_view(test_config->createView("stop"));
for (StopCriterions & stop_criterion : stop_criterions)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
stop_criterion.loadFromConfig(stop_criterions_view);
2017-05-05 11:36:55 +00:00
}
}
else
{
throw Poco::Exception("No termination conditions were found in config", 1);
}
2017-05-05 17:48:11 +00:00
AbstractConfig metrics_view(test_config->createView("metrics"));
2017-05-05 11:36:55 +00:00
Keys metrics;
2017-05-05 17:48:11 +00:00
metrics_view->keys(metrics);
main_metric = test_config->getString("main_metric", "");
2017-05-05 11:36:55 +00:00
2017-05-05 15:58:25 +00:00
if (!main_metric.empty()) {
if (std::find(metrics.begin(), metrics.end(), main_metric) == metrics.end())
metrics.push_back(main_metric);
} else {
if (lite_output)
throw Poco::Exception("Specify main_metric for lite output", 1);
2017-05-05 11:36:55 +00:00
}
2017-05-05 15:58:25 +00:00
if (metrics.size() > 0)
checkMetricsInput(metrics);
2017-05-05 11:36:55 +00:00
2017-05-05 16:49:14 +00:00
statistics.resize(times_to_run * queries.size());
2017-05-05 17:48:11 +00:00
for (size_t number_of_launch = 0; number_of_launch < times_to_run; ++number_of_launch)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
QueriesWithIndexes queries_with_indexes;
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
for (size_t query_index = 0; query_index < queries.size(); ++query_index)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
size_t statistic_index = number_of_launch * queries.size() + query_index;
stop_criterions[statistic_index].reset();
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
queries_with_indexes.push_back({queries[query_index], statistic_index});
2017-05-05 11:36:55 +00:00
}
if (interrupt_listener.check())
gotSIGINT = true;
if (gotSIGINT)
break;
2017-05-05 17:48:11 +00:00
runQueries(queries_with_indexes);
2017-05-05 11:36:55 +00:00
}
2017-05-05 15:58:25 +00:00
if (lite_output)
minOutput(main_metric);
2017-05-05 11:36:55 +00:00
else
constructTotalInfo();
}
2017-05-05 15:58:25 +00:00
void checkMetricsInput(const Strings & metrics) const
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
std::vector<std::string> loop_metrics
2017-05-05 11:36:55 +00:00
= {"min_time", "quantiles", "total_time", "queries_per_second", "rows_per_second", "bytes_per_second"};
2017-05-05 17:48:11 +00:00
std::vector<std::string> non_loop_metrics
2017-05-05 11:36:55 +00:00
= {"max_rows_per_second", "max_bytes_per_second", "avg_rows_per_second", "avg_bytes_per_second"};
2017-05-05 17:48:11 +00:00
if (exec_type == loop)
2017-05-05 11:36:55 +00:00
{
2017-05-05 12:41:18 +00:00
for (const std::string & metric : metrics)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
if (std::find(non_loop_metrics.begin(), non_loop_metrics.end(), metric) != non_loop_metrics.end())
2017-05-05 12:41:18 +00:00
{
throw Poco::Exception("Wrong type of metric for loop execution type (" + metric + ")", 1);
}
2017-05-05 11:36:55 +00:00
}
}
else
{
2017-05-05 12:41:18 +00:00
for (const std::string & metric : metrics)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
if (std::find(loop_metrics.begin(), loop_metrics.end(), metric) != loop_metrics.end())
2017-05-05 12:41:18 +00:00
{
throw Poco::Exception("Wrong type of metric for non-loop execution type (" + metric + ")", 1);
}
2017-05-05 11:36:55 +00:00
}
}
}
2017-05-05 17:48:11 +00:00
void runQueries(const QueriesWithIndexes & queries_with_indexes)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
for (const std::pair<Query, const size_t> & query_and_index : queries_with_indexes)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
Query query = query_and_index.first;
const size_t statistic_index = query_and_index.second;
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
size_t max_iterations = stop_criterions[statistic_index].iterations.value;
2017-05-05 11:36:55 +00:00
size_t iteration = 0;
2017-05-05 17:48:11 +00:00
statistics[statistic_index].clear();
execute(query, statistic_index);
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (exec_type == loop)
2017-05-05 11:36:55 +00:00
{
while (!gotSIGINT)
{
++iteration;
/// check stop criterions
if (max_iterations && iteration >= max_iterations)
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, iterations);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (stop_criterions[statistic_index].number_of_initialized_min
&& (stop_criterions[statistic_index].fulfilled_criterions_min
>= stop_criterions[statistic_index].number_of_initialized_min))
2017-05-05 11:36:55 +00:00
{
/// All 'min' criterions are fulfilled
break;
}
2017-05-05 17:48:11 +00:00
if (stop_criterions[statistic_index].number_of_initialized_max && stop_criterions[statistic_index].fulfilled_criterions_max)
2017-05-05 11:36:55 +00:00
{
/// Some 'max' criterions are fulfilled
break;
}
2017-05-05 17:48:11 +00:00
execute(query, statistic_index);
2017-05-05 11:36:55 +00:00
}
}
if (!gotSIGINT)
{
2017-05-05 17:48:11 +00:00
statistics[statistic_index].ready = true;
2017-05-05 11:36:55 +00:00
}
}
}
2017-05-05 17:48:11 +00:00
void execute(const Query & query, const size_t statistic_index)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
statistics[statistic_index].watch_per_query.restart();
2017-05-05 11:36:55 +00:00
2017-05-05 12:41:18 +00:00
RemoteBlockInputStream stream(connection, query, &settings, nullptr, Tables() /*, query_processing_stage*/);
2017-05-05 11:36:55 +00:00
Progress progress;
2017-05-05 17:48:11 +00:00
stream.setProgressCallback([&progress, &stream, statistic_index, this](const Progress & value) {
2017-05-05 11:36:55 +00:00
progress.incrementPiecewiseAtomically(value);
2017-05-05 17:48:11 +00:00
this->checkFulfilledCriterionsAndUpdate(progress, stream, statistic_index);
2017-05-05 11:36:55 +00:00
});
2017-05-05 12:41:18 +00:00
stream.readPrefix();
while (Block block = stream.read())
2017-05-05 11:36:55 +00:00
;
2017-05-05 12:41:18 +00:00
stream.readSuffix();
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
statistics[statistic_index].updateQueryInfo();
statistics[statistic_index].setTotalTime();
2017-05-05 11:36:55 +00:00
}
void checkFulfilledCriterionsAndUpdate(const Progress & progress,
2017-05-05 12:41:18 +00:00
RemoteBlockInputStream & stream,
2017-05-05 17:48:11 +00:00
const size_t statistic_index)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
statistics[statistic_index].add(progress.rows, progress.bytes);
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
size_t max_rows_to_read = stop_criterions[statistic_index].rows_read.value;
if (max_rows_to_read && statistics[statistic_index].rows_read >= max_rows_to_read)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, rows_read);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
size_t max_bytes_to_read = stop_criterions[statistic_index].bytes_read_uncompressed.value;
if (max_bytes_to_read && statistics[statistic_index].bytes_read >= max_bytes_to_read)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, bytes_read_uncompressed);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (UInt64 max_timeout_ms = stop_criterions[statistic_index].timeout_ms.value)
2017-05-05 11:36:55 +00:00
{
/// cast nanoseconds to ms
2017-05-05 17:48:11 +00:00
if ((statistics[statistic_index].watch.elapsed() / (1000 * 1000)) > max_timeout_ms)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, timeout_ms);
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
size_t min_time_not_changing_for_ms = stop_criterions[statistic_index].min_time_not_changing_for_ms.value;
2017-05-05 11:36:55 +00:00
if (min_time_not_changing_for_ms)
{
2017-05-05 17:48:11 +00:00
size_t min_time_did_not_change_for = statistics[statistic_index].min_time_watch.elapsed() / (1000 * 1000);
2017-05-05 11:36:55 +00:00
if (min_time_did_not_change_for >= min_time_not_changing_for_ms)
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, min_time_not_changing_for_ms);
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
size_t max_speed_not_changing_for_ms = stop_criterions[statistic_index].max_speed_not_changing_for_ms.value;
2017-05-05 11:36:55 +00:00
if (max_speed_not_changing_for_ms)
{
2017-05-05 17:48:11 +00:00
UInt64 speed_not_changing_time = statistics[statistic_index].max_rows_speed_watch.elapsed() / (1000 * 1000);
2017-05-05 11:36:55 +00:00
if (speed_not_changing_time >= max_speed_not_changing_for_ms)
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, max_speed_not_changing_for_ms);
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
size_t average_speed_not_changing_for_ms = stop_criterions[statistic_index].average_speed_not_changing_for_ms.value;
2017-05-05 11:36:55 +00:00
if (average_speed_not_changing_for_ms)
{
2017-05-05 17:48:11 +00:00
UInt64 speed_not_changing_time = statistics[statistic_index].avg_rows_speed_watch.elapsed() / (1000 * 1000);
2017-05-05 11:36:55 +00:00
if (speed_not_changing_time >= average_speed_not_changing_for_ms)
{
2017-05-05 17:48:11 +00:00
incFulfilledCriterions(statistic_index, average_speed_not_changing_for_ms);
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
if (stop_criterions[statistic_index].number_of_initialized_min
&& (stop_criterions[statistic_index].fulfilled_criterions_min >= stop_criterions[statistic_index].number_of_initialized_min))
2017-05-05 11:36:55 +00:00
{
/// All 'min' criterions are fulfilled
2017-05-05 12:41:18 +00:00
stream.cancel();
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (stop_criterions[statistic_index].number_of_initialized_max && stop_criterions[statistic_index].fulfilled_criterions_max)
2017-05-05 11:36:55 +00:00
{
/// Some 'max' criterions are fulfilled
2017-05-05 12:41:18 +00:00
stream.cancel();
2017-05-05 11:36:55 +00:00
}
2017-05-05 12:41:18 +00:00
if (interrupt_listener.check())
{
2017-05-05 11:36:55 +00:00
gotSIGINT = true;
2017-05-05 12:41:18 +00:00
stream.cancel();
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
void constructSubstitutions(AbstractConfig & substitutions_view, StringToVector & substitutions)
2017-05-05 11:36:55 +00:00
{
Keys xml_substitutions;
2017-05-05 17:48:11 +00:00
substitutions_view->keys(xml_substitutions);
2017-05-05 11:36:55 +00:00
for (size_t i = 0; i != xml_substitutions.size(); ++i)
{
2017-05-05 17:48:11 +00:00
const AbstractConfig xml_substitution(substitutions_view->createView("substitution[" + std::to_string(i) + "]"));
2017-05-05 11:36:55 +00:00
/// Property values for substitution will be stored in a vector
/// accessible by property name
std::vector<std::string> xml_values;
xml_substitution->keys("values", xml_values);
std::string name = xml_substitution->getString("name");
for (size_t j = 0; j != xml_values.size(); ++j)
{
substitutions[name].push_back(xml_substitution->getString("values.value[" + std::to_string(j) + "]"));
}
}
}
std::vector<std::string> formatQueries(const std::string & query, StringToVector substitutions)
{
std::vector<std::string> queries;
StringToVector::iterator substitutions_first = substitutions.begin();
StringToVector::iterator substitutions_last = substitutions.end();
--substitutions_last;
2017-05-05 17:48:11 +00:00
std::map<std::string, std::string> substitutions_map;
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
runThroughAllOptionsAndPush(substitutions_first, substitutions_last, query, queries, substitutions_map);
2017-05-05 11:36:55 +00:00
return queries;
}
/// Recursive method which goes through all substitution blocks in xml
/// and replaces property {names} by their values
void runThroughAllOptionsAndPush(StringToVector::iterator substitutions_left,
StringToVector::iterator substitutions_right,
const std::string & template_query,
std::vector<std::string> & queries,
2017-05-05 17:48:11 +00:00
const StringKeyValue & template_substitutions_map = StringKeyValue())
2017-05-05 11:36:55 +00:00
{
std::string name = substitutions_left->first;
std::vector<std::string> values = substitutions_left->second;
for (const std::string & value : values)
{
/// Copy query string for each unique permutation
Query query = template_query;
2017-05-05 17:48:11 +00:00
StringKeyValue substitutions_map = template_substitutions_map;
size_t substr_pos = 0;
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
while (substr_pos != std::string::npos)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
substr_pos = query.find("{" + name + "}");
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (substr_pos != std::string::npos)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
query.replace(substr_pos, 1 + name.length() + 1, value);
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
substitutions_map[name] = value;
2017-05-05 11:36:55 +00:00
/// If we've reached the end of substitution chain
if (substitutions_left == substitutions_right)
{
queries.push_back(query);
2017-05-05 17:48:11 +00:00
substitutions_maps.push_back(substitutions_map);
2017-05-05 11:36:55 +00:00
}
else
{
StringToVector::iterator next_it = substitutions_left;
++next_it;
2017-05-05 17:48:11 +00:00
runThroughAllOptionsAndPush(next_it, substitutions_right, query, queries, substitutions_map);
2017-05-05 11:36:55 +00:00
}
}
}
2017-02-24 22:02:08 +00:00
public:
2017-05-05 11:36:55 +00:00
void constructTotalInfo()
{
2017-05-05 17:48:11 +00:00
JSONString json_output;
2017-05-05 11:36:55 +00:00
std::string hostname;
char hostname_buffer[256];
if (gethostname(hostname_buffer, 256) == 0)
{
hostname = std::string(hostname_buffer);
}
2017-05-05 17:48:11 +00:00
json_output["hostname"].set(hostname);
json_output["Number of CPUs"].set(sysconf(_SC_NPROCESSORS_ONLN));
json_output["test_name"].set(test_name);
json_output["main_metric"].set(main_metric);
2017-05-05 11:36:55 +00:00
if (substitutions.size())
{
2017-05-05 17:48:11 +00:00
JSONString json_parameters(2); /// here, 2 is the size of \t padding
2017-05-05 11:36:55 +00:00
for (auto it = substitutions.begin(); it != substitutions.end(); ++it)
{
std::string parameter = it->first;
std::vector<std::string> values = it->second;
2017-05-05 17:48:11 +00:00
std::string array_string = "[";
2017-05-05 11:36:55 +00:00
for (size_t i = 0; i != values.size(); ++i)
{
2017-05-05 17:48:11 +00:00
array_string += '\"' + values[i] + '\"';
2017-05-05 11:36:55 +00:00
if (i != values.size() - 1)
{
2017-05-05 17:48:11 +00:00
array_string += ", ";
2017-05-05 11:36:55 +00:00
}
}
2017-05-05 17:48:11 +00:00
array_string += ']';
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
json_parameters[parameter].set(array_string);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
json_output["parameters"].set(json_parameters);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
std::vector<JSONString> run_infos;
for (size_t query_index = 0; query_index < queries.size(); ++query_index)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
for (size_t number_of_launch = 0; number_of_launch < statistics.size(); ++number_of_launch)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
if (!statistics[number_of_launch].ready)
2017-05-05 16:49:14 +00:00
continue;
JSONString runJSON;
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (substitutions_maps.size())
2017-05-05 11:36:55 +00:00
{
2017-05-05 16:49:14 +00:00
JSONString parameters(4);
2017-05-05 17:48:11 +00:00
for (auto it = substitutions_maps[query_index].begin(); it != substitutions_maps[query_index].end(); ++it)
2017-05-05 16:49:14 +00:00
{
parameters[it->first].set(it->second);
}
runJSON["parameters"].set(parameters);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
if (exec_type == loop)
2017-05-05 16:49:14 +00:00
{
/// in seconds
2017-05-05 17:48:11 +00:00
runJSON["min_time"].set(statistics[number_of_launch].min_time / double(1000));
2017-05-05 11:36:55 +00:00
2017-05-05 16:49:14 +00:00
JSONString quantiles(4); /// here, 4 is the size of \t padding
for (double percent = 10; percent <= 90; percent += 10)
{
2017-05-05 17:48:11 +00:00
quantiles[percent / 100].set(statistics[number_of_launch].sampler.quantileInterpolated(percent / 100.0));
2017-05-05 16:49:14 +00:00
}
2017-05-05 17:48:11 +00:00
quantiles[0.95].set(statistics[number_of_launch].sampler.quantileInterpolated(95 / 100.0));
quantiles[0.99].set(statistics[number_of_launch].sampler.quantileInterpolated(99 / 100.0));
quantiles[0.999].set(statistics[number_of_launch].sampler.quantileInterpolated(99.9 / 100.0));
quantiles[0.9999].set(statistics[number_of_launch].sampler.quantileInterpolated(99.99 / 100.0));
2017-05-05 11:36:55 +00:00
2017-05-05 16:49:14 +00:00
runJSON["quantiles"].set(quantiles);
2017-05-05 17:48:11 +00:00
runJSON["total_time"].set(statistics[number_of_launch].total_time);
runJSON["queries_per_second"].set(double(statistics[number_of_launch].queries) / statistics[number_of_launch].total_time);
runJSON["rows_per_second"].set(double(statistics[number_of_launch].rows_read) / statistics[number_of_launch].total_time);
runJSON["bytes_per_second"].set(double(statistics[number_of_launch].bytes_read) / statistics[number_of_launch].total_time);
2017-05-05 16:49:14 +00:00
}
else
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
runJSON["max_rows_per_second"].set(statistics[number_of_launch].max_rows_speed);
runJSON["max_bytes_per_second"].set(statistics[number_of_launch].max_bytes_speed);
runJSON["avg_rows_per_second"].set(statistics[number_of_launch].avg_rows_speed_value);
runJSON["avg_bytes_per_second"].set(statistics[number_of_launch].avg_bytes_speed_value);
2017-05-05 11:36:55 +00:00
}
2017-05-05 17:48:11 +00:00
run_infos.push_back(runJSON);
2017-05-05 11:36:55 +00:00
}
}
2017-01-13 18:26:51 +00:00
2017-05-05 17:48:11 +00:00
json_output["runs"].set(run_infos);
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
std::cout << std::endl << json_output << std::endl;
2017-05-05 11:36:55 +00:00
}
void minOutput(const std::string & main_metric)
{
2017-05-05 17:48:11 +00:00
for (size_t query_index = 0; query_index < queries.size(); ++query_index)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
for (size_t number_of_launch = 0; number_of_launch < times_to_run; ++number_of_launch)
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
std::cout << test_name << ", ";
2017-05-05 11:36:55 +00:00
2017-05-05 17:48:11 +00:00
if (substitutions_maps.size())
2017-05-05 11:36:55 +00:00
{
2017-05-05 17:48:11 +00:00
for (auto it = substitutions_maps[query_index].begin(); it != substitutions_maps[query_index].end(); ++it)
2017-05-05 11:36:55 +00:00
{
std::cout << it->first << " = " << it->second << ", ";
}
}
2017-05-05 17:48:11 +00:00
std::cout << "run " << number_of_launch + 1 << ": ";
2017-05-05 11:36:55 +00:00
std::cout << main_metric << " = ";
2017-05-05 17:48:11 +00:00
std::cout << statistics[number_of_launch * queries.size() + query_index].getStatisticByName(main_metric);
2017-05-05 11:36:55 +00:00
std::cout << std::endl;
}
}
}
};
2017-01-13 18:26:51 +00:00
}
2017-05-06 14:36:37 +00:00
void getFilesFromDir(FS::path && dir, std::vector<std::string> & input_files)
2017-05-05 18:32:38 +00:00
{
if (dir.extension().string() == ".xml")
std::cerr << "Warning: \"" + dir.string() + "\" is a directory, but has .xml extension" << std::endl;
2017-05-06 14:36:37 +00:00
FS::directory_iterator end;
for (FS::directory_iterator it(dir); it != end; ++it)
2017-05-05 18:32:38 +00:00
{
2017-05-06 14:36:37 +00:00
const FS::path file = (*it);
if (!FS::is_directory(file) && file.extension().string() == ".xml")
2017-05-05 18:32:38 +00:00
input_files.push_back(file.string());
}
}
2017-05-05 11:36:55 +00:00
int mainEntryClickhousePerformanceTest(int argc, char ** argv)
{
using namespace DB;
try
{
using boost::program_options::value;
using Strings = std::vector<std::string>;
boost::program_options::options_description desc("Allowed options");
2017-05-05 12:41:18 +00:00
desc.add_options()
("help", "produce help message")
2017-05-05 15:58:25 +00:00
("lite", "use lite version of output")
2017-05-05 12:41:18 +00:00
("host,h", value<std::string>()->default_value("localhost"), "")
("port", value<UInt16>()->default_value(9000), "")
("user", value<std::string>()->default_value("default"), "")
("password", value<std::string>()->default_value(""), "")
("database", value<std::string>()->default_value("default"), "")
("tag", value<Strings>(), "Run only tests with tag")
("without-tag", value<Strings>(), "Do not run tests with tag")
("name", value<Strings>(), "Run tests with specific name")
("without-name", value<Strings>(), "Do not run tests with name")
("name-regexp", value<Strings>(), "Run tests with names matching regexp")
("without-name-regexp", value<Strings>(), "Do not run tests with names matching regexp");
2017-05-05 11:36:55 +00:00
/// These options will not be displayed in --help
boost::program_options::options_description hidden("Hidden options");
hidden.add_options()("input-files", value<std::vector<std::string>>(), "");
/// But they will be legit, though. And they must be given without name
boost::program_options::positional_options_description positional;
positional.add("input-files", -1);
boost::program_options::options_description cmdline_options;
cmdline_options.add(desc).add(hidden);
boost::program_options::variables_map options;
boost::program_options::store(
boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(positional).run(), options);
boost::program_options::notify(options);
if (options.count("help"))
{
std::cout << "Usage: " << argv[0] << " [options] [test_file ...] [tests_folder]\n";
std::cout << desc << "\n";
2017-05-05 15:58:25 +00:00
return 0;
2017-05-05 11:36:55 +00:00
}
2017-05-05 18:32:38 +00:00
Strings input_files;
2017-05-05 11:36:55 +00:00
if (!options.count("input-files"))
{
2017-05-05 18:32:38 +00:00
std::cout << "Trying to find tests in current folder" << std::endl;
2017-05-06 14:36:37 +00:00
getFilesFromDir(FS::path("."), input_files);
2017-05-05 18:32:38 +00:00
if (input_files.empty())
throw Poco::Exception("Did not find any xml files", 1);
} else {
input_files = options["input-files"].as<Strings>();
2017-05-06 14:36:37 +00:00
for (const std::string filename : input_files)
2017-05-05 18:32:38 +00:00
{
2017-05-06 14:36:37 +00:00
FS::path file(filename);
2017-05-05 18:32:38 +00:00
2017-05-06 14:36:37 +00:00
if (!FS::exists(file))
throw Poco::Exception("File \"" + filename + "\" does not exist", 1);
2017-05-05 18:32:38 +00:00
2017-05-06 14:36:37 +00:00
if (FS::is_directory(file))
2017-05-05 18:32:38 +00:00
{
2017-05-06 14:36:37 +00:00
input_files.erase( std::remove(input_files.begin(), input_files.end(), filename) , input_files.end());
2017-05-05 18:32:38 +00:00
getFilesFromDir(std::move(file), input_files);
} else {
if (file.extension().string() != ".xml")
2017-05-06 14:36:37 +00:00
throw Poco::Exception("File \"" + filename + "\" does not have .xml extension", 1);
2017-05-05 18:32:38 +00:00
}
}
2017-05-05 11:36:55 +00:00
}
Strings tests_tags;
Strings skip_tags;
Strings tests_names;
Strings skip_names;
Strings name_regexp;
Strings skip_matching_regexp;
if (options.count("tag"))
{
tests_tags = options["tag"].as<Strings>();
}
if (options.count("without-tag"))
{
skip_tags = options["without-tag"].as<Strings>();
}
if (options.count("name"))
{
tests_names = options["name"].as<Strings>();
}
if (options.count("without-name"))
{
skip_names = options["without-name"].as<Strings>();
}
if (options.count("name-regexp"))
{
name_regexp = options["name-regexp"].as<Strings>();
}
if (options.count("without-name-regexp"))
{
skip_matching_regexp = options["without-name-regexp"].as<Strings>();
}
2017-05-05 12:41:18 +00:00
PerformanceTest performanceTest(
2017-05-05 11:36:55 +00:00
options["host"].as<std::string>(),
options["port"].as<UInt16>(),
options["database"].as<std::string>(),
options["user"].as<std::string>(),
options["password"].as<std::string>(),
2017-05-05 15:58:25 +00:00
options.count("lite") > 0,
2017-05-05 18:32:38 +00:00
input_files,
2017-05-05 11:36:55 +00:00
tests_tags,
skip_tags,
tests_names,
skip_names,
name_regexp,
skip_matching_regexp);
}
catch (const Exception & e)
{
std::string text = e.displayText();
std::cerr << "Code: " << e.code() << ". " << text << "\n\n";
/// Если есть стек-трейс на сервере, то не будем писать стек-трейс на клиенте.
if (std::string::npos == text.find("Stack trace"))
std::cerr << "Stack trace:\n" << e.getStackTrace().toString();
return e.code();
}
catch (const Poco::Exception & e)
{
std::cerr << "Poco::Exception: " << e.displayText() << "\n";
return ErrorCodes::POCO_EXCEPTION;
}
catch (const std::exception & e)
{
std::cerr << "std::exception: " << e.what() << "\n";
return ErrorCodes::STD_EXCEPTION;
}
catch (...)
{
std::cerr << "Unknown exception\n";
return ErrorCodes::UNKNOWN_EXCEPTION;
}
return 0;
2017-01-13 18:26:51 +00:00
}