Browse Source

Merge pull request #5073 from oatpp/master

Add oatpp-thread tests
Mike Smith 5 years ago
parent
commit
86fa7a855b

+ 9 - 0
frameworks/C++/oatpp/README.md

@@ -16,6 +16,15 @@
 - `JSON` - [http://127.0.0.1:8000/json](http://127.0.0.1:8000/json)
 - `PLAINTEXT` - [http://127.0.0.1:8000/plaintext](http://127.0.0.1:8000/plaintext) 
 
+### oatpp-thread
+
+- [src-thread](src-thread/) - source code for oatpp test project implemented using oatpp simple API.
+- [src-thread/src/controller/MyController.hpp](src-thread/src/controller/MyController.hpp) - Controller for `JSON` endpoint.
+
+#### Test URLs
+
+- `JSON` - [http://127.0.0.1:8000/json](http://127.0.0.1:8000/json)
+
 ## Contacts
 
 - Leonid Stryzhevskyi (Creator)

+ 58 - 3
frameworks/C++/oatpp/benchmark_config.json

@@ -8,7 +8,7 @@
       "approach": "Realistic",
       "classification": "Micro",
       "database": "None",
-      "framework": "oatpp-async",
+      "framework": "oatpp",
       "language": "C++",
       "flavor": "None",
       "orm": "Raw",
@@ -16,8 +16,63 @@
       "webserver": "None",
       "os": "Linux",
       "database_os": "Linux",
-      "display_name": "oatpp",
-      "notes": "Test of Oat++ Async API",
+      "display_name": "oatpp-async",
+      "notes": "Test of Oat++ Async API.",
+      "versus": "None"
+    },
+    "async-no-mp": {
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8000,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "None",
+      "framework": "oatpp",
+      "language": "C++",
+      "flavor": "None",
+      "orm": "Raw",
+      "platform": "None",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "oatpp-async-no-mp",
+      "notes": "Test of Oat++ Async API. No memory pool.",
+      "versus": "None"
+    },
+    "thread": {
+      "json_url": "/json",
+      "port": 8000,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "None",
+      "framework": "oatpp",
+      "language": "C++",
+      "flavor": "None",
+      "orm": "Raw",
+      "platform": "None",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "oatpp-thread",
+      "notes": "Test of Oat++ Simple API.",
+      "versus": "None"
+    },
+    "thread-no-mp": {
+      "json_url": "/json",
+      "port": 8000,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "None",
+      "framework": "oatpp",
+      "language": "C++",
+      "flavor": "None",
+      "orm": "Raw",
+      "platform": "None",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "oatpp-thread-no-mp",
+      "notes": "Test of Oat++ Simple API. No memory pool.",
       "versus": "None"
     }
   }]

+ 31 - 0
frameworks/C++/oatpp/oatpp-async-no-mp.dockerfile

@@ -0,0 +1,31 @@
+FROM lganzzzo/ubuntu-cmake
+
+#---------------------------------------------------------------
+# install oatpp
+
+WORKDIR /test
+
+RUN git clone https://github.com/oatpp/oatpp
+
+WORKDIR /test/oatpp
+
+RUN git checkout 73d9523a5ac627609f8d409ce46305404cfbae0e
+
+WORKDIR /test/oatpp/build
+
+RUN cmake -DOATPP_DISABLE_ENV_OBJECT_COUNTERS=ON -DOATPP_DISABLE_POOL_ALLOCATIONS=ON -DCMAKE_BUILD_TYPE=Release -DOATPP_BUILD_TESTS=OFF ..
+RUN make install
+
+#---------------------------------------------------------------
+# build test app
+
+ADD src-async /test/src-async
+
+WORKDIR /test/src-async/build
+
+RUN cmake -DCMAKE_BUILD_TYPE=Release ..
+RUN make
+
+EXPOSE 8000 8000
+
+CMD ./oatpp-async-test

+ 31 - 0
frameworks/C++/oatpp/oatpp-thread-no-mp.dockerfile

