Skip to content

Commit

Permalink
feat: update devops config
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Feb 22, 2024
1 parent 6aa377e commit f5cc7ad
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 49 deletions.
3 changes: 1 addition & 2 deletions .env
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export POSTGRES_PASS=""
export IP_NODE=$(hostname)
export IP_V4_ADDRESS=$(hostname)

export SECRET_KEY_BASE=cyqpgMlaL59EKA4OQMFYT7JksQ6vTYMY
export DATABASE_URL=postgresql://zoedsoupe@localhost:5432/rinha_dev
export DATABASE_INFO="host=localhost port=5432 user=$POSTGRES_USER password=$POSTGRES_PASS dbname=rinha_dev sslmode=disable"
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Define variables
TAG=ghcr.io/cciuenf/rinha-backend-2024-q1

# Default target
all: build

# Build the Docker image
build:
docker build -t $(TAG) .

# Run the application
run:
docker compose down && docker build -t $(TAG) . && docker compose up

# Publish the Docker image
publish:
docker build -t $(TAG) . && docker push $(TAG)

# Use .PHONY to specify that these are not file names
.PHONY: build run publish
39 changes: 0 additions & 39 deletions config/init.sql

This file was deleted.

10 changes: 6 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
image: ghcr.io/cciuenf/rinha-backend-2024-q1:latest
hostname: api01
environment:
- PORT=4000
- DATABASE_URL=postgresql://postgres:postgres@db:5432/rinha
- IP_V4_ADDRESS=192.0.1.11
- IP_NODE=192.0.1.12
Expand All @@ -27,19 +28,20 @@ services:
<<: *api
hostname: api02
environment:
- PORT=4001
- DATABASE_URL=postgresql://postgres:postgres@db:5432/rinha
- IP_V4_ADDRESS=192.0.1.12
- IP_NODE=192.0.1.11
ports:
- "4000:4001"
- "4001:4001"
networks:
erlcluster:
ipv4_address: 192.0.1.12

