Browse Source

Update lithium. Tune the number of SQL connections. (#5427)

* Update lithium. Tune the number of SQL connections.

* Fix compilation.

* Lithium: 9999 -> 10000
Matthieu Garrigues 5 years ago
parent
commit
a46d4f22fd

+ 1 - 1
frameworks/C++/lithium/lithium-mysql.dockerfile

@@ -5,7 +5,7 @@ RUN apt install -yqq libboost-dev libmariadb-dev wget  libboost-context-dev g++-
 
 
 COPY ./ ./
 COPY ./ ./
 
 
-ENV COMMIT=db7562dd6a58a66b895c4fca53d10bf4b7463536
+ENV COMMIT=d40cbe5aca32aa237dbce14b892e273b9d5a6908
 
 
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_mysql.hh
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_mysql.hh
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_http_backend.hh
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_http_backend.hh

+ 1 - 1
frameworks/C++/lithium/lithium-postgres.dockerfile

@@ -5,7 +5,7 @@ RUN apt install -yqq g++-9 libboost-dev postgresql-server-dev-all libpq-dev wget
 
 
 COPY ./ ./
 COPY ./ ./
 
 
-ENV COMMIT=db7562dd6a58a66b895c4fca53d10bf4b7463536
+ENV COMMIT=d40cbe5aca32aa237dbce14b892e273b9d5a6908
 
 
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_pgsql.hh
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_pgsql.hh
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_http_backend.hh
 RUN wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_http_backend.hh

+ 40 - 27
frameworks/C++/lithium/lithium.cc

@@ -24,6 +24,15 @@ void escape_html_entities(B& buffer, const std::string& data)
     }
     }
 }
 }
 
 
+void set_max_sql_connections_per_thread(int max)
+{
+#if TFB_MYSQL
+  li::max_mysql_connections_per_thread = max;
+#elif TFB_PGSQL
+  li::max_pgsql_connections_per_thread = max;
+#endif
+}
+
 int main(int argc, char* argv[]) {
 int main(int argc, char* argv[]) {
 
 
   if (argc != 3)
   if (argc != 3)
@@ -71,10 +80,12 @@ int main(int argc, char* argv[]) {
     response.write_json(s::message = "Hello, World!");
     response.write_json(s::message = "Hello, World!");
   };
   };
   my_api.get("/db") = [&](http_request& request, http_response& response) {
   my_api.get("/db") = [&](http_request& request, http_response& response) {
+    set_max_sql_connections_per_thread(1024 / nprocs);
     response.write_json(random_numbers.connect(request.yield).find_one(s::id = 1234).value());
     response.write_json(random_numbers.connect(request.yield).find_one(s::id = 1234).value());
   };
   };
 
 
   my_api.get("/queries") = [&](http_request& request, http_response& response) {
   my_api.get("/queries") = [&](http_request& request, http_response& response) {
+    set_max_sql_connections_per_thread(2);
     std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
     std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
     int N = atoi(N_str.c_str());
     int N = atoi(N_str.c_str());
     
     
@@ -83,55 +94,57 @@ int main(int argc, char* argv[]) {
     auto c = random_numbers.connect(request.yield);
     auto c = random_numbers.connect(request.yield);
     std::vector<decltype(random_numbers.all_fields())> numbers(N);
     std::vector<decltype(random_numbers.all_fields())> numbers(N);
     for (int i = 0; i < N; i++)
     for (int i = 0; i < N; i++)
-      numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
+      numbers[i] = c.find_one(s::id = 1 + rand() % 10000).value();
 
 
     response.write_json(numbers);
     response.write_json(numbers);
   };
   };
 
 
   my_api.get("/updates") = [&](http_request& request, http_response& response) {
   my_api.get("/updates") = [&](http_request& request, http_response& response) {
+    set_max_sql_connections_per_thread(2);
     std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
     std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
     int N = atoi(N_str.c_str());
     int N = atoi(N_str.c_str());
     N = std::max(1, std::min(N, 500));
     N = std::max(1, std::min(N, 500));
+    std::vector<decltype(random_numbers.all_fields())> numbers(N);
 
 
-     
-    auto c = random_numbers.connect(request.yield);
-    auto& raw_c = c.backend_connection();
+    {     
+      auto c = random_numbers.connect(request.yield);
+      auto& raw_c = c.backend_connection();
 
 
-    std::vector<decltype(random_numbers.all_fields())> numbers(N);
-    
+      
 #if TFB_MYSQL
 #if TFB_MYSQL
-    raw_c("START TRANSACTION");
+      raw_c("START TRANSACTION");
 #endif
 #endif
-    for (int i = 0; i < N; i++)
-    {
-      numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
-      numbers[i].randomNumber = 1 + rand() % 9999;
-    }
+      for (int i = 0; i < N; i++)
+      {
+        numbers[i] = c.find_one(s::id = 1 + rand() % 10000).value();
+        numbers[i].randomNumber = 1 + rand() % 10000;
+      }
 
 
-    std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
+      std::sort(numbers.begin(), numbers.end(), [] (auto a, auto b) { return a.id < b.id; });
 
 
 #if TFB_MYSQL
 #if TFB_MYSQL
-    for (int i = 0; i < N; i++)
-      c.update(numbers[i]);
-    raw_c("COMMIT");
+      for (int i = 0; i < N; i++)
+        c.update(numbers[i]);
+      raw_c("COMMIT");
 #elif TFB_PGSQL
 #elif TFB_PGSQL
-    raw_c.cached_statement
-      ([N] {
-         std::ostringstream ss;
-         ss << "UPDATE World SET randomNumber=tmp.randomNumber FROM (VALUES ";
-         for (int i = 0; i < N; i++)
-           ss << "($" << i*2+1 << "::integer, $" << i*2+2 << "::integer) "<< (i == N-1 ? "": ",");
-         ss << ") AS tmp(id, randomNumber) WHERE tmp.id = World.id";
-         return ss.str();
-       }, N)(numbers);
-    
+      raw_c.cached_statement
+        ([N] {
+          std::ostringstream ss;
+          ss << "UPDATE World SET randomNumber=tmp.randomNumber FROM (VALUES ";
+          for (int i = 0; i < N; i++)
+            ss << "($" << i*2+1 << "::integer, $" << i*2+2 << "::integer) "<< (i == N-1 ? "": ",");
+          ss << ") AS tmp(id, randomNumber) WHERE tmp.id = World.id";
+          return ss.str();
+        }, N)(numbers);
+      
 #endif
 #endif
-    
+    }
 
 
     response.write_json(numbers);
     response.write_json(numbers);
   };
   };
 
 
   my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
   my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
+    set_max_sql_connections_per_thread(1024 / nprocs);
 
 
     typedef decltype(fortunes.all_fields()) fortune;
     typedef decltype(fortunes.all_fields()) fortune;
     std::vector<fortune> table;
     std::vector<fortune> table;