Skip to content

Commit 8acd997

Browse files
authored
Update mixphp-workerman-pgsql orm to raw (#6823)
* feat: add mixphp * update /fortunes views and mixphp-workerman.dockerfile * Add bin/*.php * update dockerfile * update mixphp-workerman.dockerfile * Add mixphp-workerman-pgsql * update views/fortunes.php * update fortunes() * update conn pool $maxOpen * orm switch to object * update orm switch to object * Remove useless code * debug build docker * build dockerfile remove > /dev/null * handle false & update routes * Add 500 handle, close swoole coroutine * Update dockerfile * Update dockerfile * Update dockerfile * Update dockerfile * Update dockerfile * Update dockerfile * Update readme * Enable swoole coroutine * disable swoole coroutine * enable swoole coroutine * Update pool * disable swoole coroutine * Update ubuntu version * Update composer install * Remove workerman-mysql json_url plaintext_url * Update start params * Add phpfpm * Add phpfpm * Add phpfpm * Update mixphp.dockerfile * Update mixphp.dockerfile * Update mixphp.dockerfile * Update dockerfile * Update swoole.php * Update db options * Update db options * Update mixphp-workerman-pgsql orm to raw * Update mixphp-workerman-pgsql orm to raw * Update mixphp-workerman-pgsql orm to raw * Update mixphp-workerman-pgsql orm to raw * Update mixphp-workerman-pgsql orm to raw * Modify the method of obtaining parameters * Modify the method of obtaining parameters
1 parent 911b86d commit 8acd997

File tree

7 files changed

+179
-14
lines changed

7 files changed

+179
-14
lines changed

frameworks/PHP/mixphp/benchmark_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"framework": "MixPHP",
8484
"language": "PHP",
8585
"flavor": "PHP8",
86-
"orm": "micro",
86+
"orm": "raw",
8787
"platform": "workerman",
8888
"webserver": "None",
8989
"os": "Linux",

frameworks/PHP/mixphp/config.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ classification = "Fullstack"
6262
database = "Postgres"
6363
database_os = "Linux"
6464
os = "Linux"
65-
orm = "micro"
65+
orm = "raw"
6666
platform = "workerman"
6767
webserver = "None"
68-
versus = "workerman"
68+
versus = "workerman"

frameworks/PHP/mixphp/mixphp-workerman-pgsql.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ COPY php-jit.ini /etc/php/8.0/cli/php.ini
1515
ADD ./ /mixphp
1616
WORKDIR /mixphp
1717

18-
RUN sed -i "s|'mysql:host|'pgsql:host|g" /mixphp/src/Container/DB.php
18+
RUN sed -i "s|Benchmark();|BenchmarkRaw();|g" /mixphp/routes/index.php
1919

2020
RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
2121
RUN composer install --no-dev --classmap-authoritative --quiet > /dev/null

frameworks/PHP/mixphp/routes/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use App\Controller\Benchmark;
4+
use App\Controller\BenchmarkRaw;
45

56
return function (Mix\Vega\Engine $vega) {
67
$benchmark = new Benchmark();

frameworks/PHP/mixphp/src/Controller/Benchmark.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,14 @@ public function db(Context $ctx)
4242
public function query(Context $ctx)
4343
{
4444
$queryCount = 1;
45-
$q = (int)$ctx->query('q');
45+
$q = static::getQuery($ctx);
4646
if ($q > 1) {
4747
$queryCount = min($q, 500);
4848
}
4949

5050
$arr = [];
5151
while ($queryCount--) {
52-
$id = mt_rand(1, 10000);
53-
$ret = DB::instance()->raw('SELECT id,randomNumber FROM World WHERE id=?', $id)->first();
54-
if (!$ret) {
55-
continue;
56-
}
52+
$ret = DB::instance()->raw('SELECT id,randomNumber FROM World WHERE id=?', mt_rand(1, 10000))->first();
5753
$arr[] = $ret;
5854
}
5955

@@ -80,7 +76,7 @@ public function fortunes(Context $ctx)
8076
public function update(Context $ctx)
8177
{
8278
$queryCount = 1;
83-
$q = (int)$ctx->query('q');
79+
$q = static::getQuery($ctx);
8480
if ($q > 1) {
8581
$queryCount = min($q, 500);
8682
}
@@ -89,9 +85,6 @@ public function update(Context $ctx)
8985
while ($queryCount--) {
9086
$id = mt_rand(1, 10000);
9187
$ret = DB::instance()->raw('SELECT id,randomNumber FROM World WHERE id=?', $id)->first();
92-
if (!$ret) {
93-
continue;
94-
}
9588
DB::instance()->exec('UPDATE World SET randomNumber=? WHERE id=?', $ret->randomNumber = mt_rand(1, 10000), $id);
9689
$arr[] = $ret;
9790
}
@@ -100,4 +93,20 @@ public function update(Context $ctx)
10093
$ctx->string(200, json_encode($arr));
10194
}
10295

96+
/**
97+
* @param Context $ctx
98+
* @return int
99+
*/
100+
protected static function getQuery(Context $ctx): int
101+
{
102+
$request = $ctx->request;
103+
if ($request instanceof \Swoole\Http\Request) {
104+
return (int)($request->get['q'] ?? '');
105+
} elseif ($request instanceof \Workerman\Protocols\Http\Request) {
106+
return (int)$request->get('q');
107+
} else {
108+
return (int)$ctx->query('q');
109+
}
110+
}
111+
103112
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Mix\Vega\Context;
6+
7+
class BenchmarkRaw
8+
{
9+
10+
public function init()
11+
{
12+
global $world, $fortune, $update;
13+
if (isset($world)) {
14+
return;
15+
}
16+
17+
$pdo = new \PDO(
18+
'pgsql:host=tfb-database;dbname=hello_world',
19+
'benchmarkdbuser',
20+
'benchmarkdbpass',
21+
[
22+
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
23+
]
24+
);
25+
$world = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
26+
$fortune = $pdo->prepare('SELECT id,message FROM Fortune');
27+
$update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
28+
}
29+
30+
/**
31+
* @param Context $ctx
32+
*/
33+
public function json(Context $ctx)
34+
{
35+
$ctx->setHeader('Content-Type', 'application/json');
36+
$ctx->string(200, json_encode(['message' => 'Hello, World!']));
37+
}
38+
39+
/**
40+
* @param Context $ctx
41+
*/
42+
public function plaintext(Context $ctx)
43+
{
44+
$ctx->setHeader('Content-Type', 'text/plain; charset=utf-8');
45+
$ctx->string(200, 'Hello, World!');
46+
}
47+
48+
/**
49+
* @param Context $ctx
50+
*/
51+
public function db(Context $ctx)
52+
{
53+
$this->init();
54+
global $world;
55+
56+
$world->execute([mt_rand(1, 10000)]);
57+
58+
$ctx->setHeader('Content-Type', 'application/json');
59+
$ctx->string(200, json_encode($world->fetch()));
60+
}
61+
62+
/**
63+
* @param Context $ctx
64+
*/
65+
public function query(Context $ctx)
66+
{
67+
$this->init();
68+
global $world;
69+
70+
$queryCount = 1;
71+
$q = static::getQuery($ctx);
72+
if ($q > 1) {
73+
$queryCount = min($q, 500);
74+
}
75+
76+
$arr = [];
77+
while ($queryCount--) {
78+
$world->execute([mt_rand(1, 10000)]);
79+
$arr[] = $world->fetch();
80+
}
81+
82+
$ctx->setHeader('Content-Type', 'application/json');
83+
$ctx->string(200, json_encode($arr));
84+
}
85+
86+
/**
87+
* @param Context $ctx
88+
*/
89+
public function fortunes(Context $ctx)
90+
{
91+
$this->init();
92+
global $fortune;
93+
94+
$fortune->execute();
95+
96+
$arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
97+
$arr[0] = 'Additional fortune added at request time.';
98+
asort($arr);
99+
100+
$html = '';
101+
foreach ($arr as $id => $message) {
102+
$message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
103+
$html .= "<tr><td>$id</td><td>$message</td></tr>";
104+
}
105+
106+
$ctx->string(200, "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>");
107+
}
108+
109+
/**
110+
* @param Context $ctx
111+
*/
112+
public function update(Context $ctx)
113+
{
114+
$this->init();
115+
global $world, $update;
116+
117+
$queryCount = 1;
118+
$q = static::getQuery($ctx);
119+
if ($q > 1) {
120+
$queryCount = min($q, 500);
121+
}
122+
123+
$arr = [];
124+
while ($queryCount--) {
125+
$id = mt_rand(1, 10000);
126+
$world->execute([$id]);
127+
$ret = $world->fetch();
128+
$update->execute(
129+
[$ret['randomNumber'] = mt_rand(1, 10000), $id]
130+
);
131+
$arr[] = $ret;
132+
}
133+
134+
$ctx->setHeader('Content-Type', 'application/json');
135+
$ctx->string(200, json_encode($arr));
136+
}
137+
138+
/**
139+
* @param Context $ctx
140+
* @return int
141+
*/
142+
protected static function getQuery(Context $ctx): int
143+
{
144+
$request = $ctx->request;
145+
if ($request instanceof \Swoole\Http\Request) {
146+
return (int)($request->get['q'] ?? '');
147+
} elseif ($request instanceof \Workerman\Protocols\Http\Request) {
148+
return (int)$request->get('q');
149+
} else {
150+
return (int)$ctx->query('q');
151+
}
152+
}
153+
154+
}

frameworks/PHP/mixphp/src/Vega.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Vega
1717
public static function new(): Engine
1818
{
1919
$vega = new Engine();
20+
$vega->mode(Engine::FAST_MODE);
2021

2122
// 500
2223
$vega->use(function (Context $ctx) {

0 commit comments

Comments
 (0)