lithium.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifdef PROFILE_MODE
  24. void siege(int port) {
  25. auto sockets = http_benchmark_connect(512, port);
  26. http_benchmark(sockets, 2, 1000, "GET /json HTTP/1.1\r\n\r\n");
  27. http_benchmark(sockets, 2, 1000, "GET /plaintext HTTP/1.1\r\n\r\n");
  28. http_benchmark(sockets, 2, 1000, "GET /db HTTP/1.1\r\n\r\n");
  29. http_benchmark(sockets, 2, 1000, "GET /queries?N=20 HTTP/1.1\r\n\r\n");
  30. http_benchmark(sockets, 2, 1000, "GET /fortunes HTTP/1.1\r\n\r\n");
  31. http_benchmark(sockets, 2, 1000, "GET /updates?N=20 HTTP/1.1\r\n\r\n");
  32. http_benchmark_close(sockets);
  33. }
  34. #endif
  35. int main(int argc, char* argv[]) {
  36. if (argc != 3)
  37. {
  38. std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
  39. return 1;
  40. }
  41. int port = atoi(argv[2]);
  42. int nprocs = std::thread::hardware_concurrency();
  43. #if MONOTHREAD
  44. int nthreads = 1;
  45. #else
  46. int nthreads = nprocs;
  47. #endif
  48. #if TFB_MYSQL
  49. auto sql_db = mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  50. s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
  51. #elif TFB_PGSQL
  52. auto sql_db = pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  53. s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8");
  54. #endif
  55. auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
  56. s::id(s::auto_increment, s::primary_key) = int(),
  57. s::message = std::string());
  58. auto random_numbers = sql_orm_schema(sql_db, "World").fields(
  59. s::id(s::auto_increment, s::primary_key) = int(),
  60. s::randomNumber = int());
  61. #ifndef N_SQL_CONNECTIONS
  62. #if TFB_MYSQL
  63. int db_nconn = 5;
  64. int queries_nconn = 4;
  65. int fortunes_nconn = 5;
  66. int updates_nconn = 2;
  67. #elif TFB_PGSQL
  68. int db_nconn = 5;
  69. int queries_nconn = 3;
  70. int fortunes_nconn = 7;
  71. int updates_nconn = 3;
  72. #endif
  73. #else
  74. int db_nconn = N_SQL_CONNECTIONS;
  75. int queries_nconn = N_SQL_CONNECTIONS;
  76. int fortunes_nconn = N_SQL_CONNECTIONS;
  77. int updates_nconn = N_SQL_CONNECTIONS;
  78. #endif
  79. http_api my_api;
  80. my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
  81. response.set_header("Content-Type", "text/plain");
  82. response.write("Hello, World!");
  83. };
  84. my_api.get("/json") = [&](http_request& request, http_response& response) {
  85. response.write_json(s::message = "Hello, World!");
  86. };
  87. my_api.get("/db") = [&](http_request& request, http_response& response) {
  88. sql_db.max_async_connections_per_thread_ = db_nconn;
  89. response.write_json(*random_numbers.connect(request.fiber).find_one(s::id = 1 + rand() % 10000));
  90. };
  91. my_api.get("/queries") = [&](http_request& request, http_response& response) {
  92. sql_db.max_async_connections_per_thread_ = queries_nconn;
  93. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  94. int N = atoi(N_str.c_str());
  95. N = std::max(1, std::min(N, 500));
  96. auto c = random_numbers.connect(request.fiber);
  97. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  98. for (int i = 0; i < N; i++)
  99. numbers[i] = *c.find_one(s::id = 1 + rand() % 10000);
  100. response.write_json(numbers);
  101. };
  102. my_api.get("/updates") = [&](http_request& request, http_response& response) {
  103. sql_db.max_async_connections_per_thread_ = updates_nconn;
  104. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  105. int N = atoi(N_str.c_str());
  106. N = std::max(1, std::min(N, 500));
  107. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  108. {
  109. auto c = random_numbers.connect(request.fiber);
  110. auto& raw_c = c.backend_connection();
  111. #if TFB_MYSQL
  112. raw_c("START TRANSACTION");
  113. #endif
  114. for (int i = 0; i < N; i++)
  115. {
  116. numbers[i] = *c.find_one(s::id = 1 + rand() % 10000);
  117. numbers[i].randomNumber = 1 + rand() % 10000;
  118. }
  119. std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
  120. c.bulk_update(numbers);
  121. #if TFB_MYSQL
  122. raw_c("COMMIT");
  123. #endif
  124. }
  125. response.write_json(numbers);
  126. };
  127. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  128. sql_db.max_async_connections_per_thread_ = fortunes_nconn;
  129. typedef decltype(fortunes.all_fields()) fortune;
  130. std::vector<fortune> table;
  131. auto c = fortunes.connect(request.fiber);
  132. c.forall([&] (const auto& f) { table.emplace_back(metamap_clone(f)); });
  133. table.emplace_back(0, "Additional fortune added at request time.");
  134. std::sort(table.begin(), table.end(),
  135. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  136. char b[100000];
  137. li::output_buffer ss(b, sizeof(b));
  138. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  139. for(auto& f : table)
  140. {
  141. ss << "<tr><td>" << f.id << "</td><td>";
  142. escape_html_entities(ss, f.message);
  143. ss << "</td></tr>";
  144. }
  145. ss << "</table></body></html>";
  146. response.set_header("Content-Type", "text/html; charset=utf-8");
  147. response.write(ss.to_string_view());
  148. };
  149. #ifndef PROFILE_MODE
  150. // Start the server for the Techempower benchmark.
  151. http_serve(my_api, port, s::nthreads = nthreads);
  152. #else
  153. std::thread server_thread([&] {
  154. http_serve(my_api, port, s::nthreads = nprocs);
  155. });
  156. usleep(2e6);
  157. siege(port);
  158. li::quit_signal_catched = true;
  159. server_thread.join();
  160. #endif
  161. return 0;
  162. }