Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions frameworks/PHP/workerman/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Workerman\Timer;

class Date
{
public $date = null;
public function __construct()
{
$this->date = gmdate('D, d M Y H:i:s').' GMT';
Timer::add(1, function() {
$this->date = gmdate('D, d M Y H:i:s').' GMT';
});
}
}
82 changes: 82 additions & 0 deletions frameworks/PHP/workerman/Mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

class Mysql
{

protected PDO $pdo;
protected PDOStatement $world;
protected PDOStatement $fortune;
protected PDOStatement $update;

public function __construct()
{
$this->pdo = new PDO(
'mysql:host=tfb-database;dbname=hello_world',
'benchmarkdbuser',
'benchmarkdbpass',
[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]
);
$this->world = $this->pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$this->fortune = $this->pdo->prepare('SELECT id,message FROM Fortune');
$this->update = $this->pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
}

function db(): array
{
$this->world->execute([mt_rand(1, 10000)]);
return $this->world->fetch();
}

function query($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$arr = [];
while ($query_count--) {
$this->world->execute([mt_rand(1, 10000)]);
$arr[] = $this->world->fetch();
}
return $arr;
}

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$arr = [];
while ($query_count--) {
$id = mt_rand(1, 10000);
$this->world->execute([$id]);
$item = $this->world->fetch();
$this->update->execute(
[$item['randomNumber'] = mt_rand(1, 10000), $id]
);
$arr[] = $item;
}
return $arr;
}

function fortune(): string
{
$this->fortune->execute();
$arr = $this->fortune->fetchAll(PDO::FETCH_KEY_PAIR);
$arr[0] = 'Additional fortune added at request time.';
asort($arr);
$html = '';
foreach ($arr as $id => $message) {
$message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
$html .= "<tr><td>$id</td><td>$message</td></tr>";
}
return "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>";
}

}
91 changes: 91 additions & 0 deletions frameworks/PHP/workerman/MysqlSwoole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
class MysqlSwoole
{

protected Pool|PDOPool $pool;

public function __construct($size)
{
$config = (new PDOConfig())
->withDriver('mysql')
->withHost('tfb-database')
->withPort(3306)
->withDbName('hello_world')
->withUsername('benchmarkdbuser')
->withPassword('benchmarkdbpass');
$this->pool = new PDOPool($config, $size);
}

function db(): array
{
$pdo = $this->pool->get();
$stmt = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$stmt->execute([mt_rand(1, 10000)]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$this->pool->put($pdo);
return $result;
}

function query($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$pdo = $this->pool->get();
$stmt = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$arr = [];
while ($query_count--) {
$stmt->execute([mt_rand(1, 10000)]);
$arr[] = $stmt->fetch(PDO::FETCH_ASSOC);
}
$this->pool->put($pdo);
return $arr;
}

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$arr = [];
$pdo = $this->pool->get();
$world = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
while ($query_count--) {
$id = mt_rand(1, 10000);
$world->execute([$id]);
$item = $world->fetch(PDO::FETCH_ASSOC);
$update->execute(
[$item['randomNumber'] = mt_rand(1, 10000), $id]
);
$arr[] = $item;
}
$this->pool->put($pdo);
return $arr;
}

function fortune(): string
{
$pdo = $this->pool->get();
$stmt = $pdo->prepare('SELECT id,message FROM Fortune');
$stmt->execute();
$arr = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
$this->pool->put($pdo);
$arr[0] = 'Additional fortune added at request time.';
asort($arr);
$html = '';
foreach ($arr as $id => $message) {
$message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
$html .= "<tr><td>$id</td><td>$message</td></tr>";
}
return "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>";
}

}
12 changes: 12 additions & 0 deletions frameworks/PHP/workerman/MysqlSwow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Swoole\Database\PDOPool;
class MysqlSwow extends MysqlSwoole
{

public function __construct($size)
{
$this->pool = new Pool("mysql:host=tfb-database;dbname=hello_world", 'benchmarkdbuser', 'benchmarkdbpass', $size);
}

}
65 changes: 65 additions & 0 deletions frameworks/PHP/workerman/Pgsql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

