static void DATABASE_serialize_json(benchmark::State& state) { auto qd = getExampleQueryData(state.range_x(), state.range_y()); while (state.KeepRunning()) { std::string content; serializeQueryDataJSON(qd, content); } }
Status Query::addNewResults(const QueryData& current_qd, DiffResults& dr, bool calculate_diff, DBHandleRef db) { // Get the rows from the last run of this query name. QueryData previous_qd; auto status = getPreviousQueryResults(previous_qd); if (!status.ok()) { return status; } // Calculate the differential between previous and current query results. if (calculate_diff) { dr = diff(previous_qd, current_qd); } if (previous_qd.size() == 0 || dr.added.size() != 0 || dr.removed.size() != 0) { // Replace the "previous" query data with the current. std::string json; status = serializeQueryDataJSON(current_qd, json); if (!status.ok()) { return status; } status = db->Put(kQueries, name_, json); if (!status.ok()) { return status; } } return Status(0, "OK"); }
Status Query::addNewResults(const QueryData& current_qd, DiffResults& dr, bool calculate_diff, DBHandleRef db) { // Get the rows from the last run of this query name. QueryData previous_qd; auto status = getPreviousQueryResults(previous_qd); // Sanitize all non-ASCII characters from the query data values. QueryData escaped_current_qd; escapeQueryData(current_qd, escaped_current_qd); // Calculate the differential between previous and current query results. if (calculate_diff) { dr = diff(previous_qd, escaped_current_qd); } // Replace the "previous" query data with the current. std::string json; status = serializeQueryDataJSON(escaped_current_qd, json); if (!status.ok()) { return status; } status = db->Put(kQueries, name_, json); if (!status.ok()) { return status; } return Status(0, "OK"); }
TEST_F(ResultsTests, test_serialize_query_data_json) { auto results = getSerializedQueryDataJSON(); std::string json; auto s = serializeQueryDataJSON(results.second, json); EXPECT_TRUE(s.ok()); EXPECT_EQ(s.toString(), "OK"); EXPECT_EQ(results.first, json); }
void TablePlugin::setCache(size_t step, size_t interval, const QueryData& results) { // Serialize QueryData and save to database. std::string content; if (!FLAGS_disable_caching && serializeQueryDataJSON(results, content)) { last_cached_ = step; last_interval_ = interval; setDatabaseValue(kQueries, "cache." + getName(), content); } }
static void DATABASE_store_large(benchmark::State& state) { // Serialize the example result set into a string. std::string content; auto qd = getExampleQueryData(20, 100); serializeQueryDataJSON(qd, content); while (state.KeepRunning()) { setDatabaseValue(kPersistentSettings, "benchmark", content); } // All benchmarks will share a single database handle. deleteDatabaseValue(kPersistentSettings, "benchmark"); }
static void DATABASE_store_append(benchmark::State& state) { // Serialize the example result set into a string. std::string content; auto qd = getExampleQueryData(20, 100); serializeQueryDataJSON(qd, content); size_t k = 0; while (state.KeepRunning()) { setDatabaseValue(kPersistentSettings, "key" + std::to_string(k), content); deleteDatabaseValue(kPersistentSettings, "key" + std::to_string(k)); k++; } // All benchmarks will share a single database handle. for (size_t i = 0; i < k; ++i) { // deleteDatabaseValue(kPersistentSettings, "key" + std::to_string(i)); } }
Status Query::addNewResults(QueryData current_qd, const uint64_t current_epoch, uint64_t& counter, DiffResults& dr, bool calculate_diff) const { // The current results are 'fresh' when not calculating a differential. bool fresh_results = !calculate_diff; bool new_query = false; if (!isQueryNameInDatabase()) { // This is the first encounter of the scheduled query. fresh_results = true; LOG(INFO) << "Storing initial results for new scheduled query: " << name_; saveQuery(name_, query_); } else if (getPreviousEpoch() != current_epoch) { fresh_results = true; LOG(INFO) << "New Epoch " << current_epoch << " for scheduled query " << name_; } else if (isNewQuery()) { // This query is 'new' in that the previous results may be invalid. new_query = true; LOG(INFO) << "Scheduled query has been updated: " + name_; saveQuery(name_, query_); } // Use a 'target' avoid copying the query data when serializing and saving. // If a differential is requested and needed the target remains the original // query data, otherwise the content is moved to the differential's added set. const auto* target_gd = ¤t_qd; bool update_db = true; if (!fresh_results && calculate_diff) { // Get the rows from the last run of this query name. QueryDataSet previous_qd; auto status = getPreviousQueryResults(previous_qd); if (!status.ok()) { return status; } // Calculate the differential between previous and current query results. dr = diff(previous_qd, current_qd); update_db = (!dr.added.empty() || !dr.removed.empty()); } else { dr.added = std::move(current_qd); target_gd = &dr.added; } counter = getQueryCounter(fresh_results || new_query); auto status = setDatabaseValue(kQueries, name_ + "counter", std::to_string(counter)); if (!status.ok()) { return status; } if (update_db) { // Replace the "previous" query data with the current. std::string json; status = serializeQueryDataJSON(*target_gd, json); if (!status.ok()) { return status; } status = setDatabaseValue(kQueries, name_, json); if (!status.ok()) { return status; } status = setDatabaseValue( kQueries, name_ + "epoch", std::to_string(current_epoch)); if (!status.ok()) { return status; } } return Status(0, "OK"); }