<commit_before>#pragma once
#include <libdariadb/engines/strategy.h>
#include <libdariadb/interfaces/imeasstorage.h>
#include <libdariadb/timeutil.h>
#include <libdariadb/utils/async/thread_manager.h>
#include <algorithm>
#include <atomic>
#include <iostream>
#include <random>
#include <tuple>

#include <boost/date_time/posix_time/posix_time.hpp>

namespace dariadb_bench {
struct BenchmarkSummaryInfo {
  size_t writed;
  double write_speed;
  double read_interval_speed;
  double read_timepoint_speed;
  double read_all_time;
  double copy_to;
  double foreach_read_all_time;
  double page_repack_time;
  size_t page_repacked;
  double stat_time;
  double compaction_time;
  double full_read_timeseries;
  dariadb::STRATEGY strategy;

  BenchmarkSummaryInfo(dariadb::STRATEGY _strategy) {
    strategy = _strategy;
    writed = size_t(0);
    page_repacked = size_t(0);
    write_speed = read_interval_speed = read_timepoint_speed = read_all_time = stat_time =
        full_read_timeseries = page_repack_time = foreach_read_all_time = copy_to =
            compaction_time = 0.0;
  }

  void print() {
    std::cout << "benhcmark summary (" << strategy << ")" << std::endl;
    std::cout << "writed: " << writed << std::endl;
    std::cout << "write speed(average): " << write_speed << " per/sec" << std::endl;
    std::cout << "page repack: " << page_repack_time << " secs." << std::endl;
    std::cout << "page repacked: " << page_repacked << std::endl;
    std::cout << "read interval: " << read_interval_speed << " per/sec" << std::endl;
    std::cout << "stat: " << stat_time << " sec" << std::endl;
    std::cout << "read timepoint: " << read_timepoint_speed << " per/sec" << std::endl;
    std::cout << "read timeseries: " << full_read_timeseries << " secs." << std::endl;
	std::cout << "copy timeseries: " << copy_to << " secs." << std::endl;
    std::cout << "read all: " << read_all_time << " secs." << std::endl;
    std::cout << "foreach all: " << foreach_read_all_time << " secs." << std::endl;
    std::cout << "compaction: " << compaction_time << " secs." << std::endl;
  }
};

struct BenchmarkParams {
  size_t total_threads_count = 2;
  size_t hours_write_perid = 48;
  size_t writes_per_second = 2;
  size_t total_readers_count = 1;
  size_t id_count = 100;

  size_t id_per_thread() const { return id_count / total_threads_count; }

  size_t write_per_id_count() const {
    return writes_per_second * 60 * 60 * hours_write_perid;
  }

  uint64_t all_writes() const {
    return total_threads_count * write_per_id_count() * id_per_thread();
  }