@@ -0,0 +1,31 @@
+FROM lganzzzo/ubuntu-cmake
+
+#---------------------------------------------------------------
+# install oatpp
+
+WORKDIR /test
+
+RUN git clone https://github.com/oatpp/oatpp
+
+WORKDIR /test/oatpp
+
+RUN git checkout 73d9523a5ac627609f8d409ce46305404cfbae0e
+
+WORKDIR /test/oatpp/build
+
+RUN cmake -DOATPP_DISABLE_ENV_OBJECT_COUNTERS=ON -DOATPP_DISABLE_POOL_ALLOCATIONS=ON -DCMAKE_BUILD_TYPE=Release -DOATPP_BUILD_TESTS=OFF ..
+RUN make install
+
+#---------------------------------------------------------------
+# build test app
+
+ADD src-thread /test/src-thread
+
+WORKDIR /test/src-thread/build
+
+RUN cmake -DCMAKE_BUILD_TYPE=Release ..
+RUN make
+
+EXPOSE 8000 8000
+
+CMD ./oatpp-thread-test

+ 31 - 0
frameworks/C++/oatpp/oatpp-thread.dockerfile

@@ -0,0 +1,31 @@
+FROM lganzzzo/ubuntu-cmake
+
+#---------------------------------------------------------------
+# install oatpp
+
+WORKDIR /test
+
+RUN git clone https://github.com/oatpp/oatpp
+
+WORKDIR /test/oatpp
+
+RUN git checkout 73d9523a5ac627609f8d409ce46305404cfbae0e
+
+WORKDIR /test/oatpp/build
+
+RUN cmake -DOATPP_DISABLE_ENV_OBJECT_COUNTERS=ON -DCMAKE_BUILD_TYPE=Release -DOATPP_BUILD_TESTS=OFF ..
+RUN make install
+
+#---------------------------------------------------------------
+# build test app
+
+ADD src-thread /test/src-thread
+
+WORKDIR /test/src-thread/build
+
+RUN cmake -DCMAKE_BUILD_TYPE=Release ..
+RUN make
+
+EXPOSE 8000 8000
+
+CMD ./oatpp-thread-test

+ 3 - 10
frameworks/C++/oatpp/oatpp.dockerfile

@@ -1,11 +1,4 @@
-FROM debian:9
-
-RUN apt-get update
-
-RUN apt-get install -yqq cmake
-RUN apt-get install -yqq build-essential
-
-RUN apt-get install -yqq git
+FROM lganzzzo/ubuntu-cmake
 
 #---------------------------------------------------------------
 # install oatpp
@@ -16,11 +9,11 @@ RUN git clone https://github.com/oatpp/oatpp
 
 WORKDIR /test/oatpp
 
-RUN git checkout 68bbb14ec4bb6b67cfb5917c7c1ed9201f82d341
+RUN git checkout 73d9523a5ac627609f8d409ce46305404cfbae0e
 
 WORKDIR /test/oatpp/build
 
-RUN cmake -DCMAKE_BUILD_TYPE=Release -DOATPP_BUILD_TESTS=OFF ..
+RUN cmake -DOATPP_DISABLE_ENV_OBJECT_COUNTERS=ON -DCMAKE_BUILD_TYPE=Release -DOATPP_BUILD_TESTS=OFF ..
 RUN make install
 
 #---------------------------------------------------------------

+ 2 - 1
frameworks/C++/oatpp/src-async/README.md

@@ -1,2 +1,3 @@
 # oatpp-async-benchmark project
- 
+
+Source code of oatpp test project built with Async API.

+ 40 - 0
frameworks/C++/oatpp/src-thread/.gitignore

@@ -0,0 +1,40 @@
+# Prerequisites
+*.d
+
+# Compiled Object files
+*.slo
+*.lo
+*.o
+*.obj
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Compiled Dynamic libraries
+*.so
+*.dylib
+*.dll
+
+# Fortran module files
+*.mod
+*.smod
+
+# Compiled Static libraries
+*.lai
+*.la
+*.a
+*.lib
+
+# Executables
+*.exe
+*.out
+*.app
+
+# custom build
+build/
+
+# idea
+.idea/
+cmake-build-debug/
+*/cmake-build-debug/

