Streamlined sim db schema for easier querying.
This commit is contained in:
1 parent
f9b2f413d7
commit
524e83c595
5 files changed
+177
-257
No files matched your search
+1
-1
@@ -220,7 +220,7 @@ void SimOrchestrator::run() {
|
||||
workers[i % workers.size()].assigned_runs.push_back(&me->runs[i]);
|
||||
|
||||
auto session_id =
|
||||
me->db.create_session(me->options.algo(), me->options.session_title());
|
||||
me->db.create_session(me->options.algo());
|
||||
|
||||
std::atomic<size_t> run_count = me->runs.size();
|
||||
for (auto &w : workers)
|
||||
|
||||
-3
@@ -36,9 +36,6 @@ message SimSessionOptions {
|
||||
// grouped and used as a unit for each simulation run.
|
||||
repeated string file = 2;
|
||||
|
||||
// An optional user friendly title for this session.
|
||||
string session_title = 3;
|
||||
|
||||
// The max number of cores to use for the simulation. By default all
|
||||
// cores will be used.
|
||||
uint32 n_cores = 4 ;
|
||||
|
||||
@@ -27,6 +27,7 @@ struct SimDB::PImpl {
|
||||
SQLite::Database db;
|
||||
std::mutex lock;
|
||||
std::vector<PendingLog> pending;
|
||||
std::unordered_map<RunID,size_t> log_entry_order;
|
||||
};
|
||||
|
||||
SimDB::SimDB(std::string const &loc) {
|
||||
@@ -52,26 +53,18 @@ void SimDB::destroy_db(std::string const &loc) {
|
||||
|
||||
SimDB::~SimDB() {}
|
||||
|
||||
SimDB::SessionID SimDB::create_session(std::string const &algo,
|
||||
std::string const &title) {
|
||||
SimDB::SessionID SimDB::create_session(std::string const &algo) {
|
||||
std::lock_guard<std::mutex> l(me->lock);
|
||||
|
||||
auto insert_session = "insert into session ("
|
||||
"session_run_date,"
|
||||
"algo,"
|
||||
"title"
|
||||
") values ("
|
||||
":session_run_date,"
|
||||
":algo,"
|
||||
":title"
|
||||
")";
|
||||
auto max_session_query = "select max(rowid) from run";
|
||||
|
||||
SQLite::Statement statement(me->db, insert_session);
|
||||
statement.bind(":session_run_date", datetime::date_string(datetime::now()));
|
||||
statement.bind(":algo", algo);
|
||||
statement.bind(":title", title);
|
||||
statement.exec();
|
||||
return SessionID{last_insert_rowid()};
|
||||
SQLite::Statement statement(me->db, max_session_query);
|
||||
size_t session_id=0;
|
||||
while (statement.executeStep()) {
|
||||
session_id = (long long)statement.getColumn(0);
|
||||
}
|
||||
|
||||
return SessionID{session_id, algo};
|
||||
}
|
||||
|
||||
SimDB::RunID SimDB::begin_run(SessionID sid,
|
||||
@@ -80,32 +73,83 @@ SimDB::RunID SimDB::begin_run(SessionID sid,
|
||||
|
||||
auto insert_run = "insert into run("
|
||||
"session_id,"
|
||||
"begin_time"
|
||||
"algo,"
|
||||
"begin_run_time,"
|
||||
"end_run_time,"
|
||||
"market_date,"
|
||||
"channel,"
|
||||
"base_algo_latency_ns,"
|
||||
"order_update_delay_ns,"
|
||||
"mbp_update_delay_ns,"
|
||||
"transaction_latency_ns,"
|
||||
"realized_pandl,"
|
||||
"position_pandl,"
|
||||
"param1,"
|
||||
"param2,"
|
||||
"param3,"
|
||||
"param4,"
|
||||
"param5,"
|
||||
"param6,"
|
||||
"param7,"
|
||||
"param8,"
|
||||
"param9,"
|
||||
"param10"
|
||||
") values ("
|
||||
":session_id,"
|
||||
":begin_time"
|
||||
":algo,"
|
||||
":begin_run_time,"
|
||||
":end_run_time,"
|
||||
":market_date,"
|
||||
":channel,"
|
||||
":base_algo_latency_ns,"
|
||||
":order_update_delay_ns,"
|
||||
":mbp_update_delay_ns,"
|
||||
":transaction_latency_ns,"
|
||||
":realized_pandl,"
|
||||
":position_pandl,"
|
||||
":param1,"
|
||||
":param2,"
|
||||
":param3,"
|
||||
":param4,"
|
||||
":param5,"
|
||||
":param6,"
|
||||
":param7,"
|
||||
":param8,"
|
||||
":param9,"
|
||||
":param10"
|
||||
")";
|
||||
|
||||
SQLite::Statement statement(me->db, insert_run);
|
||||
statement.bind(":session_id", sid.value());
|
||||
statement.bind(":begin_time", datetime::time_string(datetime::now()));
|
||||
statement.bind(":session_id", (long long)sid.id);
|
||||
statement.bind(":algo", sid.algo);
|
||||
statement.bind(":begin_run_time", datetime::time_string(datetime::now()));
|
||||
statement.bind(":end_run_time", "");
|
||||
statement.bind(":market_date", options.market_date());
|
||||
statement.bind(":channel", options.channels(0));
|
||||
statement.bind(":base_algo_latency_ns",
|
||||
(long long)options.base_algo_latency_ns());
|
||||
statement.bind(":order_update_delay_ns",
|
||||
(long long)options.order_update_delay_ns());
|
||||
statement.bind(":mbp_update_delay_ns",
|
||||
(long long)options.mbp_update_delay_ns());
|
||||
statement.bind(":transaction_latency_ns",
|
||||
(long long)options.transaction_latency_ns());
|
||||
statement.bind(":realized_pandl", 0);
|
||||
statement.bind(":position_pandl", 0);
|
||||
|
||||
std::map<std::string, std::string> sorted_params(
|
||||
options.parameters().begin(), options.parameters().end());
|
||||
|
||||
size_t p_idx = 1;
|
||||
for (auto const &p : sorted_params)
|
||||
statement.bind(fmt::format(":param{}", p_idx++), p.second);
|
||||
|
||||
for (auto i = p_idx; i <= 10; i++)
|
||||
statement.bind(fmt::format(":param{}", i), "");
|
||||
|
||||
statement.exec();
|
||||
|
||||
auto run_id = RunID{last_insert_rowid()};
|
||||
|
||||
// Log parameters
|
||||
_add_param(run_id, "market_date", options.market_date());
|
||||
for (auto const &c : options.channels())
|
||||
_add_param(run_id, "channel", c);
|
||||
_add_param(run_id, "base_algo_latency_ns", options.base_algo_latency_ns());
|
||||
_add_param(run_id, "order_update_delay_ns", options.order_update_delay_ns());
|
||||
_add_param(run_id, "mbp_update_delay_ns", options.mbp_update_delay_ns());
|
||||
_add_param(run_id, "transaction_latency_ns",
|
||||
options.transaction_latency_ns());
|
||||
|
||||
for (auto const &p : options.parameters()) {
|
||||
_add_param(run_id, p.first, p.second);
|
||||
}
|
||||
auto run_id = RunID{last_insert_rowid()};
|
||||
me->log_entry_order[run_id] = 1;
|
||||
return run_id;
|
||||
}
|
||||
|
||||
@@ -115,14 +159,14 @@ void SimDB::end_run(RunID run_id, double realized_pandl,
|
||||
|
||||
auto update_run = "update run "
|
||||
"set "
|
||||
"end_time=:end_time, "
|
||||
"end_run_time=:end_run_time, "
|
||||
"realized_pandl=:realized_pandl,"
|
||||
"position_pandl=:position_pandl "
|
||||
"where "
|
||||
"run_id=:run_id";
|
||||
|
||||
SQLite::Statement statement(me->db, update_run);
|
||||
statement.bind(":end_time", datetime::time_string(datetime::now()));
|
||||
statement.bind(":end_run_time", datetime::time_string(datetime::now()));
|
||||
statement.bind(":run_id", run_id.value());
|
||||
statement.bind(":realized_pandl", realized_pandl);
|
||||
statement.bind(":position_pandl", position_pandl);
|
||||
@@ -147,22 +191,24 @@ void SimDB::_log(bool flush) {
|
||||
auto now = l.ts;
|
||||
auto &entry_type = l.log_entry_type;
|
||||
auto &log_args = l.args;
|
||||
|
||||
auto &log_entry_order = me->log_entry_order[rid];
|
||||
std::ostringstream insert_log_entry;
|
||||
insert_log_entry << "insert into log("
|
||||
"run_id,"
|
||||
"log_entry_order,"
|
||||
"t_time,"
|
||||
"h_t_time,"
|
||||
"entry_type";
|
||||
for (size_t i = 0; i < log_args.size(); i++)
|
||||
insert_log_entry << ",val" << i + 1;
|
||||
insert_log_entry << ") values (:run_id,:t_time,:h_t_time,:entry_type";
|
||||
insert_log_entry << ") values (:run_id,:log_entry_order,:t_time,:h_t_time,:entry_type";
|
||||
for (size_t i = 0; i < log_args.size(); i++)
|
||||
insert_log_entry << ",:val" << i + 1;
|
||||
insert_log_entry << ")";
|
||||
|
||||
SQLite::Statement statement(me->db, insert_log_entry.str());
|
||||
statement.bind(":run_id", rid.value());
|
||||
statement.bind(":log_entry_order",(long long) log_entry_order++);
|
||||
statement.bind(":t_time", (long long)now.value());
|
||||
statement.bind(":h_t_time", to_time_string(now));
|
||||
statement.bind(":entry_type", entry_type);
|
||||
@@ -178,25 +224,6 @@ void SimDB::_log(bool flush) {
|
||||
}
|
||||
}
|
||||
|
||||
void SimDB::_add_param(RunID run_id, std::string const ¶m_name,
|
||||
std::string const ¶m_value) {
|
||||
auto insert_param = "insert into param("
|
||||
"run_id,"
|
||||
"param_name,"
|
||||
"param_value"
|
||||
") values ("
|
||||
":run_id,"
|
||||
":param_name,"
|
||||
":param_value"
|
||||
")";
|
||||
|
||||
SQLite::Statement statement(me->db, insert_param);
|
||||
statement.bind(":run_id", run_id.value());
|
||||
statement.bind(":param_name", param_name);
|
||||
statement.bind(":param_value", param_value);
|
||||
statement.exec();
|
||||
}
|
||||
|
||||
int SimDB::last_insert_rowid() {
|
||||
SQLite::Statement statement(me->db, "select last_insert_rowid()");
|
||||
statement.executeStep();
|
||||
|
||||
@@ -17,9 +17,11 @@ class SimDB {
|
||||
SimDB(std::string const &location = persistence::storage_location("sim_db"));
|
||||
~SimDB();
|
||||
|
||||
using SessionID = utils::ValueWrapper<struct SessionIDTag, int>;
|
||||
SessionID create_session(std::string const &algo,
|
||||
std::string const &title = "");
|
||||
struct SessionID {
|
||||
size_t id;
|
||||
std::string algo;
|
||||
};
|
||||
SessionID create_session(std::string const &algo);
|
||||
|
||||
using RunID = utils::ValueWrapper<struct RunIDTag, int>;
|
||||
RunID begin_run(SessionID, protos::SimRunOptions const &);
|
||||
@@ -32,15 +34,6 @@ class SimDB {
|
||||
std::string const &location = persistence::storage_location("sim_db"));
|
||||
|
||||
private:
|
||||
|
||||
template <class T>
|
||||
void _add_param(RunID rid, std::string const ¶m_name, T v) {
|
||||
_add_param(rid, param_name, std::to_string(v));
|
||||
}
|
||||
|
||||
void _add_param(RunID, std::string const ¶m_name,
|
||||
std::string const ¶m_value);
|
||||
|
||||
void _log(bool flush = false);
|
||||
int last_insert_rowid();
|
||||
struct PendingLog;
|
||||
|
||||
Reference in new issue
Block a user