  void print() {
    std::cout << "Benchmark params:" << std::endl;
    std::cout << "writers: " << total_threads_count << std::endl;
    std::cout << "hours: " << hours_write_perid << std::endl;
    std::cout << "write frequency: " << writes_per_second << std::endl;
    std::cout << "id count: " << id_count << std::endl;
    std::cout << "readers: " << total_readers_count << std::endl;
    std::cout << "id per thread: " << id_per_thread() << std::endl;
    std::cout << "values per time series: " << write_per_id_count() << std::endl;
    std::cout << "total writes: " << all_writes() << std::endl;
  }
};

class BenchmarkLogger : public dariadb::utils::ILogger {
public:
  std::atomic<uint64_t> _calls;
  void message(dariadb::utils::LOG_MESSAGE_KIND kind, const std::string &msg) {
    _calls += 1;
    auto ct = dariadb::timeutil::current_time();
    auto ct_str = dariadb::timeutil::to_string(ct);
    std::stringstream ss;
    ss << ct_str << " ";
    switch (kind) {
    case dariadb::utils::LOG_MESSAGE_KIND::FATAL:
      ss << "[err] " << msg << std::endl;
      break;
    case dariadb::utils::LOG_MESSAGE_KIND::INFO:
      ss << "[inf] " << msg << std::endl;
      break;
    case dariadb::utils::LOG_MESSAGE_KIND::MESSAGE:
      ss << "[dbg] " << msg << std::endl;
      break;
    }
    std::cout << ss.str();
  }
};

class BenchCallback : public dariadb::IReadCallback {
public:
  BenchCallback() {
    count = 0;
    is_end_called = false;
  }
  void apply(const dariadb::Meas &) override { count++; }
  void is_end() override {
    is_end_called = true;
    dariadb::IReadCallback::is_end();
  }
  std::mutex _locker;
  size_t count;
  bool is_end_called;
};

class BenchWriteCallback : public dariadb::IReadCallback {
public:
	BenchWriteCallback(dariadb::IMeasStorage *store) {
		count = 0;
		is_end_called = false;
		storage = store;
	}
	void apply(const dariadb::Meas &m) override {
		count++;
		dariadb::Meas nm = m;
		nm.id = dariadb::MAX_ID - m.id;
		storage->append(nm);
	}

