2017-08-24 21:23:33 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <gflags/gflags.h>
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2017-08-30 23:17:43 +08:00
|
|
|
#include "common.hpp"
|
2017-08-24 21:23:33 +08:00
|
|
|
#include "communication/bolt/client.hpp"
|
2017-08-30 23:17:43 +08:00
|
|
|
#include "communication/bolt/v1/decoder/decoded_value.hpp"
|
2017-08-24 21:23:33 +08:00
|
|
|
#include "io/network/network_endpoint.hpp"
|
|
|
|
#include "io/network/socket.hpp"
|
|
|
|
#include "threading/sync/spinlock.hpp"
|
|
|
|
#include "utils/algorithm.hpp"
|
|
|
|
#include "utils/timer.hpp"
|
|
|
|
|
|
|
|
using SocketT = io::network::Socket;
|
|
|
|
using EndpointT = io::network::NetworkEndpoint;
|
|
|
|
using ClientT = communication::bolt::Client<SocketT>;
|
2017-08-30 23:17:43 +08:00
|
|
|
using communication::bolt::DecodedValue;
|
2017-08-24 21:23:33 +08:00
|
|
|
|
|
|
|
DEFINE_string(address, "127.0.0.1", "Server address");
|
|
|
|
DEFINE_string(port, "7687", "Server port");
|
2017-08-25 17:08:45 +08:00
|
|
|
DEFINE_int32(num_workers, 1, "Number of workers");
|
2017-08-24 21:23:33 +08:00
|
|
|
DEFINE_string(output, "", "Output file");
|
|
|
|
DEFINE_string(username, "", "Username for the database");
|
|
|
|
DEFINE_string(password, "", "Password for the database");
|
|
|
|
|
2017-08-25 17:08:45 +08:00
|
|
|
const int MAX_RETRIES = 1000;
|
2017-08-24 21:23:33 +08:00
|
|
|
|
|
|
|
void PrintJsonMetadata(
|
|
|
|
std::ostream &os,
|
2017-08-30 23:17:43 +08:00
|
|
|
const std::vector<std::map<std::string, DecodedValue>> &metadata) {
|
2017-08-24 21:23:33 +08:00
|
|
|
os << "[";
|
|
|
|
PrintIterable(os, metadata, ", ", [](auto &stream, const auto &item) {
|
|
|
|
PrintJsonDecodedValue(stream, item);
|
|
|
|
});
|
|
|
|
os << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintSummary(
|
|
|
|
std::ostream &os, double duration,
|
2017-08-30 23:17:43 +08:00
|
|
|
const std::vector<std::map<std::string, DecodedValue>> &metadata) {
|
2017-08-24 21:23:33 +08:00
|
|
|
os << "{\"wall_time\": " << duration << ", "
|
|
|
|
<< "\"metadatas\": ";
|
|
|
|
PrintJsonMetadata(os, metadata);
|
|
|
|
os << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
|
google::InitGoogleLogging(argv[0]);
|
|
|
|
|
|
|
|
std::string query;
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
|
2017-08-25 17:08:45 +08:00
|
|
|
SpinLock spinlock;
|
2017-08-24 21:23:33 +08:00
|
|
|
uint64_t last = 0;
|
|
|
|
std::vector<std::string> queries;
|
2017-08-30 23:17:43 +08:00
|
|
|
std::vector<std::map<std::string, DecodedValue>> metadata;
|
2017-08-24 21:23:33 +08:00
|
|
|
|
|
|
|
while (std::getline(std::cin, query)) {
|
|
|
|
queries.push_back(query);
|
|
|
|
}
|
|
|
|
metadata.resize(queries.size());
|
|
|
|
|
|
|
|
utils::Timer timer;
|
|
|
|
|
|
|
|
for (int i = 0; i < FLAGS_num_workers; ++i) {
|
|
|
|
threads.push_back(std::thread([&]() {
|
|
|
|
SocketT socket;
|
|
|
|
EndpointT endpoint;
|
|
|
|
|
|
|
|
try {
|
|
|
|
endpoint = EndpointT(FLAGS_address, FLAGS_port);
|
|
|
|
} catch (const io::network::NetworkEndpointException &e) {
|
2017-08-30 23:17:43 +08:00
|
|
|
LOG(FATAL) << "Invalid address or port: " << FLAGS_address << ":"
|
|
|
|
<< FLAGS_port;
|
2017-08-24 21:23:33 +08:00
|
|
|
}
|
|
|
|
if (!socket.Connect(endpoint)) {
|
2017-08-30 23:17:43 +08:00
|
|
|
LOG(FATAL) << "Could not connect to: " << FLAGS_address << ":"
|
|
|
|
<< FLAGS_port;
|
2017-08-24 21:23:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientT client(std::move(socket), FLAGS_username, FLAGS_password);
|
|
|
|
|
|
|
|
std::string str;
|
|
|
|
while (true) {
|
2017-08-25 17:08:45 +08:00
|
|
|
uint64_t pos;
|
2017-08-24 21:23:33 +08:00
|
|
|
{
|
2017-08-25 17:08:45 +08:00
|
|
|
std::lock_guard<SpinLock> lock(spinlock);
|
2017-08-24 21:23:33 +08:00
|
|
|
if (last == queries.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos = last++;
|
|
|
|
str = queries[pos];
|
|
|
|
}
|
2017-08-30 23:17:43 +08:00
|
|
|
try {
|
|
|
|
metadata[pos] =
|
|
|
|
ExecuteNTimesTillSuccess(client, str, MAX_RETRIES).metadata;
|
|
|
|
} catch (const communication::bolt::ClientQueryException &e) {
|
|
|
|
LOG(FATAL) << "Could not execute query '" << str << "' "
|
|
|
|
<< MAX_RETRIES << "times";
|
2017-08-24 21:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
client.Close();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < FLAGS_num_workers; ++i) {
|
|
|
|
threads[i].join();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto elapsed = timer.Elapsed();
|
|
|
|
double duration = elapsed.count();
|
|
|
|
|
|
|
|
if (FLAGS_output != "") {
|
|
|
|
std::ofstream ofile;
|
|
|
|
ofile.open(FLAGS_output);
|
|
|
|
PrintSummary(ofile, duration, metadata);
|
|
|
|
} else {
|
|
|
|
PrintSummary(std::cout, duration, metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|