I have an API written in C++, I'm using Xcode IDE for development, and using Silicon Framework as the web framework and ODB as the ORM to connect to MySQL (read and write) and SQLite (read only) databases. I compile the API using CMake and GNU compiler to deploy it to a Debian or Ubuntu VM. My problem is there is a big performance difference between running my API on Xcode and running it on the VM and I'm unable to minimize the difference.
When I'm running on Xcode I'm using MacBook Pro 13 inch, Quad-Core Core i5 processor, 16GB RAM and 512GB SSD, with Apple clang version 11.0.3.
When running on VM I'm using a Google Compute Instance (GCE) with 2vCPU, 7.5GB RAM and 50GB standard persistent disk, and I'm running using docker engine, one container for C++ API and another one for MySQL. And these specs are more than enough for the API to run as intended.
I'm using curl to calculate the execution time, always on the same device to eliminate any delay related to connection, and all the tests were done for the same request, and same data, and the time is the average for 10 consecutive requests.
All the tests on Debian or Ubuntu were done after using CMake for compilation, all the linked libraries are static, except mysqlclient which is linked dynamically, and using:
CMAKE_CXX_FLAGS="-w -O3 -flto -frtti -fno-common -fstrict-aliasing -fexceptions"
C++14 is used for compilation in both Xcode and CMake.
Here is a list of tests and results:
- On MacBook using Xcode, and XAMPP MySQL: 5.2 seconds.
- On MacBook using Docker, Debian 9 container, GNU 6.3 compiler, ODB 2.4, and MySQL 5.7 container: 15.1 seconds.
- On GCE (Google Compute Engine) without using docker, using Debian 9, GNU 6.3 compiler, ODB 2.4 and MySQL server: 11.4 seconds.
- On GCE using docker, using Debian 9, GNU 6.3 compiler, ODB 2.4, and MySQL 5.7 container: 13.1 seconds.
- On GCE using docker, using Ubuntu 20, GNU 9.3 compiler, ODB 2.5, and MySQL 5.7 container: 11.2 seconds.
I also tried using SSD disk for GCE, but this didn't affect the results. And Changing the compiler from GNU to Clang caused worse performance.
My ideal case is to keep using Docker to run. But even without using docker, the execution time when I compile using CMake is twice as it is when I'm using Xcode.
Any idea why this difference in performance exists? can it be minimized? It would be great if I had a starting point to research and test.