Add worker_threads setting

This commit is contained in:
King_DuckZ 2020-08-11 01:33:11 +01:00
parent d0a41d916a
commit 3866462ff5
7 changed files with 56 additions and 12 deletions

View file

@ -1,2 +1,3 @@
[main]
db_path=originsro.db3
worker_threads=3

View file

@ -19,6 +19,11 @@
#include "orotool_config.hpp"
#include <fstream>
#include <string>
#include <string_view>
#include <thread>
#include <algorithm>
#include <stdexcept>
#include <iostream>
namespace duck {
@ -28,6 +33,28 @@ namespace {
input >> std::noskipws;
return { std::istream_iterator<char>(input), std::istream_iterator<char>() };
}
std::string_view value_ifp (
const kamokan::IniFile& ini,
std::string_view section,
std::string_view key,
std::string_view def,
bool allow_empty
) {
const auto& map = ini.parsed();
auto it_section = map.find(section);
if (map.end() == it_section)
return def;
auto it_setting = it_section->second.find(key);
if (it_section->second.end() == it_setting)
return def;
if (not allow_empty and it_setting->second.empty())
return def;
else
return it_setting->second;
}
} //unnamed namespace
AppConfig::AppConfig() :
@ -38,15 +65,27 @@ AppConfig::AppConfig() :
AppConfig::~AppConfig() noexcept = default;
std::string_view AppConfig::db_path() const {
const auto& map = m_ini.parsed();
auto it_section = map.find("main");
if (map.end() == it_section)
return g_def_sqlite_db_name;
return value_ifp(m_ini, "main", "db_path", g_def_sqlite_db_name, false);
}
auto it_setting = it_section->second.find("db_path");
if (it_section->second.end() == it_setting)
return g_def_sqlite_db_name;
return it_setting->second;
std::size_t AppConfig::worker_threads() const {
std::string_view opt_name("worker_threads");
std::string_view val = value_ifp(m_ini, "main", opt_name, g_def_worker_threads, false);
if (val == "max") {
return std::max(3U, std::thread::hardware_concurrency()) - 1;
}
else {
try {
const std::size_t num = std::stoul(std::string(val.data(), val.size()));
const std::size_t hard_max = 4U * std::thread::hardware_concurrency();
return std::max<std::size_t>(2U, std::min(num, hard_max));
}
catch (const std::logic_error& err) {
std::cerr << "Error reading setting " << opt_name << ": " << err.what() << '\n';
return std::stoul(g_def_worker_threads);
}
}
}
} //namespace duck

View file

@ -19,6 +19,7 @@
#include "ini_file.hpp"
#include <string_view>
#include <cstddef>
namespace duck {
@ -28,6 +29,7 @@ public:
~AppConfig() noexcept;
std::string_view db_path() const;
std::size_t worker_threads() const;
private:
kamokan::IniFile m_ini;

View file

@ -22,5 +22,6 @@ namespace duck {
constexpr const char g_base_url[] = @BASE_URL@;
constexpr const char g_config_file_path[] = @CONFIG_FILE_PATH@;
constexpr const char g_def_sqlite_db_name[] = @DEF_SQLITE_DB_NAME@;
constexpr const char g_def_worker_threads[] = "2";
} //namespace duck

View file

@ -43,12 +43,11 @@ void join(roar11::ThreadPool& pool) {
} //unnamed namespace
void test(oro::Api* api, oro::OriginsDB* db) {
void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count) {
typedef TimerOroApi<&oro::Api::market_list> TimerShops;
typedef TimerOroApi<&oro::Api::items_list> TimerItems;
typedef TimerOroApi<&oro::Api::items_icons> TimerIcons;
const std::size_t thread_count = 2U; //std::min(std::max(3U, std::thread::hardware_concurrency()) - 1, 4U);
std::cout << "Running with " << thread_count << " worker threads\n";
roar11::ThreadPool pool(thread_count);
eve::Eventia worker;

View file

@ -17,6 +17,8 @@
#pragma once
#include <cstddef>
namespace oro {
class Api;
class OriginsDB;
@ -24,6 +26,6 @@ namespace oro {
namespace duck {
void test(oro::Api* api, oro::OriginsDB* db);
void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count);
} //namespace duck

View file

@ -44,7 +44,7 @@ int main(int argc, char* argv[]) {
duck::AppConfig app_conf;
oro::OriginsDB db(app_conf.db_path());
duck::test(&oro_api, &db);
duck::test(&oro_api, &db, app_conf.worker_threads());
/*
{