Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve C++ #435

Open
antonputra opened this issue Feb 13, 2025 · 0 comments
Open

Improve C++ #435

antonputra opened this issue Feb 13, 2025 · 0 comments

Comments

@antonputra
Copy link
Owner

antonputra commented Feb 13, 2025

Please improve the performance of the C++ application using the Dragon framework. For the second test, add a PostgreSQL database. In the second test, the application should parse the JSON request body, save it to the PostgreSQL database, and return the entire object back to the client, including the database-generated ID.

C++ app - https://github.com/antonputra/tutorials/tree/main/lessons/245/drogon-app

  1. Test
    curl -i http://localhost:8080/api/devices
HTTP/1.1 200 OK
Server: gnet
Date: Thu, 13 Feb 2025 01:21:35 GMT
Content-Length: 271
Content-Type: application/json

[{"id":0,"mac":"5F-33-CC-1F-43-82","firmware":"2.1.6"},{"id":1,"mac":"44-39-34-5E-9C-F2","firmware":"3.0.1"},{"id":2,"mac":"2B-6E-79-C7-22-1B","firmware":"1.8.9"},{"id":3,"mac":"06-0A-79-47-18-E1","firmware":"4.0.9"},{"id":4,"mac":"68-32-8F-00-B6-F4","firmware":"5.0.0"}]
  1. Test (Postgres)

curl -i http://localhost:8080/api/devices -d '{"mac": "81-6E-79-DA-5A-B2", "firmware": "4.0.2"}' -H "Content-Type: application/json"

HTTP/1.1 201 Created
Server: gnet
Date: Thu, 13 Feb 2025 01:23:00 GMT
Content-Length: 57
Content-Type: application/json

{"id":66009,"mac":"81-6E-79-DA-5A-B2","firmware":"4.0.2"}

DB Schema/migration*

  --
  -- Create application users.
  --
  CREATE USER cpp WITH PASSWORD 'devops123' SUPERUSER CREATEDB CREATEROLE LOGIN;
  CREATE USER rust WITH PASSWORD 'devops123' SUPERUSER CREATEDB CREATEROLE LOGIN;

  --
  -- Clean up idle connections.
  --
  WITH inactive_connections AS (SELECT pid, usename FROM pg_stat_activity WHERE usename = 'cpp') SELECT pg_terminate_backend(pid) FROM inactive_connections;
  WITH inactive_connections AS (SELECT pid, usename FROM pg_stat_activity WHERE usename = 'rust') SELECT pg_terminate_backend(pid) FROM inactive_connections;

  BEGIN;
  --
  -- Drop tables.
  --
  DROP TABLE IF EXISTS cpp_device;
  DROP TABLE IF EXISTS rust_device;

  --
  -- Create device tables.
  --
  CREATE TABLE "cpp_device" ("id" SERIAL PRIMARY KEY, "mac" varchar(17) NOT NULL, "firmware" varchar(10) NOT NULL);
  CREATE TABLE "rust_device" ("id" SERIAL PRIMARY KEY, "mac" varchar(17) NOT NULL, "firmware" varchar(10) NOT NULL);
  COMMIT;

  VACUUM full;

Compose for testing

services:
  postgres:
    image: postgres:17.2
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: myapp
      POSTGRES_DB: mydb
      POSTGRES_PASSWORD: devops123
    networks:
      - private

networks:
  private:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant