lithium.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "lithium_http_backend.hh"
  2. #if TFB_MYSQL
  3. #include "lithium_mysql.hh"
  4. #elif TFB_PGSQL
  5. #include "lithium_pgsql.hh"
  6. #endif
  7. #include "symbols.hh"
  8. using namespace li;
  9. template <typename B>
  10. void escape_html_entities(B& buffer, const std::string& data)
  11. {
  12. for(size_t pos = 0; pos != data.size(); ++pos) {
  13. switch(data[pos]) {
  14. case '&': buffer << "&amp;"; break;
  15. case '\"': buffer << "&quot;"; break;
  16. case '\'': buffer << "&apos;"; break;
  17. case '<': buffer << "&lt;"; break;
  18. case '>': buffer << "&gt;"; break;
  19. default: buffer << data[pos]; break;
  20. }
  21. }
  22. }
  23. int main(int argc, char* argv[]) {
  24. if (argc != 4)
  25. {
  26. std::cerr << "Usage: " << argv[0] << " sql_host port nprocs" << std::endl;
  27. return 1;
  28. }
  29. int port = atoi(argv[2]);
  30. int nprocs = atoi(argv[3]);
  31. #if TFB_MYSQL
  32. auto sql_db =
  33. mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  34. s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
  35. int mysql_max_connection = sql_db.connect()("SELECT @@GLOBAL.max_connections;").read<int>() * 0.75;
  36. li::max_mysql_connections_per_thread = (mysql_max_connection / nprocs);
  37. std::cout << "Using " << li::max_mysql_connections_per_thread << " connections per thread. " << nprocs << " threads." << std::endl;
  38. #elif TFB_PGSQL
  39. auto sql_db =
  40. pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  41. s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8");
  42. int pgsql_max_connection = atoi(sql_db.connect()("SHOW max_connections;").read<std::string>().c_str()) * 0.75;
  43. li::max_pgsql_connections_per_thread = (pgsql_max_connection / nprocs);
  44. std::cout << "Using " << li::max_pgsql_connections_per_thread << " connections per thread. " << nprocs << " threads." << std::endl;
  45. #endif
  46. auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
  47. s::id(s::auto_increment, s::primary_key) = int(),
  48. s::message = std::string());
  49. auto random_numbers = sql_orm_schema(sql_db, "World").fields(
  50. s::id(s::auto_increment, s::primary_key) = int(),
  51. s::randomNumber = int());
  52. http_api my_api;
  53. my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
  54. response.set_header("Content-Type", "text/plain");
  55. response.write("Hello, World!");
  56. };
  57. my_api.get("/json") = [&](http_request& request, http_response& response) {
  58. response.write_json(s::message = "Hello, World!");
  59. };
  60. my_api.get("/db") = [&](http_request& request, http_response& response) {
  61. response.write_json(random_numbers.connect(request.yield).find_one(s::id = 1234).value());
  62. };
  63. my_api.get("/queries") = [&](http_request& request, http_response& response) {
  64. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  65. int N = atoi(N_str.c_str());
  66. N = std::max(1, std::min(N, 500));
  67. auto c = random_numbers.connect(request.yield);
  68. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  69. for (int i = 0; i < N; i++)
  70. numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
  71. response.write_json(numbers);
  72. };
  73. my_api.get("/updates") = [&](http_request& request, http_response& response) {
  74. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  75. int N = atoi(N_str.c_str());
  76. N = std::max(1, std::min(N, 500));
  77. auto c = random_numbers.connect(request.yield);
  78. auto& raw_c = c.backend_connection();
  79. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  80. #if TFB_MYSQL
  81. raw_c("START TRANSACTION");
  82. #endif
  83. for (int i = 0; i < N; i++)
  84. {
  85. numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
  86. numbers[i].randomNumber = 1 + rand() % 9999;
  87. }
  88. std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
  89. #if TFB_MYSQL
  90. for (int i = 0; i < N; i++)
  91. c.update(numbers[i]);
  92. raw_c("COMMIT");
  93. #elif TFB_PGSQL
  94. raw_c.cached_statement
  95. ([N] {
  96. std::ostringstream ss;
  97. ss << "UPDATE World SET randomNumber=tmp.randomNumber FROM (VALUES ";
  98. for (int i = 0; i < N; i++)
  99. ss << "($" << i*2+1 << "::integer, $" << i*2+2 << "::integer) "<< (i == N-1 ? "": ",");
  100. ss << ") AS tmp(id, randomNumber) WHERE tmp.id = World.id";
  101. return ss.str();
  102. }, N)(numbers);
  103. #endif
  104. response.write_json(numbers);
  105. };
  106. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  107. typedef decltype(fortunes.all_fields()) fortune;
  108. std::vector<fortune> table;
  109. auto c = fortunes.connect(request.yield);
  110. c.forall([&] (auto f) { table.emplace_back(f); });
  111. table.emplace_back(0, "Additional fortune added at request time.");
  112. std::sort(table.begin(), table.end(),
  113. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  114. char b[100000];
  115. li::output_buffer ss(b, sizeof(b));
  116. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  117. for(auto& f : table)
  118. {
  119. ss << "<tr><td>" << f.id << "</td><td>";
  120. escape_html_entities(ss, f.message);
  121. ss << "</td></tr>";
  122. }
  123. ss << "</table></body></html>";
  124. response.set_header("Content-Type", "text/html; charset=utf-8");
  125. response.write(ss.to_string_view());
  126. };
  127. http_serve(my_api, port, s::nthreads = nprocs);
  128. return 0;
  129. }