lithium.cc 7.4 KB

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