lithium.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. raw_c("START TRANSACTION");
  81. for (int i = 0; i < N; i++)
  82. {
  83. numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
  84. numbers[i].randomNumber = 1 + rand() % 9999;
  85. }
  86. std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
  87. #if TFB_MYSQL
  88. for (int i = 0; i < N; i++)
  89. c.update(numbers[i]);
  90. #elif TFB_PGSQL
  91. std::ostringstream ss;
  92. ss << "UPDATE World SET randomNumber=tmp.randomNumber FROM (VALUES ";
  93. for (int i = 0; i < N; i++)
  94. ss << "(" << numbers[i].id << ", " << numbers[i].randomNumber << ") "<< (i == N-1 ? "": ",");
  95. ss << ") AS tmp(id, randomNumber) WHERE tmp.id = World.id";
  96. raw_c(ss.str());
  97. #endif
  98. raw_c("COMMIT");
  99. response.write_json(numbers);
  100. };
  101. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  102. typedef decltype(fortunes.all_fields()) fortune;
  103. std::vector<fortune> table;
  104. auto c = fortunes.connect(request.yield);
  105. c.forall([&] (auto f) { table.emplace_back(f); });
  106. table.emplace_back(0, "Additional fortune added at request time.");
  107. std::sort(table.begin(), table.end(),
  108. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  109. char b[100000];
  110. li::output_buffer ss(b, sizeof(b));
  111. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  112. for(auto& f : table)
  113. {
  114. ss << "<tr><td>" << f.id << "</td><td>";
  115. escape_html_entities(ss, f.message);
  116. ss << "</td></tr>";
  117. }
  118. ss << "</table></body></html>";
  119. response.set_header("Content-Type", "text/html; charset=utf-8");
  120. response.write(ss.to_string_view());
  121. };
  122. http_serve(my_api, port, s::nthreads = nprocs);
  123. return 0;
  124. }