+ 35 - 0
frameworks/C++/oatpp/src-thread/CMakeLists.txt

@@ -0,0 +1,35 @@
+cmake_minimum_required(VERSION 3.1)
+
+set(project_name oatpp-thread-test) ## rename your project here
+
+project(${project_name})
+
+set(CMAKE_CXX_STANDARD 11)
+
+include_directories(src)
+
+add_executable(${project_name}
+        src/AppComponent.hpp
+        src/App.cpp
+        src/controller/MyController.hpp
+        src/dto/DTOs.hpp
+        src/Utils.cpp
+        src/Utils.hpp
+)
+
+## link libs
+
+find_package(oatpp 0.19.8 REQUIRED)
+
+target_link_libraries(${project_name}
+        PUBLIC oatpp::oatpp
+        PUBLIC oatpp::oatpp-test
+)
+
+set_target_properties(${project_name} PROPERTIES
+        CXX_STANDARD 11
+        CXX_EXTENSIONS OFF
+        CXX_STANDARD_REQUIRED ON
+        LINKER_LANGUAGE CXX
+)
+

+ 3 - 0
frameworks/C++/oatpp/src-thread/README.md

@@ -0,0 +1,3 @@
+# oatpp-thread-benchmark project
+
+Source code of oatpp test project built with Simple API.

+ 53 - 0
frameworks/C++/oatpp/src-thread/src/App.cpp

@@ -0,0 +1,53 @@
+
+#include "./controller/MyController.hpp"
+#include "./AppComponent.hpp"
+
+#include "oatpp/network/server/Server.hpp"
+
+#include <iostream>
+
+void run() {
+
+  /* Register Components in scope of run() method */
+  AppComponent components;
+
+  /* Get router component */
+  OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
+
+  /* Create MyController and add all of its endpoints to router */
+  auto myController = std::make_shared<MyController>();
+  myController->addEndpointsToRouter(router);
+
+  /* Get connection handler component */
+  OATPP_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, connectionHandler);
+
+  /* Get connection provider component */
+  OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
+
+  /* Create server which takes provided TCP connections and passes them to HTTP connection handler */
+  oatpp::network::server::Server server(connectionProvider, connectionHandler);
+
+  /* Priny info about server port */
+  OATPP_LOGI("MyApp", "Server running on port %s", connectionProvider->getProperty("port").getData());
+
+  /* Run server */
+  server.run();
+
+}
+
+int main(int argc, const char * argv[]) {
+
+  oatpp::base::Environment::init();
+
+  run();
+
+  /* Print how much objects were created during app running, and what have left-probably leaked */
+  /* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
+  std::cout << "\nEnvironment:\n";
+  std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
+  std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
+
+  oatpp::base::Environment::destroy();
+
+  return 0;
+}

+ 47 - 0
frameworks/C++/oatpp/src-thread/src/AppComponent.hpp

@@ -0,0 +1,47 @@
+
+#ifndef AppComponent_hpp
+#define AppComponent_hpp
+
+#include "oatpp/web/server/HttpConnectionHandler.hpp"
+#include "oatpp/web/server/HttpRouter.hpp"
+#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
+
+#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
+
+#include "oatpp/core/macro/component.hpp"
+
+class AppComponent {
+public:
+
+  /**
+   *  Create ConnectionProvider component which listens on the port
+   */
+  OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
+    return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000);
+  }());
+
+  /**
+   *  Create Router component
+   */
+  OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
+    return oatpp::web::server::HttpRouter::createShared();
+  }());
+
+  /**
+   *  Create ConnectionHandler component which uses Router component to route requests
+   */
+  OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
+    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
+    return oatpp::web::server::HttpConnectionHandler::createShared(router);
+  }());
+
+  /**
+   *  Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
+   */
+  OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
+    return oatpp::parser::json::mapping::ObjectMapper::createShared();
+  }());
+
+};
+
+#endif /* AppComponent_hpp */