class Pgsql extends Mysql
{
protected PDO $pdo;
protected PDOStatement $world;
protected PDOStatement $fortune;
protected PDOStatement $update;
protected PDOStatement $random;
protected array $updates = [];

public function __construct()
{
$this->pdo = new PDO(
'pgsql:host=tfb-database;dbname=hello_world',
'benchmarkdbuser',
'benchmarkdbpass',
[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
]
);
$this->world = $this->random = $this->pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
$this->fortune = $this->pdo->prepare('SELECT id,message FROM Fortune');
$this->update = $this->pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
}

function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$worlds = [];
while ($query_count--) {
$this->random->execute([\mt_rand(1, 10000)]);
$world = $this->random->fetch();
$world['randomNumber'] = \mt_rand(1, 10000);
$worlds[] = $world;
}
$rows = count($worlds);

if (!isset($this->updates[$rows])) {
$sql = 'UPDATE world SET randomNumber = CASE id'
. str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $rows)
. 'END WHERE id IN ('
. str_repeat('?::INTEGER,', $rows - 1) . '?::INTEGER)';

$this->updates[$rows] = $this->pdo->prepare($sql);
}

$val = [];
$keys = [];
foreach ($worlds as $world) {
$val[] = $keys[] = $world['id'];
$val[] = $world['randomNumber'];
}

$this->updates[$rows]->execute([...$val, ...$keys]);
return $worlds;
}

}
61 changes: 61 additions & 0 deletions frameworks/PHP/workerman/PgsqlSwoole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
class PgsqlSwoole extends MysqlSwoole
{

protected Pool|PDOPool $pool;

public function __construct($size)
{
$config = (new PDOConfig())
->withDriver('pgsql')
->withHost('tfb-database')
->withPort(5432)
->withDbName('hello_world')
->withUsername('benchmarkdbuser')
->withPassword('benchmarkdbpass');

$this->pool = new PDOPool($config, $size);
}


function update($request): array
{
$query_count = 1;
$q = (int)$request->get('q');
if ($q > 1) {
$query_count = min($q, 500);
}
$worlds = [];
$pdo = $this->pool->get();
$random = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
while ($query_count--) {
$random->execute([mt_rand(1, 10000)]);
$world = $random->fetch(PDO::FETCH_ASSOC);
$world['randomNumber'] = mt_rand(1, 10000);
$worlds[] = $world;
}
$rows = count($worlds);

$sql = 'UPDATE world SET randomNumber = CASE id'
. str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $rows)
. 'END WHERE id IN ('
. str_repeat('?::INTEGER,', $rows - 1) . '?::INTEGER)';

$update = $pdo->prepare($sql);

$val = [];
$keys = [];
foreach ($worlds as $world) {
$val[] = $keys[] = $world['id'];
$val[] = $world['randomNumber'];
}

$update->execute([...$val, ...$keys]);
$this->pool->put($pdo);
return $worlds;
}

}
12 changes: 12 additions & 0 deletions frameworks/PHP/workerman/PgsqlSwow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Swoole\Database\PDOPool;
class PgsqlSwow extends MysqlSwow
{

public function __construct($size)
{
$this->pool = new Pool("pgsql:host=tfb-database;dbname=hello_world", 'benchmarkdbuser', 'benchmarkdbpass', $size);
}

}
29 changes: 29 additions & 0 deletions frameworks/PHP/workerman/Pool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Swow\Channel;

class Pool
{

protected static Channel $channel;

public function __construct($dsn, $username, $password, $size)
{
static::$channel = new Channel($size);
for ($i = 0; $i < $size; $i++) {
static::$channel->push(new PDO($dsn, $username, $password,[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]));
}
}

public function get(): PDO
{
return static::$channel->pop();
}

public function put(PDO $pdo)
{
return static::$channel->push($pdo);
}
}
Loading
Loading