<commit_before>#include "lithium_http_backend.hh"

#if TFB_MYSQL
  #include "lithium_mysql.hh"
#elif TFB_PGSQL
  #include "lithium_pgsql.hh"
#endif

#include "symbols.hh"
using namespace li;

template <typename B>
void escape_html_entities(B& buffer, const std::string& data)
{
    for(size_t pos = 0; pos != data.size(); ++pos) {
        switch(data[pos]) {
            case '&':  buffer << "&amp;";       break;
            case '\"': buffer << "&quot;";      break;
            case '\'': buffer << "&apos;";      break;
            case '<':  buffer << "&lt;";        break;
            case '>':  buffer << "&gt;";        break;
            default:   buffer << data[pos]; break;
        }
    }
}

void set_max_sql_connections_per_thread(int max)
{
#if TFB_MYSQL
  li::max_mysql_connections_per_thread = max;
#elif TFB_PGSQL
  li::max_pgsql_connections_per_thread = max;
#endif
}

void tune_n_sql_connections(int& nc_to_tune, std::string http_req, int port, int min, int max) {

  std::cout << std::endl << "Benchmark " << http_req << std::endl;

  auto sockets = http_benchmark_connect(512, port);

  float max_req_per_s = 0;
  int best_nconn = 2;
  for (int i = 0; i <= 7; i++)
  {
    int nc = min + (max - min) * i / 7;
    nc_to_tune = nc;

    // Warmup.
    http_benchmark(sockets, 4, 200, http_req);
    float req_per_s = http_benchmark(sockets, 4, 1000, http_req);
    std::cout << nc << " -> " << req_per_s << " req/s." << std::endl;
    if (req_per_s > max_req_per_s)
    {
      max_req_per_s = req_per_s;
      best_nconn = nc;
    }
  }

  http_benchmark_close(sockets);

  std::cout << "best: " << best_nconn << " (" << max_req_per_s << " req/s)."<< std::endl;
  nc_to_tune = best_nconn;
}

int main(int argc, char* argv[]) {

  if (argc != 3)
  {
    std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
    return 1;
  }

  int port = atoi(argv[2]);
  int nprocs = std::thread::hardware_concurrency();

#if TFB_MYSQL
  auto sql_db =
    mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
                   s::password = "<PASSWORD>", s::port = 3306, s::charset = "utf8");
  int sql_max_connection = sql_db.connect()("SELECT @@GLOBAL.max_connections;").template read<int>() - 10;
#elif TFB_PGSQL
  auto sql_db =
    pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
                   s::password = "<PASSWORD>", s::port = 5432, s::charset = "utf8");
  int sql_max_connection = atoi(sql_db.connect()("SHOW max_connections;").template read<std::string>().c_str()) - 10;
#endif

  auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
    s::id(s::auto_increment, s::primary_key) = int(),
    s::message = std::string());

  auto random_numbers = sql_orm_schema(sql_db, "World").fields(
    s::id(s::auto_increment, s::primary_key) = int(),
    s::randomNumber = int());


  int db_nconn = 128/nprocs;
  int queries_nconn = 2;
  int fortunes_nconn = 128/nprocs;
  int updates_nconn = 2;
<commit_msg>Update lithium.cc (#5575)<commit_after>#include "lithium_http_backend.hh"

#if TFB_MYSQL
  #include "lithium_mysql.hh"
#elif TFB_PGSQL
  #include "lithium_pgsql.hh"
#endif

#include "symbols.hh"
using namespace li;

template <typename B>
void escape_html_entities(B& buffer, const std::string& data)
{
    for(size_t pos = 0; pos != data.size(); ++pos) {
        switch(data[pos]) {
            case '&':  buffer << "&amp;";       break;
            case '\"': buffer << "&quot;";      break;
            case '\'': buffer << "&apos;";      break;
            case '<':  buffer << "&lt;";        break;
            case '>':  buffer << "&gt;";        break;
            default:   buffer << data[pos]; break;
        }
    }
}

void set_max_sql_connections_per_thread(int max)
{
#if TFB_MYSQL
  li::max_mysql_connections_per_thread = max;
#elif TFB_PGSQL
  li::max_pgsql_connections_per_thread = max;
#endif
}

void tune_n_sql_connections(int& nc_to_tune, std::string http_req, int port, int min, int max) {

  std::cout << std::endl << "Benchmark " << http_req << std::endl;

  auto sockets = http_benchmark_connect(512, port);

  float max_req_per_s = 0;
  int best_nconn = 2;
  for (int i = 0; i <= 7; i++)
  {
    int nc = min + (max - min) * i / 7;
    nc_to_tune = nc;

    // Warmup.
    http_benchmark(sockets, 4, 200, http_req);
    float req_per_s = http_benchmark(sockets, 4, 1000, http_req);
    std::cout << nc << " -> " << req_per_s << " req/s." << std::endl;
    if (req_per_s > max_req_per_s)
    {
      max_req_per_s = req_per_s;
      best_nconn = nc;
    }
  }

  http_benchmark_close(sockets);

  std::cout << "best: " << best_nconn << " (" << max_req_per_s << " req/s)."<< std::endl;
  nc_to_tune = best_nconn;
}

int main(int argc, char* argv[]) {

  if (argc != 3)
  {
    std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
    return 1;
  }

  int port = atoi(argv[2]);
  int nprocs = std::thread::hardware_concurrency();

#if TFB_MYSQL
  auto sql_db =
    mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
                   s::password = "<PASSWORD>", s::port = 3306, s::charset = "utf8");
  int sql_max_connection = sql_db.connect()("SELECT @@GLOBAL.max_connections;").template read<int>() - 10;
#elif TFB_PGSQL
  auto sql_db =
    pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
                   s::password = "<PASSWORD>", s::port = 5432, s::charset = "utf8");
  int sql_max_connection = atoi(sql_db.connect()("SHOW max_connections;").template read<std::string>().c_str()) - 10;
#endif

  auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
    s::id(s::auto_increment, s::primary_key) = int(),
    s::message = std::string());

  auto random_numbers = sql_orm_schema(sql_db, "World").fields(
    s::id(s::auto_increment, s::primary_key) = int(),
    s::randomNumber = int());


  int db_nconn = 128/nprocs;
  int queries_nconn = 2;
  int fortunes_nconn = 128/nprocs;
  int updates_nconn = 1;

{'fraction_non_alphanumeric': 0.17107001321003962, 'fraction_numerical': 0.015852047556142668, 'mean_word_length': 3.295744680851064, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 19, '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': '2993958', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2296, 'n_words': 480}