Skip to content

Commit de11533

Browse files
authored
Add laravel-s bechmark for laravel/lumen (#6216)
* add laravel-s benchmark * rename lumen-s to laravel-s * optimize codestyle
1 parent 987c956 commit de11533

File tree

13 files changed

+397
-150
lines changed

13 files changed

+397
-150
lines changed

frameworks/PHP/laravel/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,7 @@ http://localhost:8080/updates/[count]
114114

115115
### FORTUNES
116116

117-
http://localhost:8080/fortunes
117+
http://localhost:8080/fortunes
118+
119+
# Add laravel-s Benchmarking Test
120+
[laravel-s](https://github.com/hhxsv5/laravel-s) is an out-of-the-box adapter between Swoole and Laravel/Lumen, similar to laravel-swoole.
Lines changed: 76 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,84 @@
11
<?php
2+
23
namespace App\Http\Controllers;
34

45
use App\Models\Fortune;
56
use App\Models\World;
67
use Illuminate\Routing\Controller as BaseController;
78

8-
class Controller extends BaseController {
9-
10-
public function json() {
11-
return [
12-
'message' => 'Hello, World!'
13-
];
14-
}
15-
16-
public function db() {
17-
return World::where('id', \mt_rand(1, 10000))->first(); // to compare with find()
18-
}
19-
20-
public function queries($queries = 1) {
21-
$rows = [];
22-
$numbers = $this->getUniqueRandomNumbers($this->clamp($queries));
23-
foreach ($numbers as $id) {
24-
$rows[] = World::find($id);
25-
}
26-
27-
return $rows;
28-
}
29-
30-
public function fortunes() {
31-
$rows = Fortune::all();
32-
33-
$insert = new Fortune();
34-
$insert->id = 0;
35-
$insert->message = 'Additional fortune added at request time.';
36-
37-
$rows->add($insert);
38-
$rows = $rows->sortBy('message');
39-
40-
return view('fortunes', [
41-
'rows' => $rows
42-
]);
43-
}
44-
45-
public function updates($queries = 1) {
46-
$rows = [];
47-
48-
$numbers = $this->getUniqueRandomNumbers($this->clamp($queries));
49-
foreach ($numbers as $id) {
50-
$row = World::find($id);
51-
$oldId = $row->randomNumber;
52-
do {
53-
$newId = mt_rand(1, 10000);
54-
} while ($oldId === $newId);
55-
$row->randomNumber = $newId;
56-
do {
57-
try {
58-
$saved = $row->save();
59-
} catch (\Exception $e) {
60-
$saved = false;
61-
}
62-
} while (! $saved);
63-
$rows[] = $row;
64-
}
65-
66-
return $rows;
67-
}
68-
69-
public function plaintext() {
70-
return response('Hello, World!')->header('Content-Type', 'text/plain');
71-
}
72-
73-
private function clamp($value): int {
74-
if (! \is_numeric($value) || $value < 1) {
75-
return 1;
76-
} else if ($value > 500) {
77-
return 500;
78-
} else {
79-
return $value;
80-
}
81-
}
82-
83-
private function getUniqueRandomNumbers($count) {
84-
$res = [];
85-
do {
86-
$res[\mt_rand(1, 10000)] = 1;
87-
} while (\count($res) < $count);
88-
\ksort($res);
89-
return \array_keys($res);
90-
}
9+
class Controller extends BaseController
10+
{
11+
public function json()
12+
{
13+
return response()->json(['message' => 'Hello, World!']);
14+
}
15+
16+
public function db()
17+
{
18+
return response()->json(World::query()->find(self::randomInt()));
19+
}
20+
21+
public function queries($queries = 1)
22+
{
23+
$queries = self::clamp($queries);
24+
25+
$rows = [];
26+
while ($queries--) {
27+
$rows[] = World::query()->find(self::randomInt());
28+
}
29+
30+
return response()->json($rows);
31+
}
32+
33+
public function fortunes()
34+
{
35+
$rows = Fortune::all();
36+
37+
$insert = new Fortune();
38+
$insert->id = 0;
39+
$insert->message = 'Additional fortune added at request time.';
40+
41+
$rows->add($insert);
42+
$rows = $rows->sortBy('message');
43+
44+
return view('fortunes', ['rows' => $rows]);
45+
}
46+
47+
public function updates($queries = 1)
48+
{
49+
$queries = self::clamp($queries);
50+
51+
$rows = [];
52+
53+
while ($queries--) {
54+
$row = World::query()->find(self::randomInt());
55+
$row->randomNumber = self::randomInt();
56+
$row->save();
57+
58+
$rows[] = $row;
59+
}
60+
61+
return response()->json($rows);
62+
}
63+
64+
public function plaintext()
65+
{
66+
return response('Hello, World!', 200, ['Content-Type' => 'text/plain']);
67+
}
68+
69+
private static function randomInt()
70+
{
71+
return random_int(1, 10000);
72+
}
73+
74+
private static function clamp($value)
75+
{
76+
if (!is_numeric($value) || $value < 1) {
77+
return 1;
78+
}
79+
if ($value > 500) {
80+
return 500;
81+
}
82+
return (int)$value;
83+
}
9184
}

frameworks/PHP/laravel/benchmark_config.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"database": "MySQL",
1515
"framework": "laravel",
1616
"language": "PHP",
17-
"flavor": "PHP7",
17+
"flavor": "PHP8",
1818
"orm": "Full",
1919
"platform": "FPM/FastCGI",
2020
"webserver": "nginx",
@@ -46,6 +46,29 @@
4646
"display_name": "laravel-swoole",
4747
"notes": "",
4848
"versus": "swoole"
49+
},
50+
"laravel-s": {
51+
"json_url": "/json",
52+
"db_url": "/db",
53+
"query_url": "/queries/",
54+
"fortune_url": "/fortunes",
55+
"update_url": "/updates/",
56+
"plaintext_url": "/plaintext",
57+
"port": 5200,
58+
"approach": "Realistic",
59+
"classification": "Fullstack",
60+
"database": "MySQL",
61+
"framework": "laravel",
62+
"language": "PHP",
63+
"flavor": "None",
64+
"orm": "Full",
65+
"platform": "swoole",
66+
"webserver": "none",
67+
"os": "Linux",
68+
"database_os": "Linux",
69+
"display_name": "laravel-s",
70+
"notes": "",
71+
"versus": "swoole"
4972
}
5073
}]
5174
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "laravel/laravel",
3+
"type": "project",
4+
"description": "The Laravel Framework.",
5+
"keywords": [
6+
"framework",
7+
"laravel"
8+
],
9+
"license": "MIT",
10+
"require": {
11+
"php": "^7.3|^8.0",
12+
"laravel/framework": "^8.0",
13+
"hhxsv5/laravel-s": "~3.7.0"
14+
},
15+
"require-dev": {
16+
"facade/ignition": "^2.3.6",
17+
"fzaninotto/faker": "^1.9.1",
18+
"mockery/mockery": "^1.3.1",
19+
"nunomaduro/collision": "^5.0",
20+
"phpunit/phpunit": "^9.3"
21+
},
22+
"config": {
23+
"optimize-autoloader": true,
24+
"preferred-install": "dist",
25+
"sort-packages": true
26+
},
27+
"extra": {
28+
"laravel": {
29+
"dont-discover": []
30+
}
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"App\\": "app/"
35+
}
36+
},
37+
"autoload-dev": {
38+
"psr-4": {
39+
"Tests\\": "tests/"
40+
}
41+
},
42+
"minimum-stability": "dev",
43+
"prefer-stable": true,
44+
"scripts": {
45+
"post-autoload-dump": [
46+
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
47+
"@php artisan package:discover --ansi"
48+
],
49+
"post-root-package-install": [
50+
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
51+
],
52+
"post-create-project-cmd": [
53+
"@php artisan key:generate --ansi"
54+
]
55+
}
56+
}
57+
58+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
EXPECTED_SIGNATURE="$(curl -s https://composer.github.io/installer.sig)"
4+
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
5+
ACTUAL_SIGNATURE="$(php -r "echo hash_file('SHA384', 'composer-setup.php');")"
6+
7+
if [ "$EXPECTED_SIGNATURE" != "$ACTUAL_SIGNATURE" ]
8+
then
9+
>&2 echo 'ERROR: Invalid installer signature'
10+
rm composer-setup.php
11+
exit 1
12+
fi
13+
14+
php composer-setup.php --quiet
15+
RESULT=$?
16+
rm composer-setup.php
17+
exit $RESULT
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM php:8.0
2+
3+
RUN pecl install swoole > /dev/null && \
4+
docker-php-ext-enable swoole
5+
RUN docker-php-ext-install pdo_mysql pcntl opcache > /dev/null
6+
7+
RUN echo "opcache.enable_cli=1" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
8+
RUN echo "opcache.jit=1205" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
9+
RUN echo "opcache.jit_buffer_size=128M" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
10+
11+
ADD ./ /laravel
12+
WORKDIR /laravel
13+
14+
RUN mkdir -p /laravel/bootstrap/cache /laravel/storage/logs /laravel/storage/framework/sessions /laravel/storage/framework/views /laravel/storage/framework/cache
15+
RUN chmod -R 777 /laravel
16+
17+
RUN deploy/swoole/install-composer.sh
18+
RUN apt-get update > /dev/null && \
19+
apt-get install -yqq git unzip > /dev/null
20+
COPY deploy/laravel-s/composer* ./
21+
22+
RUN echo "LARAVELS_LISTEN_IP=0.0.0.0" >> .env
23+
RUN echo "LARAVELS_LISTEN_PORT=5200" >> .env
24+
25+
RUN php composer.phar install -a --no-dev --quiet
26+
RUN php artisan optimize
27+
RUN php artisan laravels publish
28+
29+
CMD bin/laravels start

frameworks/PHP/lumen/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ http://localhost:8080/updates/[count]
7777
### FORTUNES
7878

7979
http://localhost:8080/fortunes
80+
81+
82+
# Add laravel-s Benchmarking Test
83+
[laravel-s](https://github.com/hhxsv5/laravel-s) is an out-of-the-box adapter between Swoole and Laravel/Lumen, similar to laravel-swoole.

0 commit comments

Comments
 (0)