Skip to content

Commit a5005ac

Browse files
committed
Performance tweaks
1 parent 92a5b9f commit a5005ac

37 files changed

Lines changed: 1775 additions & 302 deletions

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
.gitignore
33
.idea
44
vendor
5-
composer.lock
65
*.md
76
.dockerignore
87
Dockerfile

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/vendor/
22
/composer.lock
33
/.phpunit.cache
4+
/.phpunit.result.cache
45
/.php-cs-fixer.cache
56
/phpstan.neon
67
/.idea/

Dockerfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN apk add --no-cache \
66
make \
77
linux-headers \
88
libstdc++ \
9+
brotli-dev \
910
libzip-dev \
1011
openssl-dev
1112

@@ -14,17 +15,23 @@ RUN docker-php-ext-install \
1415
sockets \
1516
zip
1617

17-
RUN pecl install swoole-6.0.1 && \
18+
RUN pecl channel-update pecl.php.net && \
19+
pecl install swoole-6.0.1 && \
1820
docker-php-ext-enable swoole
1921

20-
RUN pecl install redis && \
22+
RUN pecl channel-update pecl.php.net && \
23+
pecl install redis && \
2124
docker-php-ext-enable redis
2225

2326
WORKDIR /app
2427

2528
COPY composer.json composer.lock ./
2629
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
27-
RUN composer install --no-dev --optimize-autoloader
30+
RUN composer install --no-dev --optimize-autoloader \
31+
--ignore-platform-req=ext-mongodb \
32+
--ignore-platform-req=ext-memcached \
33+
--ignore-platform-req=ext-opentelemetry \
34+
--ignore-platform-req=ext-protobuf
2835

2936
COPY . .
3037

PERFORMANCE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ ab -n 100000 -c 1000 http://localhost:8080/
147147
# wrk
148148
wrk -t12 -c1000 -d30s http://localhost:8080/
149149

150+
# wrk script (env configurable)
151+
benchmarks/wrk.sh
152+
153+
# wrk2 script (env configurable)
154+
benchmarks/wrk2.sh
155+
150156
# Custom benchmark
151157
php benchmarks/http.php
152158
```

README.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,23 @@ The protocol-proxy uses the **Adapter Pattern** - similar to [utopia-php/databas
4848
<?php
4949
require 'vendor/autoload.php';
5050

51-
use Utopia\Proxy\Adapter\HTTP;
51+
use Utopia\Platform\Action;
52+
use Utopia\Proxy\Adapter\HTTP\Swoole as HTTPAdapter;
53+
use Utopia\Proxy\Server\HTTP\Swoole as HTTPServer;
54+
use Utopia\Proxy\Service\HTTP as HTTPService;
5255

53-
$adapter = new HTTP($cache, $dbPool);
56+
$adapter = new HTTPAdapter();
57+
$service = $adapter->getService() ?? new HTTPService();
5458

5559
// Required: Provide backend resolution logic
56-
$adapter->hook('resolve', function (string $hostname) {
57-
// Your resolution logic here (database, K8s, config, etc.)
58-
return $backend->getEndpoint($hostname);
59-
});
60+
$service->addAction('resolve', (new class extends Action {})
61+
->callback(function (string $hostname) use ($backend): string {
62+
return $backend->getEndpoint($hostname);
63+
}));
6064

61-
$server = new HttpServer(
65+
$adapter->setService($service);
66+
67+
$server = new HTTPServer(
6268
host: '0.0.0.0',
6369
port: 80,
6470
workers: swoole_cpu_num() * 2,
@@ -74,9 +80,9 @@ $server->start();
7480
<?php
7581
require 'vendor/autoload.php';
7682

77-
use Utopia\Proxy\Tcp\TcpServer;
83+
use Utopia\Proxy\Server\TCP\Swoole as TCPServer;
7884

79-
$server = new TcpServer(
85+
$server = new TCPServer(
8086
host: '0.0.0.0',
8187
ports: [5432, 3306], // PostgreSQL, MySQL
8288
workers: swoole_cpu_num() * 2
@@ -91,9 +97,9 @@ $server->start();
9197
<?php
9298
require 'vendor/autoload.php';
9399

94-
use Utopia\Proxy\Smtp\SmtpServer;
100+
use Utopia\Proxy\Server\SMTP\Swoole as SMTPServer;
95101

96-
$server = new SmtpServer(
102+
$server = new SMTPServer(
97103
host: '0.0.0.0',
98104
port: 25,
99105
workers: swoole_cpu_num() * 2
@@ -120,7 +126,7 @@ $config = [
120126
// Routing cache
121127
'cache_ttl' => 1, // 1 second
122128

123-
// Database connection (for cache and resolution hooks)
129+
// Database connection (for cache and resolution actions)
124130
'db_host' => 'localhost',
125131
'db_port' => 3306,
126132
'db_user' => 'appwrite',
@@ -133,6 +139,24 @@ $config = [
133139
];
134140
```
135141