nginx:
image: nginx:latest
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro
- ./setup/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api01
- api02
Expand All @@ -64,8 +66,8 @@ services:
ports:
- "5432:5432"
volumes:
- ./config/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./config/postgresql.conf:/etc/postgresql/postgresql.conf
- ./setup/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./setup/postgresql.conf:/etc/postgresql/postgresql.conf
command: postgres -c config_file=/etc/postgresql/postgresql.conf
deploy:
resources:
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
mkShell {
name = "rinha-go";
packages = with pkgs;
[go_1_22 nginx postgresql]
[go_1_22 nginx postgresql k6]
++ lib.optional stdenv.isLinux [inotify-tools]
++ lib.optional stdenv.isDarwin [
darwin.apple_sdk.frameworks.CoreServices
Expand Down
136 changes: 136 additions & 0 deletions load-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import http from 'k6/http';
import { sleep, check } from 'k6';
import { SharedArray } from 'k6/data';

// Helper functions for dynamic data
function randomClienteId() {
return Math.floor(Math.random() * 5) + 1;
}

function randomValorTransacao() {
return Math.floor(Math.random() * 10000) + 1;
}

function randomDescricao() {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 10; i++ ) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}

// Validation logic
function validarConsistenciaSaldoLimite(response) {
// Assume response.json('saldo') and response.json('limite') correctly extract the values
let saldo = response.json('saldo');
let limite = response.json('limite');
if (!saldo || !limite) return false; // Basic check to ensure data exists

// Convert to integers if they're not already (depending on your API response format)
saldo = parseInt(saldo, 10);
limite = parseInt(limite, 10);

// Perform the validation logic
return saldo >= -1 * limite;
}

const baseUrl = 'http://localhost:9999';

export let options = {
scenarios: {
creditos: {
executor: 'ramping-vus',
startVUs: 0,
stages: [
{ duration: '30s', target: 110 }, // simulate ramp up of traffic from 1 to 110 users over 2 minutes
{ duration: '30s', target: 110 }, // stay at 110 users for 2 minutes
],
gracefulRampDown: '60s',
exec: 'creditos', // name of the function to execute
},
debitos: {
executor: 'ramping-vus',
startVUs: 0,
stages: [
{ duration: '20s', target: 220 }, // simulate ramp up of traffic from 1 to 220 users over 2 minutes
{ duration: '20s', target: 220 }, // stay at 220 users for 2 minutes
],
gracefulRampDown: '60s',
exec: 'debitos',
},
extratos: {
executor: 'ramping-vus',
startVUs: 0,
stages: [
{ duration: '30s', target: 10 }, // simulate ramp up of traffic from 1 to 10 users over 2 minutes
{ duration: '20s', target: 10 }, // stay at 10 users for 2 minutes
],
gracefulRampDown: '60s',
exec: 'extratos',
},
},
};

function createTransactionPayload(valor, tipo, descricao) {
return JSON.stringify({
valor,
tipo,
descricao,
});
}

export function creditos() {
const clienteId = randomClienteId();
const valor = randomValorTransacao();
const descricao = randomDescricao();
const payload = createTransactionPayload(valor, 'c', descricao);
const params = {
headers: {
'Content-Type': 'application/json',
},
};

const res = http.post(`${baseUrl}/clientes/${clienteId}/transacoes`, payload, params);

check(res, {
'is status 200': (r) => r.status === 200,
'validar consistencia saldo limite': () => validarConsistenciaSaldoLimite(res),
});

sleep(1);
}

export function debitos() {
const clienteId = randomClienteId();
const valor = randomValorTransacao();
const descricao = randomDescricao();
const payload = createTransactionPayload(valor, 'd', descricao);
const params = {
headers: {
'Content-Type': 'application/json',
},
};

const res = http.post(`${baseUrl}/clientes/${clienteId}/transacoes`, payload, params);

check(res, {
'is status 200 or 422': (r) => [200, 422].includes(r.status),
'validar consistencia saldo limite': () => validarConsistenciaSaldoLimite(res),
});

sleep(1);
}

export function extratos() {
const clienteId = randomClienteId();
const res = http.get(`${baseUrl}/clientes/${clienteId}/extrato`);

check(res, {
'is status 200': (r) => r.status === 200,
'validar consistencia saldo limite': () => validarConsistenciaSaldoLimite(res),
});

sleep(1);
}

27 changes: 27 additions & 0 deletions setup/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
max_limit INTEGER NOT NULL,
balance INTEGER CHECK (balance >= -max_limit) NOT NULL
);

CREATE TABLE transactions(
id SERIAL PRIMARY KEY,
value INTEGER NOT NULL,
customer_id INTEGER REFERENCES customers(id),
type VARCHAR(1) CHECK (type IN ('c', 'd')) NOT NULL,
description VARCHAR(10) NOT NULL,
created_at TIMESTAMP DEFAULT now() NOT NULL
);

DO $$
BEGIN
INSERT INTO customers (name, max_limit, balance)
VALUES
('o barato sai caro', 1000 * 100, 0),
('zan corp ltda', 800 * 100, 0),
('les cruders', 10000 * 100, 0),
('padaria joia de cocaia', 100000 * 100, 0),
('kid mais', 5000 * 100, 0);
END;
$$;
6 changes: 5 additions & 1 deletion config/nginx.conf → setup/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
worker_processes auto;

events {
worker_connections 1000;
}

http {
access_log off;
sendfile on;
sendfile off;
error_log /dev/null;

upstream api {
keepalive 128;
server api01:4000;
server api02:4001;
}
Expand Down
7 changes: 5 additions & 2 deletions config/postgresql.conf → setup/postgresql.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
listen_addresses = '*'

# RESOURCE USAGE
max_connections = 30
max_connections = 300
shared_buffers = 35MB
# max_wal_size = 2MB


# QUERY TUNING
random_page_cost = 1.1
effective_io_concurrency = 30
effective_io_concurrency = 30

0 comments on commit f5cc7ad

Please sign in to comment.