Browse Source

Lithium: sql optimizations for non-pipeline mode + PGO for pipeline mode. (#5924)

* Lithium: sql optimizations for non-pipeline mode.

* Lithium: Fix profiling.

* Lithium: fix readme.

* Lithium: speedup docker builds.
Matthieu Garrigues 5 years ago
parent
commit
e3c5277d8a

+ 9 - 4
frameworks/C++/lithium/README.md

@@ -1,7 +1,7 @@
-# Lithium Benchmarking Test
+# [Lithium](https://github.com/matt-42/lithium) Benchmarking Test
 
-This test benchmarks the `lithium::http_backend`. It is a modern C++17
-asynchronous web server based on epoll.
+This test benchmarks the [Lithium](https://github.com/matt-42/lithium) framework. It is a modern C++17
+asynchronous web server based on epoll and coroutines.
 
 Author: Matthieu Garrigues <[email protected]>
 
@@ -34,7 +34,7 @@ http://localhost:8080/db
 
 ### QUERY
 
-http://localhost:8080/query?N=
+http://localhost:8080/queries?N=
 
 ### UPDATE
 
@@ -43,3 +43,8 @@ http://localhost:8080/update?N=
 ### FORTUNES
 
 http://localhost:8080/fortunes
+
+### CACHED QUERY
+
+http://localhost:8080/cached-world?N=
+

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

@@ -1,13 +1,13 @@
 #! /bin/sh
 
 DB_FLAG=$1
-COMMIT=343cb6880927eb57b6ae87b0c6b8dde29a3515b7
+COMMIT=36e7b46fc3d191ba9d56ef9adf00b59c058fe4d8
 
 if [ $DB_FLAG = "TFB_MYSQL" ]; then
   CXX_FLAGS="-I /usr/include/mariadb  -lmariadbclient "
   wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_mysql.hh
 elif [ $DB_FLAG = "TFB_PGSQL" ]; then
-  CXX_FLAGS="-I/usr/include/postgresql -I /usr/include/postgresql/12/server -lpthread -lpq"
+  CXX_FLAGS="-lpthread  -L/usr/lib -lpq -I/postgres-bab150045bd9766869f471ede88734ea0989261c/src/include"
   wget https://raw.githubusercontent.com/matt-42/lithium/$COMMIT/single_headers/lithium_pgsql.hh
 fi
 

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

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

+ 6 - 16
frameworks/C++/lithium/compile_clang-pipeline.sh

@@ -4,20 +4,6 @@ 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
@@ -31,5 +17,9 @@ 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
+clang++ -fprofile-instr-generate=./profile.prof -flto -DPROFILE_MODE -DN_SQL_CONNECTIONS=1  -DMONOTHREAD=$MONOTHREAD -DNDEBUG -D$DB_FLAG -O3 -march=native -std=c++17 ./lithium_pipeline.cc $CXX_FLAGS -lpthread -lboost_context -lssl -lcrypto -o /lithium_tbf
+/lithium_tbf tfb-database 8081
+llvm-profdata-10 merge -output=./profile.pgo  ./profile.prof
+clang++ -fprofile-instr-use=./profile.pgo -flto -DNDEBUG -D$DB_FLAG -DN_SQL_CONNECTIONS=1  -DMONOTHREAD=$MONOTHREAD -O3 -march=native -std=c++17 ./lithium_pipeline.cc $CXX_FLAGS -lpthread -lboost_context -lssl -lcrypto -o /lithium_tbf
+
+/lithium_tbf tfb-database 8080

+ 16 - 0
frameworks/C++/lithium/compile_libpq.sh

@@ -0,0 +1,16 @@
+#! /bin/sh
+
+# Compile libpq
+wget -nv https://github.com/postgres/postgres/archive/bab150045bd9766869f471ede88734ea0989261c.zip
+
+unzip -q bab150045bd9766869f471ede88734ea0989261c.zip
+cd postgres-bab150045bd9766869f471ede88734ea0989261c
+if [ "$1" = "batchmode" ]; then
+  wget -nv https://www.postgresql.org/message-id/attachment/112272/v18-0001-libpq-batch-support.patch
+  git apply ./v18-0001-libpq-batch-support.patch
+fi
+
+./configure --prefix=/usr CFLAGS='-O3 -march=native -flto'
+cd src/interfaces/libpq
+make all install -j4
+cd /

+ 8 - 4
frameworks/C++/lithium/lithium-postgres-pipeline-monothread.dockerfile

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

+ 5 - 5
frameworks/C++/lithium/lithium-postgres-pipeline.dockerfile

@@ -1,12 +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
+RUN apt-get install -yqq clang libboost-context-dev libboost-dev wget
+RUN apt-get install -yqq bison flex
 
 COPY ./ ./
 
-RUN ./compile_clang-pipeline.sh TFB_PGSQL 0
-
-
+RUN ./compile_libpq.sh batchmode
 ENV LD_LIBRARY_PATH=/usr/lib
-CMD /lithium_tbf tfb-database 8080
+
+CMD ./compile_clang-pipeline.sh TFB_PGSQL 0

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

@@ -1,8 +1,12 @@
 FROM buildpack-deps:focal
 
 RUN apt-get update -yqq
-RUN apt-get install -yqq clang libboost-dev postgresql-server-dev-all libpq-dev wget libboost-context-dev
+RUN apt-get install -yqq clang libboost-context-dev libboost-dev wget
+RUN apt-get install -yqq bison flex
 
 COPY ./ ./
 
+RUN ./compile_libpq.sh
+ENV LD_LIBRARY_PATH=/usr/lib
+
 CMD ./compile_and_start_clang.sh TFB_PGSQL

+ 21 - 15
frameworks/C++/lithium/lithium.cc

@@ -114,10 +114,12 @@ int main(int argc, char* argv[]) {
     
     N = std::max(1, std::min(N, 500));
     
-    auto c = random_numbers.connect(request.fiber);
     std::vector<decltype(random_numbers.all_fields())> numbers(N);
-    for (int i = 0; i < N; i++)
-      numbers[i] = *c.find_one(s::id = 1 + rand() % 10000);
+    {
+      auto c = random_numbers.connect(request.fiber);
+      for (int i = 0; i < N; i++)
+        numbers[i] = *c.find_one(s::id = 1 + rand() % 10000);
+    }
 
     response.write_json(numbers);
   };
@@ -129,18 +131,20 @@ int main(int argc, char* argv[]) {
     
     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); });
+      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); });
+        });
+
+      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);
@@ -185,8 +189,10 @@ int main(int argc, char* argv[]) {
     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)); });
+    {
+      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(),

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

@@ -1,7 +1,8 @@
 FROM buildpack-deps:focal
 
 RUN apt-get update -yqq
-RUN apt-get install -yqq libboost-dev libmariadb-dev wget  libboost-context-dev clang
+RUN apt-get install -yqq clang libboost-context-dev libboost-dev wget
+RUN apt-get install -yqq libmariadb-dev
 
 COPY ./ ./
 

+ 1 - 1
frameworks/C++/lithium/lithium_pipeline.cc

@@ -32,7 +32,7 @@ void siege(int port) {
   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(sockets, 1, 100, "GET /updates?N=20 HTTP/1.1\r\n\r\n");
   http_benchmark_close(sockets);
 }
 #endif