lithium.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "lithium_http_server.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_view& data)
  11. {
  12. size_t pos = 0;
  13. auto search_for_special = [&] () {
  14. size_t start = pos;
  15. size_t end = pos;
  16. for(;pos != data.size(); ++pos) {
  17. char c = data[pos];
  18. if (c > '>' || (c != '&' && c != '\"' && c != '\'' && c != '<' && c == '>'))
  19. end = pos + 1;
  20. else break;
  21. }
  22. if (start != end)
  23. buffer << std::string_view(data.data() + start, end - start);
  24. };
  25. for(; pos != data.size(); ++pos) {
  26. search_for_special();
  27. if (pos >= data.size()) return;
  28. switch(data[pos]) {
  29. case '&': buffer << "&amp;"; break;
  30. case '\"': buffer << "&quot;"; break;
  31. case '\'': buffer << "&apos;"; break;
  32. case '<': buffer << "&lt;"; break;
  33. case '>': buffer << "&gt;"; break;
  34. default: buffer << data[pos]; break;
  35. }
  36. }
  37. }
  38. #ifdef PROFILE_MODE
  39. void siege(int port) {
  40. auto sockets = http_benchmark_connect(512, port);
  41. http_benchmark(sockets, 2, 1000, "GET /json HTTP/1.1\r\n\r\n");
  42. http_benchmark(sockets, 2, 1000, "GET /plaintext HTTP/1.1\r\n\r\n");
  43. http_benchmark(sockets, 2, 1000, "GET /db HTTP/1.1\r\n\r\n");
  44. http_benchmark(sockets, 2, 1000, "GET /queries?N=20 HTTP/1.1\r\n\r\n");
  45. http_benchmark(sockets, 2, 1000, "GET /fortunes HTTP/1.1\r\n\r\n");
  46. http_benchmark(sockets, 2, 1000, "GET /updates?N=20 HTTP/1.1\r\n\r\n");
  47. http_benchmark(sockets, 2, 1000, "GET /cached-world?N=100 HTTP/1.1\r\n\r\n");
  48. http_benchmark_close(sockets);
  49. }
  50. #endif
  51. template <typename T>
  52. struct cache {
  53. void insert(T o) {
  54. if (buffer.size() <= o.id) buffer.resize(o.id+1);
  55. buffer[o.id] = o;
  56. }
  57. const T& get(int id) const {
  58. return buffer[id];
  59. }
  60. std::vector<T> buffer;
  61. };
  62. cache<decltype(mmm(s::id = int(), s::randomNumber = int()))> world_cache;
  63. int main(int argc, char* argv[]) {
  64. if (argc != 3)
  65. {
  66. std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
  67. return 1;
  68. }
  69. int port = atoi(argv[2]);
  70. int nprocs = std::thread::hardware_concurrency();
  71. #if MONOTHREAD
  72. int nthreads = 1;
  73. #else
  74. int nthreads = nprocs;
  75. #endif
  76. #if TFB_MYSQL
  77. auto sql_db = mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  78. s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
  79. #elif TFB_PGSQL
  80. auto sql_db = pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  81. s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8");
  82. #endif
  83. auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
  84. s::id(s::auto_increment, s::primary_key) = int(),
  85. s::message = std::string());
  86. auto random_numbers = sql_orm_schema(sql_db, "World").fields(
  87. s::id(s::auto_increment, s::primary_key) = int(),
  88. s::randomNumber = int());
  89. #ifndef N_SQL_CONNECTIONS
  90. #if TFB_MYSQL
  91. int db_nconn = 5;
  92. int queries_nconn = 4;
  93. int fortunes_nconn = 5;
  94. int updates_nconn = 2;
  95. #elif TFB_PGSQL
  96. int db_nconn = 5;
  97. int queries_nconn = 3;
  98. int fortunes_nconn = 7;
  99. int updates_nconn = 3;
  100. #endif
  101. #else
  102. int db_nconn = N_SQL_CONNECTIONS;
  103. int queries_nconn = N_SQL_CONNECTIONS;
  104. int fortunes_nconn = N_SQL_CONNECTIONS;
  105. int updates_nconn = N_SQL_CONNECTIONS;
  106. #endif
  107. http_api my_api;
  108. my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
  109. response.set_header("Content-Type", "text/plain");
  110. response.write("Hello, World!");
  111. };
  112. my_api.get("/json") = [&](http_request& request, http_response& response) {
  113. response.write_json(s::message = "Hello, World!");
  114. };
  115. my_api.get("/db") = [&](http_request& request, http_response& response) {
  116. sql_db.max_async_connections_per_thread_ = db_nconn;
  117. response.write_json(*random_numbers.connect(request.fiber).find_one(s::id = 1 + rand() % 10000));
  118. };
  119. my_api.get("/queries") = [&](http_request& request, http_response& response) {
  120. sql_db.max_async_connections_per_thread_ = queries_nconn;
  121. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  122. int N = atoi(N_str.c_str());
  123. N = std::max(1, std::min(N, 500));
  124. auto c = random_numbers.connect(request.fiber);
  125. response.write_json_generator(N, [&] { return *c.find_one(s::id = 1 + rand() % 10000); });
  126. };
  127. random_numbers.connect().forall([&] (const auto& number) {
  128. world_cache.insert(metamap_clone(number));
  129. });
  130. my_api.get("/cached-worlds") = [&](http_request& request, http_response& response) {
  131. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  132. int N = atoi(N_str.c_str());
  133. response.write_json_generator(std::max(1, std::min(N, 500)),
  134. [&] { return world_cache.get(1 + rand() % 10000); });
  135. };
  136. my_api.get("/updates") = [&](http_request& request, http_response& response) {
  137. sql_db.max_async_connections_per_thread_ = updates_nconn;
  138. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  139. int N = atoi(N_str.c_str());
  140. N = std::max(1, std::min(N, 500));
  141. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  142. {
  143. auto c = random_numbers.connect(request.fiber);
  144. auto& raw_c = c.backend_connection();
  145. #if TFB_MYSQL
  146. raw_c("START TRANSACTION");
  147. #endif
  148. for (int i = 0; i < N; i++)
  149. {
  150. numbers[i] = *c.find_one(s::id = 1 + rand() % 10000);
  151. numbers[i].randomNumber = 1 + rand() % 10000;
  152. }
  153. std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
  154. c.bulk_update(numbers);
  155. #if TFB_MYSQL
  156. raw_c("COMMIT");
  157. #endif
  158. }
  159. response.write_json(numbers);
  160. };
  161. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  162. sql_db.max_async_connections_per_thread_ = fortunes_nconn;
  163. typedef decltype(fortunes.all_fields()) fortune;
  164. std::vector<fortune> table;
  165. {
  166. auto c = fortunes.connect(request.fiber);
  167. c.forall([&] (const auto& f) { table.emplace_back(metamap_clone(f)); });
  168. }
  169. table.emplace_back(0, "Additional fortune added at request time.");
  170. std::sort(table.begin(), table.end(),
  171. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  172. li::growing_output_buffer ss;
  173. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  174. for(auto& f : table)
  175. {
  176. ss << "<tr><td>" << f.id << "</td><td>";
  177. escape_html_entities(ss, f.message);
  178. ss << "</td></tr>";
  179. }
  180. ss << "</table></body></html>";
  181. response.set_header("Content-Type", "text/html; charset=utf-8");
  182. response.write(ss.to_string_view());
  183. };
  184. #ifndef PROFILE_MODE
  185. // Start the server for the Techempower benchmark.
  186. http_serve(my_api, port, s::nthreads = nthreads);
  187. #else
  188. std::thread server_thread([&] {
  189. http_serve(my_api, port, s::nthreads = nprocs);
  190. });
  191. usleep(2e6);
  192. siege(port);
  193. li::quit_signal_catched = true;
  194. server_thread.join();
  195. #endif
  196. return 0;
  197. }