	void is_end() override {
		is_end_called = true;
		IReadCallback::is_end();
	}
	std::atomic<size_t> count;
	bool is_end_called;
	dariadb::IMeasStorage *storage;
};

dariadb::Id get_id_from(BenchmarkParams params, dariadb::Id id) {
  return (id + 1) * params.id_per_thread() - params.id_per_thread();
}

dariadb::Id get_id_to(BenchmarkParams params, dariadb::Id id) {
  return (id + 1) * params.id_per_thread();
}

void thread_writer_rnd_stor(BenchmarkParams params, dariadb::Id id,<commit_msg>one more ensure.<commit_after>#pragma once
#include <libdariadb/engines/strategy.h>
#include <libdariadb/interfaces/imeasstorage.h>
#include <libdariadb/timeutil.h>
#include <libdariadb/utils/async/thread_manager.h>
#include <algorithm>
#include <atomic>
#include <iostream>
#include <random>
#include <tuple>

#include <boost/date_time/posix_time/posix_time.hpp>

namespace dariadb_bench {
struct BenchmarkSummaryInfo {
  size_t writed;
  double write_speed;
  double read_interval_speed;
  double read_timepoint_speed;
  double read_all_time;
  double copy_to;
  double foreach_read_all_time;
  double page_repack_time;
  size_t page_repacked;
  double stat_time;
  double compaction_time;
  double full_read_timeseries;
  dariadb::STRATEGY strategy;

  BenchmarkSummaryInfo(dariadb::STRATEGY _strategy) {
    strategy = _strategy;
    writed = size_t(0);
    page_repacked = size_t(0);
    write_speed = read_interval_speed = read_timepoint_speed = read_all_time = stat_time =
        full_read_timeseries = page_repack_time = foreach_read_all_time = copy_to =
            compaction_time = 0.0;
  }

  void print() {
    std::cout << "benhcmark summary (" << strategy << ")" << std::endl;
    std::cout << "writed: " << writed << std::endl;
    std::cout << "write speed(average): " << write_speed << " per/sec" << std::endl;
    std::cout << "page repack: " << page_repack_time << " secs." << std::endl;
    std::cout << "page repacked: " << page_repacked << std::endl;
    std::cout << "read interval: " << read_interval_speed << " per/sec" << std::endl;
    std::cout << "stat: " << stat_time << " sec" << std::endl;
    std::cout << "read timepoint: " << read_timepoint_speed << " per/sec" << std::endl;
    std::cout << "read timeseries: " << full_read_timeseries << " secs." << std::endl;
	std::cout << "copy timeseries: " << copy_to << " secs." << std::endl;
    std::cout << "read all: " << read_all_time << " secs." << std::endl;
    std::cout << "foreach all: " << foreach_read_all_time << " secs." << std::endl;
    std::cout << "compaction: " << compaction_time << " secs." << std::endl;
  }
};

struct BenchmarkParams {
  size_t total_threads_count = 2;
  size_t hours_write_perid = 48;
  size_t writes_per_second = 2;
  size_t total_readers_count = 1;
  size_t id_count = 100;

  size_t id_per_thread() const { return id_count / total_threads_count; }

  size_t write_per_id_count() const {
    return writes_per_second * 60 * 60 * hours_write_perid;
  }

  uint64_t all_writes() const {
    return total_threads_count * write_per_id_count() * id_per_thread();
  }

  void print() {
    std::cout << "Benchmark params:" << std::endl;
    std::cout << "writers: " << total_threads_count << std::endl;
    std::cout << "hours: " << hours_write_perid << std::endl;
    std::cout << "write frequency: " << writes_per_second << std::endl;
    std::cout << "id count: " << id_count << std::endl;
    std::cout << "readers: " << total_readers_count << std::endl;
    std::cout << "id per thread: " << id_per_thread() << std::endl;
    std::cout << "values per time series: " << write_per_id_count() << std::endl;
    std::cout << "total writes: " << all_writes() << std::endl;
  }
};

class BenchmarkLogger : public dariadb::utils::ILogger {
public:
  std::atomic<uint64_t> _calls;
  void message(dariadb::utils::LOG_MESSAGE_KIND kind, const std::string &msg) {
    _calls += 1;
    auto ct = dariadb::timeutil::current_time();
    auto ct_str = dariadb::timeutil::to_string(ct);
    std::stringstream ss;
    ss << ct_str << " ";
    switch (kind) {
    case dariadb::utils::LOG_MESSAGE_KIND::FATAL:
      ss << "[err] " << msg << std::endl;
      break;
    case dariadb::utils::LOG_MESSAGE_KIND::INFO:
      ss << "[inf] " << msg << std::endl;
      break;
    case dariadb::utils::LOG_MESSAGE_KIND::MESSAGE:
      ss << "[dbg] " << msg << std::endl;
      break;
    }
    std::cout << ss.str();
  }
};

class BenchCallback : public dariadb::IReadCallback {
public:
  BenchCallback() {
    count = 0;
    is_end_called = false;
  }
  void apply(const dariadb::Meas &) override { count++; }
  void is_end() override {
    is_end_called = true;
    dariadb::IReadCallback::is_end();
  }
  std::mutex _locker;
  size_t count;
  bool is_end_called;
};

class BenchWriteCallback : public dariadb::IReadCallback {
public:
	BenchWriteCallback(dariadb::IMeasStorage *store) {
		count = 0;
		is_end_called = false;
		storage = store;
	}
	void apply(const dariadb::Meas &m) override {
		count++;
		dariadb::Meas nm = m;
		nm.id = dariadb::MAX_ID - m.id;

		ENSURE(storage != nullptr);
		
		storage->append(nm);
	}

	void is_end() override {
		is_end_called = true;
		IReadCallback::is_end();
	}
	std::atomic<size_t> count;
	bool is_end_called;
	dariadb::IMeasStorage *storage;
};

dariadb::Id get_id_from(BenchmarkParams params, dariadb::Id id) {
  return (id + 1) * params.id_per_thread() - params.id_per_thread();
}

dariadb::Id get_id_to(BenchmarkParams params, dariadb::Id id) {
  return (id + 1) * params.id_per_thread();
}

void thread_writer_rnd_stor(BenchmarkParams params, dariadb::Id id,
{'fraction_non_alphanumeric': 0.15678174486082425, 'fraction_numerical': 0.0049178715451952395, 'mean_word_length': 4.211686314710405, 'pattern_counts': {'":': 0, '<': 379, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194818', 'n_tokens_mistral': 3832, 'n_tokens_neox': 3730, 'n_words': 739}