+ 29 - 0
frameworks/C++/oatpp/src-thread/src/Utils.cpp

@@ -0,0 +1,29 @@
+
+#include "Utils.hpp"
+
+#include <chrono>
+
+oatpp::String Utils::renderTime() {
+
+  static thread_local time_t lastSecond = 0;
+  static thread_local oatpp::String rederedTime;
+
+  auto time = std::chrono::system_clock::now().time_since_epoch();
+  time_t seconds = (time_t)std::chrono::duration_cast<std::chrono::seconds>(time).count();
+
+  if(seconds != lastSecond) {
+
+    lastSecond = seconds;
+
+    struct tm now;
+    gmtime_r(&seconds, &now);
+    char buffer[50];
+    auto size = std::strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T GMT", &now);
+
+    rederedTime = oatpp::String(buffer, size, true);
+
+  }
+
+  return rederedTime;
+
+}

+ 21 - 0
frameworks/C++/oatpp/src-thread/src/Utils.hpp

@@ -0,0 +1,21 @@
+
+#ifndef OATPP_ASYNC_TEST_UTILS_HPP
+#define OATPP_ASYNC_TEST_UTILS_HPP
+
+#include "oatpp/core/Types.hpp"
+
+
+class Utils {
+public:
+
+  /**
+   * Currently oatpp has no default implementation for server time rendering.
+   * Implementing custom server time rendering for test purposes.
+   * @return
+   */
+  static oatpp::String renderTime();
+
+};
+
+
+#endif //OATPP_ASYNC_TEST_UTILS_HPP

+ 47 - 0
frameworks/C++/oatpp/src-thread/src/controller/MyController.hpp

@@ -0,0 +1,47 @@
+
+#ifndef MyController_hpp
+#define MyController_hpp
+
+#include "dto/DTOs.hpp"
+
+#include "Utils.hpp"
+
+#include "oatpp/web/server/api/ApiController.hpp"
+#include "oatpp/core/macro/codegen.hpp"
+#include "oatpp/core/macro/component.hpp"
+
+/**
+ * Sample Api Controller.
+ */
+class MyController : public oatpp::web::server::api::ApiController {
+public:
+  /**
+   * Constructor with object mapper.
+   * @param objectMapper - default object mapper used to serialize/deserialize DTOs.
+   */
+  MyController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
+    : oatpp::web::server::api::ApiController(objectMapper)
+  {}
+public:
+  
+/**
+ *  Begin ENDPOINTs generation ('ApiController' codegen)
+ */
+#include OATPP_CODEGEN_BEGIN(ApiController)
+
+  ENDPOINT("GET", "/json", Json) {
+    auto dto = MessageDto::createShared();
+    dto->message = oatpp::String("Hello, World!", 13, false);
+    const auto& response = createDtoResponse(Status::CODE_200, dto);
+    response->putHeader("Date", Utils::renderTime());
+    return response;
+  }
+  
+/**
+ *  Finish ENDPOINTs generation ('ApiController' codegen)
+ */
+#include OATPP_CODEGEN_END(ApiController)
+  
+};
+
+#endif /* MyController_hpp */

+ 20 - 0
frameworks/C++/oatpp/src-thread/src/dto/DTOs.hpp

@@ -0,0 +1,20 @@
+
+#ifndef DTOs_hpp
+#define DTOs_hpp
+
+#include "oatpp/core/data/mapping/type/Object.hpp"
+#include "oatpp/core/macro/codegen.hpp"
+
+#include OATPP_CODEGEN_BEGIN(DTO)
+
+class MessageDto : public oatpp::data::mapping::type::Object {
+
+  DTO_INIT(MessageDto, Object)
+
+  DTO_FIELD(String, message);
+
+};
+
+#include OATPP_CODEGEN_END(DTO)
+
+#endif /* DTOs_hpp */