142+
## ✅ Testing
143+
144+
```bash
145+
composer test
146+
```
147+
148+
Integration tests (Docker Compose):
149+
150+
```bash
151+
composer test:integration
152+
```
153+
154+
Coverage (requires Xdebug or PCOV):
155+
156+
```bash
157+
vendor/bin/phpunit --coverage-text
158+
```
159+
136160
## 🎨 Architecture
137161

138162
The protocol-proxy follows the **Adapter Pattern** used throughout utopia-php libraries (Database, Messaging, Storage), providing a clean and extensible architecture for protocol-specific implementations.

benchmarks/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Benchmarks
2+
3+
This folder contains high-load benchmark helpers for HTTP and TCP proxies.
4+
5+
## Quick start (HTTP)
6+
7+
Run the PHP benchmark:
8+
```bash
9+
php benchmarks/http.php
10+
```
11+
12+
Run wrk:
13+
```bash
14+
benchmarks/wrk.sh
15+
```
16+
17+
Run wrk2 (fixed rate):
18+
```bash
19+
benchmarks/wrk2.sh
20+
```
21+
22+
## Quick start (TCP)
23+
24+
Run the TCP benchmark:
25+
```bash
26+
php benchmarks/tcp.php
27+
```
28+
29+
## Presets (HTTP)
30+
31+
Max throughput, burst:
32+
```bash
33+
WRK_THREADS=16 WRK_CONNECTIONS=5000 WRK_DURATION=30s WRK_URL=http://127.0.0.1:8080/ benchmarks/wrk.sh
34+
```
35+
36+
Fixed rate (wrk2):
37+
```bash
38+
WRK2_THREADS=16 WRK2_CONNECTIONS=5000 WRK2_DURATION=30s WRK2_RATE=200000 WRK2_URL=http://127.0.0.1:8080/ benchmarks/wrk2.sh
39+
```
40+
41+
PHP benchmark, moderate:
42+
```bash
43+
BENCH_CONCURRENCY=500 BENCH_REQUESTS=50000 php benchmarks/http.php
44+
```
45+
46+
## Presets (TCP)
47+
48+
Connection rate only:
49+
```bash
50+
BENCH_PROTOCOL=mysql BENCH_PORT=15433 BENCH_PAYLOAD_BYTES=0 BENCH_CONCURRENCY=500 BENCH_CONNECTIONS=50000 php benchmarks/tcp.php
51+
```
52+
53+
Throughput heavy (payload enabled):
54+
```bash
55+
BENCH_PROTOCOL=mysql BENCH_PORT=15433 BENCH_PAYLOAD_BYTES=65536 BENCH_TARGET_BYTES=17179869184 BENCH_CONCURRENCY=2000 php benchmarks/tcp.php
56+
```
57+
58+
## Environment variables
59+
60+
HTTP PHP benchmark (`benchmarks/http.php`):
61+
- `BENCH_HOST` (default `localhost`)
62+
- `BENCH_PORT` (default `8080`)
63+
- `BENCH_CONCURRENCY` (default `max(2000, cpu*500)`)
64+
- `BENCH_REQUESTS` (default `max(1000000, concurrency*500)`)
65+
- `BENCH_TIMEOUT` (default `10`)
66+
- `BENCH_KEEP_ALIVE` (default `true`)
67+
- `BENCH_SAMPLE_TARGET` (default `200000`)
68+
- `BENCH_SAMPLE_EVERY` (optional override)
69+
70+
TCP PHP benchmark (`benchmarks/tcp.php`):
71+
- `BENCH_HOST` (default `localhost`)
72+
- `BENCH_PORT` (default `5432`)
73+
- `BENCH_PROTOCOL` (`postgres` or `mysql`, default based on port)
74+
- `BENCH_CONCURRENCY` (default `max(2000, cpu*500)`)
75+
- `BENCH_CONNECTIONS` (default derived from payload/target)
76+
- `BENCH_PAYLOAD_BYTES` (default `65536`)
77+
- `BENCH_TARGET_BYTES` (default `8GB`)
78+
- `BENCH_TIMEOUT` (default `10`)
79+
- `BENCH_SAMPLE_TARGET` (default `200000`)
80+
- `BENCH_SAMPLE_EVERY` (optional override)
81+
82+
wrk (`benchmarks/wrk.sh`):
83+
- `WRK_THREADS` (default `cpu`)
84+
- `WRK_CONNECTIONS` (default `1000`)
85+
- `WRK_DURATION` (default `30s`)
86+
- `WRK_URL` (default `http://127.0.0.1:8080/`)
87+
- `WRK_EXTRA` (extra flags)
88+
89+
wrk2 (`benchmarks/wrk2.sh`):
90+
- `WRK2_THREADS` (default `cpu`)
91+
- `WRK2_CONNECTIONS` (default `1000`)
92+
- `WRK2_DURATION` (default `30s`)
93+
- `WRK2_RATE` (default `50000`)
94+
- `WRK2_URL` (default `http://127.0.0.1:8080/`)
95+
- `WRK2_EXTRA` (extra flags)
96+
97+
## Notes
98+
99+
- For realistic max numbers, run on a tuned Linux host (see `PERFORMANCE.md`).
100+
- Running in Docker on macOS will be bottlenecked by the VM and host networking.

0 commit comments

Comments
 (0)