Browse Source

Lithium cached-query tests and sql pipelining (#5918)

* Lithium: add test with sql pipeling.

* Lithium: updates.

* Add cached-query tests.

* Lithium: update readme and benchmark config.

* Lithium: quiet unzip.

* Lithium: upgrade libpq.

* Lithium: Re-run travis.

* Lithium: Fix pipeline monothread

* Lithium: optimize caching.
Matthieu Garrigues 5 years ago
parent
commit
ec30b70ceb

+ 1 - 1
frameworks/C++/lithium/README.md

@@ -11,7 +11,7 @@ Author: Matthieu Garrigues <[email protected]>
 * [PLAINTEXT] lithium.cc
 * [DB] lithium.cc
 * [QUERY] lithium.cc
-* [CACHED QUERY] Not implemented
+* [CACHED QUERY] lithium.cc
 * [UPDATE] lithium.cc
 * [FORTUNES] lithium.cc
 

+ 44 - 0
frameworks/C++/lithium/benchmark_config.json

@@ -9,6 +9,7 @@
         "fortune_url"    : "/fortunes",
         "update_url"     : "/updates?N=",
         "plaintext_url"  : "/plaintext",
+        "cached_query_url"    : "/cached-worlds?N=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Micro",
@@ -32,6 +33,7 @@
         "fortune_url"    : "/fortunes",
         "update_url"     : "/updates?N=",
         "plaintext_url"  : "/plaintext",
+        "cached_query_url": "/cached-worlds?N=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Micro",
@@ -47,8 +49,50 @@
         "display_name": "Lithium-postgres",
         "notes": "",
         "versus": "None"
+      },
+      "postgres-pipeline": {
+        "db_url"         : "/db",
+        "query_url"      : "/queries?N=",
+        "fortune_url"    : "/fortunes",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Micro",
+        "database": "Postgres",
+        "framework": "Lithium",
+        "language": "C++",
+        "flavor": "None",
+        "orm": "Full",
+        "platform": "None",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Lithium-postgres-pipeline",
+        "notes": "",
+        "versus": "None"
+      },
+      "postgres-pipeline-monothread": {
+        "db_url"         : "/db",
+        "query_url"      : "/queries?N=",
+        "fortune_url"    : "/fortunes",
+        "update_url"     : "/updates?N=",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Micro",
+        "database": "Postgres",
+        "framework": "Lithium",
+        "language": "C++",
+        "flavor": "None",
+        "orm": "Full",
+        "platform": "None",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Lithium-postgres-pipeline-1t",
+        "notes": "",
+        "versus": "None"
       }
 
+
     }
   ]
 }

+ 1 - 1
frameworks/C++/lithium/compile_and_start_clang.sh

@@ -1,7 +1,7 @@
 #! /bin/sh
 
 DB_FLAG=$1
-COMMIT=2ea53ab949627baeaaad77e7662da74f9a9d0a31
+COMMIT=343cb6880927eb57b6ae87b0c6b8dde29a3515b7
 
 if [ $DB_FLAG = "TFB_MYSQL" ]; then
   CXX_FLAGS="-I /usr/include/mariadb  -lmariadbclient "

+ 1 - 1
frameworks/C++/lithium/compile_and_start_gcc.sh

@@ -1,7 +1,7 @@
 #! /bin/sh
 
 DB_FLAG=$1
-COMMIT=2ea53ab949627baeaaad77e7662da74f9a9d0a31
+COMMIT=343cb6880927eb57b6ae87b0c6b8dde29a3515b7
 
 if [ $DB_FLAG = "TFB_MYSQL" ]; then
   CXX_FLAGS="-I /usr/include/mariadb  -lmariadbclient "

+ 35 - 0
frameworks/C++/lithium/compile_clang-pipeline.sh

@@ -0,0 +1,35 @@
+#! /bin/sh
+
+DB_FLAG=$1
+COMMIT=2d409878031f9c6ee7e7ef25535b8197fdd7d90c
+MONOTHREAD=$2
+
+# Remove conflicting libpq.
+rm /usr/lib/libpq.*
+
+# Compile libpq with pipelining support. 
+wget -nv https://www.postgresql.org/message-id/attachment/112272/v18-0001-libpq-batch-support.patch
+wget -nv https://github.com/postgres/postgres/archive/bab150045bd9766869f471ede88734ea0989261c.zip
+unzip -q bab150045bd9766869f471ede88734ea0989261c.zip
+cd postgres-bab150045bd9766869f471ede88734ea0989261c
+git apply ../v18-0001-libpq-batch-support.patch
+./configure --prefix=/usr CFLAGS='-O3 -march=native -flto'
+cd src/interfaces/libpq
+make all install -j4
+cd /
+
+if [ $DB_FLAG = "TFB_MYSQL" ]; then
+  echo "ERROR: Only Postgres has pipelining support for now."
+  exit 1
+  CXX_FLAGS="-I /usr/include/mariadb  -lmariadbclient "
+  wget -nv https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_mysql.hh
+elif [ $DB_FLAG = "TFB_PGSQL" ]; then
+  CXX_FLAGS="-lpthread  -L/usr/lib -lpq -I/postgres-bab150045bd9766869f471ede88734ea0989261c/src/include"
+  wget -nv https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_pgsql.hh
+fi
+
+
+wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_http_backend.hh
+
+echo "Compile server"
+clang++ -flto -DNDEBUG -D$DB_FLAG -DMONOTHREAD=$MONOTHREAD -DN_SQL_CONNECTIONS=1 -O3 -march=native -std=c++17 ./lithium_pipeline.cc $CXX_FLAGS -lpthread -lboost_context -lssl -lcrypto -o /lithium_tbf

