-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpdo_pgsql.php
More file actions
executable file
·69 lines (60 loc) · 2.11 KB
/
Copy pathpdo_pgsql.php
File metadata and controls
executable file
·69 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* This example demonstrates how to create and use a PostgreSQL connection pool in Swoole.
*
* We define a pool with a maximum of 100 connections (note: the default pool size is 64).
* The script then performs 1,000 iterations, and in each iteration:
* - Acquires a connection from the pool.
* - Executes a simulated slow query that takes approximately 1 second.
* - Returns the connection back to the pool.
*
* Thanks to connection pooling and coroutine concurrency, the script completes
* 1,000 sequential 1-second queries in just over 10 seconds (actual time may vary
* depending on your hardware and runtime environment).
*
* You can use following command to run this script:
* docker compose exec -t client bash -c "./pool/database-pool/pdo_pgsql.php"
*
* You can run following command to see how much time it takes to run the script:
* docker compose exec -t client bash -c "time ./pool/database-pool/pdo_pgsql.php"
*/
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use function Swoole\Coroutine\go;
use function Swoole\Coroutine\run;
run(function (): void {
$config = (new PDOConfig())
->withDriver('pgsql')
->withHost('postgresql')
->withPort(5432)
->withUsername('username')
->withPassword('password')
->withDbName('test')
;
$pool = new PDOPool($config, 100);
for ($n = 1000; $n--;) {
go(function () use ($pool): void {
/** @var PDO $pdo */
$pdo = $pool->get();
$stmt = $pdo->prepare('SELECT pg_sleep(1)');
if ($stmt === false) {
echo 'Failed to prepare the statement.', PHP_EOL;
return;
}
$stmt->execute(); // The query finishes in 1 second.
$stmt->fetchAll();
// The result array returned is:
// [
// [
// 'pg_sleep' => '',
// 0 => '',
// ]
// ];
$stmt = null;
$pool->put($pdo);
});
}
});
echo 'Done', PHP_EOL;