+ 12 - 0
frameworks/C++/lithium/lithium-postgres-pipeline-monothread.dockerfile

@@ -0,0 +1,12 @@
+FROM buildpack-deps:focal
+
+RUN apt-get update -yqq
+RUN apt-get install -yqq clang libboost-dev bison flex wget libboost-context-dev
+
+COPY ./ ./
+
+RUN ./compile_clang-pipeline.sh TFB_PGSQL 1
+
+
+ENV LD_LIBRARY_PATH=/usr/lib
+CMD /lithium_tbf tfb-database 8080

+ 12 - 0
frameworks/C++/lithium/lithium-postgres-pipeline.dockerfile

@@ -0,0 +1,12 @@
+FROM buildpack-deps:focal
+
+RUN apt-get update -yqq
+RUN apt-get install -yqq clang libboost-dev bison flex wget libboost-context-dev
+
+COPY ./ ./
+
+RUN ./compile_clang-pipeline.sh TFB_PGSQL 0
+
+
+ENV LD_LIBRARY_PATH=/usr/lib
+CMD /lithium_tbf tfb-database 8080

+ 26 - 0
frameworks/C++/lithium/lithium.cc

@@ -37,6 +37,8 @@ void siege(int port) {
 }
 #endif
 
+thread_local lru_cache<int, decltype(mmm(s::id = int(), s::randomNumber = int()))> world_cache(10000);
+
 int main(int argc, char* argv[]) {
 
   if (argc != 3)
@@ -120,6 +122,30 @@ int main(int argc, char* argv[]) {
     response.write_json(numbers);
   };
 
+  my_api.get("/cached-worlds") = [&](http_request& request, http_response& response) {
+    sql_db.max_async_connections_per_thread_ = queries_nconn;
+    std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
+    int N = atoi(N_str.c_str());
+    
+    N = std::max(1, std::min(N, 500));
+    
+    auto c = random_numbers.connect(request.fiber);
+
+    if (world_cache.size() == 0)
+      c.forall([&] (const auto& number) {
+        world_cache(number.id, [&] { return metamap_clone(number); });
+      });
+
+    std::vector<decltype(random_numbers.all_fields())> numbers(N);
+    for (int i = 0; i < N; i++)
+    {
+      int id = 1 + rand() % 10000;
+      numbers[i] = world_cache(id, [&] { return *c.find_one(s::id = id); });
+    }
+
+    response.write_json(numbers);
+  };
+
   my_api.get("/updates") = [&](http_request& request, http_response& response) {
     sql_db.max_async_connections_per_thread_ = updates_nconn;
     std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");

+ 171 - 0
frameworks/C++/lithium/lithium_pipeline.cc

@@ -0,0 +1,171 @@
+#include "lithium_http_backend.hh"
+
+#if TFB_MYSQL
+  #include "lithium_mysql.hh"
+#elif TFB_PGSQL
+  #include "lithium_pgsql.hh"
+#endif
+
+#include "symbols.hh"
+using namespace li;
+
+template <typename B>
+void escape_html_entities(B& buffer, const std::string& data)
+{
+    for(size_t pos = 0; pos != data.size(); ++pos) {
+        switch(data[pos]) {
+            case '&':  buffer << "&amp;";       break;
+            case '\"': buffer << "&quot;";      break;
+            case '\'': buffer << "&apos;";      break;
+            case '<':  buffer << "&lt;";        break;
+            case '>':  buffer << "&gt;";        break;
+            default:   buffer << data[pos]; break;
+        }
+    }
+}
+
+#ifdef PROFILE_MODE
+void siege(int port) {
+  auto sockets = http_benchmark_connect(256, port);
+  http_benchmark(sockets, 1, 100, "GET /json HTTP/1.1\r\n\r\n");
+  http_benchmark(sockets, 1, 100, "GET /plaintext HTTP/1.1\r\n\r\n");
+  http_benchmark(sockets, 1, 100, "GET /db HTTP/1.1\r\n\r\n");
+  http_benchmark(sockets, 1, 100, "GET /queries?N=20 HTTP/1.1\r\n\r\n");
+  http_benchmark(sockets, 1, 100, "GET /fortunes HTTP/1.1\r\n\r\n");
+  http_benchmark(sockets, 1, 100, "GET /updates?N=20 HTTP/1.1\r\n\r\n");
+  http_benchmark_close(sockets);
+}
+#endif
+
+int main(int argc, char* argv[]) {
+
+  if (argc != 3)
+  {
+    std::cerr << "Usage: " << argv[0] << " sql_host port" << std::endl;
+    return 1;
+  }
+
+  int port = atoi(argv[2]);
+
+  int nprocs = std::thread::hardware_concurrency();
+
+#if MONOTHREAD
+  int nthreads = 1;
+#else
+  int nthreads = nprocs;
+#endif
+
+#if TFB_MYSQL
+  auto sql_db = mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
+                s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
+#elif TFB_PGSQL
+  auto sql_db = pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
+                               s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8", s::max_async_connections_per_thread = N_SQL_CONNECTIONS);
+#endif
+
+  std::cout << "Using " << sql_db.max_async_connections_per_thread_ << " sql connections x " << nthreads << " threads." << std::endl;
+  auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
+    s::id(s::auto_increment, s::primary_key) = int(),
+    s::message = std::string());
+
+  auto random_numbers = sql_orm_schema(sql_db, "World").fields(
+    s::id(s::auto_increment, s::primary_key) = int(),
+    s::randomNumber = int());
+
+
+
+  http_api my_api;
+
+  auto select_N_random_numbers = [] (auto& orm, int N) {
+    std::vector<decltype(random_numbers.all_fields())> numbers(N);
+    std::vector<decltype(orm.find_one(s::id = 1))> results;
+    
+    for (int i = 0; i < N; i++)
+      results.push_back(orm.find_one(s::id = 1 + (i*10000/N) + rand() % (10000/N)));
+    for (int i = 0; i < N; i++){
+      // println(" read result " , i);
+      numbers[i] = results[i]().value();
+    }
+    return numbers;
+  };
+
+  my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
+    response.set_header("Content-Type", "text/plain");
+    response.write("Hello, World!");
+  };
+
+  my_api.get("/json") = [&](http_request& request, http_response& response) {
+    response.write_json(s::message = "Hello, World!");
+  };
+  my_api.get("/db") = [&](http_request& request, http_response& response) {
+    response.write_json(random_numbers.connect(request.fiber).find_one(s::id = 1 + rand() % 10000)());
+  };
+
+  my_api.get("/queries") = [&](http_request& request, http_response& response) {
+    std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
+    int N = atoi(N_str.c_str());
+    
+    N = std::max(1, std::min(N, 500));
+    
+    auto c = random_numbers.connect(request.fiber);
+    response.write_json(select_N_random_numbers(c, N));
+  };
+
+  my_api.get("/updates") = [&](http_request& request, http_response& response) {
+    std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
+    int N = atoi(N_str.c_str());
+    N = std::max(1, std::min(N, 500));
+
+    auto c = random_numbers.connect(request.fiber);
+    auto numbers = select_N_random_numbers(c, N);
+ 
+    for (int i = 0; i < N; i++)
+      numbers[i].randomNumber = 1 + rand() % 10000;
+
+    c.bulk_update(numbers).flush_results();
+    response.write_json(numbers);
+  };
+
+  my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
+
+    typedef decltype(fortunes.all_fields()) fortune;
+    std::vector<fortune> table;
+
+    auto c = fortunes.connect(request.fiber);
+    c.forall([&] (const auto& f) { table.emplace_back(metamap_clone(f)); });
+    table.emplace_back(0, "Additional fortune added at request time.");
+
+    std::sort(table.begin(), table.end(),
+              [] (const fortune& a, const fortune& b) { return a.message < b.message; });
+
+    char b[100000];
+    li::output_buffer ss(b, sizeof(b));
+ 
+    ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
+    for(auto& f : table)
+    {
+      ss << "<tr><td>" << f.id << "</td><td>";
+      escape_html_entities(ss, f.message); 
+      ss << "</td></tr>";
+    }
+    ss << "</table></body></html>";
+
+    response.set_header("Content-Type", "text/html; charset=utf-8");
+    response.write(ss.to_string_view());
+  };
+
+#ifndef PROFILE_MODE
+  // Start the server for the Techempower benchmark.
+  http_serve(my_api, port, s::nthreads = nthreads);
+#else
+  std::thread server_thread([&] {
+    http_serve(my_api, port, s::nthreads = nprocs);
+  });
+  usleep(2e6);
+  siege(port);
+  li::quit_signal_catched = true;
+  server_thread.join();
+
+#endif
+  return 0;
+}