Skip to content

Commit 510c990

Browse files
steevanbphpbenchmarks
authored and
phpbenchmarks
committed
Add template engine kit and fix source code urls questions
1 parent 2fbb294 commit 510c990

15 files changed

+280
-53
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Choose the component type and benchmark type you want to code:
119119
* [Hello world benchmark](documentation/framework/helloWorld.md)
120120
* [REST API benchmark](documentation/framework/restApi.md)
121121
* Template engine
122-
* Hello world benchmark (coming soon)
122+
* [Hello world benchmark](documentation/templateEngine/helloWorld.md)
123123

124124
Note that `all` component benchmarks needs to bo validated to make your component appear on [phpbenchmarks.com](http://www.phpbenchmarks.com).
125125

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### [3.1.0](../../compare/3.0.2...3.1.0) - 2018-05-01
2+
3+
- Add template engine kit
4+
- Fix source code urls questions
5+
16
### [3.0.2](../../compare/3.0.1...3.0.2) - 2018-04-28
27

38
- Change `responseBodyFileMinSize` for REST API for serializer who not escape `/`.

documentation/framework/helloWorld.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Hello world benchmark
22
-
33

4-
This benchmark shows the overhead cost of using a framework, instead of writing your code in PHP.
4+
This benchmark shows the overhead cost of using a framework instead of writing your code in PHP.
55

6-
To respect that, code should write `Hello World !` (yes, with space before `!`, cocorico ;)) in response body, as fast as possible.
6+
To respect that code should write `Hello World !` (yes, with space before `!`, cocorico ;)) in response body, as fast as possible.
77

88
Disable everything you can: template engine, session, database access etc.
99

@@ -12,8 +12,8 @@ Features
1212

1313
Don't forget this features as to be coded in [common repository](../repositoriesAndBranches.md).
1414

15-
* A route, with the url `benchmark/helloworld`. [Example](https://github.com/phpbenchmarks/symfony-common/blob/symfony_4_hello-world/Resources/config/routing.yml).
16-
* A controller, called by this route. [Example](https://github.com/phpbenchmarks/symfony-common/blob/symfony_4_hello-world/Controller/HelloWorldController.php).
15+
* A route with the url `benchmark/helloworld`. [Example](https://github.com/phpbenchmarks/symfony-common/blob/symfony_4_hello-world/Resources/config/routing.yml).
16+
* A controller called by this route. [Example](https://github.com/phpbenchmarks/symfony-common/blob/symfony_4_hello-world/Controller/HelloWorldController.php).
1717
* This controller should write `Hello World !` in response body as fast as possible. [Example](https://github.com/phpbenchmarks/symfony-common/blob/symfony_4_hello-world/Controller/HelloWorldController.php#L13).
1818

1919
[Back to documentation index](../../README.md)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Hello world benchmark
2+
-
3+
4+
This benchmark shows the overhead cost of using a template engine instead of writing your code in PHP.
5+
6+
To respect that code should write `Hello World !` (yes, with space before `!`, cocorico ;)) in response body, as fast as possible.
7+
8+
Features
9+
-
10+
11+
Don't forget this features as to be coded in [common repository](../repositoriesAndBranches.md).
12+
13+
* An entry point. [Example](https://github.com/phpbenchmarks/twig-common/blob/2.1.0/helloworld.php).
14+
* A template used by this entry point. [Example](https://github.com/phpbenchmarks/twig-common/blob/2.1.0/templates/helloworld.html.twig).
15+
* Write `Hello World !` in response body as fast as possible. [Example](https://github.com/phpbenchmarks/twig-common/blob/2.1.0/helloworld.php#L8).
16+
17+
[Back to documentation index](../../README.md)

src/Benchmark/BenchmarkType.php

+31-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ class BenchmarkType
2121
'defaultBenchmarkUrl' => '/benchmark/helloworld',
2222
'responseBodyFiles' => ['responseBody.txt'],
2323
'responseBodyFileMinSize' => 13,
24-
'sourceCodeUrlIds' => ['route', 'controller']
24+
'sourceCodeUrlIds' => [
25+
ComponentType::PHP => ['entryPoint'],
26+
ComponentType::FRAMEWORK => ['route', 'controller'],
27+
ComponentType::TEMPLATE_ENGINE => ['entryPoint', 'template']
28+
]
2529
],
2630
self::REST_API => [
2731
'name' => 'REST API',
@@ -31,13 +35,23 @@ class BenchmarkType
3135
'responseBodyFiles' => ['responseBody.en_GB.json', 'responseBody.fr_FR.json', 'responseBody.en.json'],
3236
'responseBodyFileMinSize' => 7541,
3337
'sourceCodeUrlIds' => [
34-
'route',
35-
'controller',
36-
'randomizeLanguageDispatchEvent',
37-
'randomizeLanguageEventListener',
38-
'translations',
39-
'translate',
40-
'serialize'
38+
ComponentType::PHP => [
39+
'entryPoint',
40+
'randomizeLanguageDispatchEvent',
41+
'randomizeLanguageEventListener',
42+
'translations',
43+
'translate',
44+
'serialize'
45+
],
46+
ComponentType::FRAMEWORK => [
47+
'route',
48+
'controller',
49+
'randomizeLanguageDispatchEvent',
50+
'randomizeLanguageEventListener',
51+
'translations',
52+
'translate',
53+
'serialize'
54+
]
4155
]
4256
],
4357
self::TEMPLATING_SMALL_OVERLOAD => [
@@ -46,15 +60,19 @@ class BenchmarkType
4660
'slug' => 'templating-small-overload',
4761
'defaultBenchmarkUrl' => '/index.php',
4862
'responseBodyFiles' => ['responseBody.html'],
49-
'sourceCodeUrlIds' => []
63+
'sourceCodeUrlIds' => [
64+
ComponentType::PHP => ['entryPoint']
65+
]
5066
],
5167
self::TEMPLATING_BIG_OVERLOAD => [
5268
'name' => 'Template engine big overload',
5369
'upperCamelCaseName' => 'TemplateEngineBigOverload',
5470
'slug' => 'templating-big-overload',
5571
'defaultBenchmarkUrl' => '/index.php',
5672
'responseBodyFiles' => ['responseBody.html'],
57-
'sourceCodeUrlIds' => []
73+
'sourceCodeUrlIds' => [
74+
ComponentType::PHP => ['entryPoint']
75+
]
5876
]
5977
];
6078

@@ -75,6 +93,7 @@ public static function getAllByComponentType(): array
7593
return [
7694
ComponentType::PHP => [
7795
static::HELLO_WORLD => $benchmarkTypes[static::HELLO_WORLD],
96+
static::REST_API => $benchmarkTypes[static::REST_API],
7897
static::TEMPLATING_SMALL_OVERLOAD => $benchmarkTypes[static::TEMPLATING_SMALL_OVERLOAD],
7998
static::TEMPLATING_BIG_OVERLOAD => $benchmarkTypes[static::TEMPLATING_BIG_OVERLOAD]
8099
],
@@ -123,9 +142,9 @@ public static function getResponseBodyFileMinSize(int $type): int
123142
return static::getConfiguration($type, 'responseBodyFileMinSize');
124143
}
125144

126-
public static function getSourceCodeUrlIds(int $type): array
145+
public static function getSourceCodeUrlIds(int $type, int $componentType): array
127146
{
128-
return static::getConfiguration($type, 'sourceCodeUrlIds');
147+
return static::getConfiguration($type, 'sourceCodeUrlIds')[$componentType];
129148
}
130149

131150
/** @return mixed */

src/Command/Configure/ConfigureComponentCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected function createFile(): int
176176
$componentTypes
177177
);
178178

179-
$benchmarkTypes = BenchmarkType::getAll();
179+
$benchmarkTypes = BenchmarkType::getByComponentType($componentType);
180180
$benchmarkType = array_search(
181181
$this->choiceQuestion('Benchmark type?', $benchmarkTypes),
182182
$benchmarkTypes

src/Command/Configure/ConfigureComponentSourceCodeUrlsCommand.php

+53-32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use AbstractComponentConfiguration\AbstractComponentConfiguration;
88
use App\{
9+
Benchmark\BenchmarkType,
910
Command\AbstractCommand,
1011
Command\Validate\ValidateConfigurationComponentSourceCodeUrlsCommand,
1112
ComponentConfiguration\ComponentConfiguration
@@ -76,68 +77,88 @@ function () use ($url) {
7677

7778
protected function getSourceCodeUrls(): array
7879
{
79-
$urls = ComponentConfiguration::getSourceCodeUrls();
80-
$return = [
80+
$return = $this->getSourceCodeUrlsToAsk(
81+
ComponentConfiguration::getBenchmarkType(),
82+
ComponentConfiguration::getComponentType(),
83+
ComponentConfiguration::getSourceCodeUrls()
84+
);
85+
86+
foreach ($return as &$url) {
87+
$violations = ValidateConfigurationComponentSourceCodeUrlsCommand::validateSourCodeUrl($url['url']);
88+
$showWarning = false;
89+
do {
90+
if (count($violations) > 0 && $showWarning) {
91+
$errors = [];
92+
/** @var ConstraintViolationInterface $violation */
93+
foreach ($violations as $violation) {
94+
$errors[] = $violation->getMessage();
95+
}
96+
$this->warning(implode(', ', $errors));
97+
}
98+
99+
$url['url'] = $this->question(
100+
$url['question'],
101+
substr($url['url'] ?? '', 0, 4) === '____' ? null : $url['url']
102+
);
103+
104+
$showWarning = true;
105+
} while (
106+
count(
107+
$violations = ValidateConfigurationComponentSourceCodeUrlsCommand::validateSourCodeUrl($url['url'])
108+
) > 0
109+
);
110+
}
111+
112+
return $return;
113+
}
114+
115+
protected function getSourceCodeUrlsToAsk(int $benchmarkType, int $componentType, array $defaultUrls): array
116+
{
117+
$availableUrls = [
118+
'entryPoint' => [
119+
'question' => 'URL to entry point code?',
120+
'variable' => '____PHPBENCHMARKS_ENTRY_POINT_URL____'
121+
],
122+
'template' => [
123+
'question' => 'URL to template code?',
124+
'variable' => '____PHPBENCHMARKS_TEMPLATE_URL____'
125+
],
81126
'route' => [
82127
'question' => 'URL to benchmark route code?',
83-
'url' => $urls['route'] ?? null,
84128
'variable' => '____PHPBENCHMARKS_ROUTE_SOURCE_CODE_URL____'
85129
],
86130
'controller' => [
87131
'question' => 'URL to Controller code?',
88-
'url' => $urls['controller'] ?? null,
89132
'variable' => '____PHPBENCHMARKS_CONTROLLER_SOURCE_CODE_URL____'
90133
],
91134
'randomizeLanguageDispatchEvent' => [
92135
'question' => 'URL to code who dispatch event to randomize language?',
93-
'url' => $urls['randomizeLanguageDispatchEvent'] ?? null,
94136
'variable' => '____PHPBENCHMARKS_RANDOMIZE_LANGUAGE_DISPATCH_EVENT_SOURCE_CODE_URL____'
95137
],
96138
'randomizeLanguageEventListener' => [
97139
'question' => 'URL to code who listen event to randomize language?',
98-
'url' => $urls['randomizeLanguageEventListener'] ?? null,
99140
'variable' => '____PHPBENCHMARKS_RANDOMIZE_LANGUAGE_EVENT_LISTENER_SOURCE_CODE_URL____'
100141
],
101142
'translations' => [
102143
'question' => 'URL to en_GB translations code?',
103-
'url' => $urls['translations'] ?? null,
104144
'variable' => '____PHPBENCHMARKS_TRANSLATIONS_SOURCE_CODE_URL____'
105145
],
106146
'translate' => [
107147
'question' => 'URL to code who translate translated.1000 key?',
108-
'url' => $urls['translate'] ?? null,
109148
'variable' => '____PHPBENCHMARKS_TRANSLATE_SOURCE_CODE_URL____'
110149
],
111150
'serialize' => [
112151
'question' => 'URL to code who serialize User?',
113-
'url' => $urls['serialize'] ?? null,
114152
'variable' => '____PHPBENCHMARKS_SERIALIZE_SOURCE_CODE_URL____'
115153
]
116154
];
117155

118-
foreach ($return as &$url) {
119-
$violations = ValidateConfigurationComponentSourceCodeUrlsCommand::validateSourCodeUrl($url['url']);
120-
$showWarning = false;
121-
do {
122-
if (count($violations) > 0) {
123-
if ($showWarning) {
124-
$errors = [];
125-
/** @var ConstraintViolationInterface $violation */
126-
foreach ($violations as $violation) {
127-
$errors[] = $violation->getMessage();
128-
}
129-
$this->warning(implode(', ', $errors));
130-
}
131-
132-
$url['url'] = $this->question($url['question']);
133-
}
134-
135-
$showWarning = true;
136-
} while (
137-
count(
138-
$violations = ValidateConfigurationComponentSourceCodeUrlsCommand::validateSourCodeUrl($url['url'])
139-
) > 0
140-
);
156+
$return = [];
157+
foreach (BenchmarkType::getSourceCodeUrlIds($benchmarkType, $componentType) as $urlId) {
158+
if (array_key_exists($urlId, $availableUrls) === false) {
159+
throw new \Exception('Unknown url id "' . $urlId . '".');
160+
}
161+
$return[$urlId] = array_merge($availableUrls[$urlId], ['url' => $defaultUrls[$urlId] ?? null]);
141162
}
142163

143164
return $return;

src/Command/Validate/ValidateConfigurationComponentSourceCodeUrlsCommand.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ protected function assertCodeSourceUrls(): self
6767
. ' parameter to validate it.'
6868
);
6969
} else {
70-
$expectedUrlIds = BenchmarkType::getSourceCodeUrlIds(ComponentConfiguration::getBenchmarkType());
70+
$expectedUrlIds = BenchmarkType::getSourceCodeUrlIds(
71+
ComponentConfiguration::getBenchmarkType(),
72+
ComponentConfiguration::getComponentType()
73+
);
7174
$urls = ComponentConfiguration::getSourceCodeUrls();
7275
foreach ($urls as $id => $url) {
7376
if (in_array($id, $expectedUrlIds) === false) {
74-
$this->error('getSourceCodeUrls() return an array with unknown key ' . $id . '.');
77+
$this->error('getSourceCodeUrls() return an array with unknown key "' . $id . '".');
7578
}
7679

7780
$violations = static::validateSourCodeUrl($url);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ____NAMESPACE____;
6+
7+
abstract class AbstractComponentConfiguration
8+
{
9+
public static function getComponentType(): int
10+
{
11+
return 3;
12+
}
13+
14+
public static function getComponentName(): string
15+
{
16+
return '____PHPBENCHMARKS_COMPONENT_NAME____';
17+
}
18+
19+
public static function getComponentSlug(): string
20+
{
21+
return '____PHPBENCHMARKS_COMPONENT_SLUG____';
22+
}
23+
24+
public static function isPhp56Enabled(): bool
25+
{
26+
return ____PHPBENCHMARKS_PHP56_ENABLED____;
27+
}
28+
29+
public static function isPhp70Enabled(): bool
30+
{
31+
return ____PHPBENCHMARKS_PHP70_ENABLED____;
32+
}
33+
34+
public static function isPhp71Enabled(): bool
35+
{
36+
return ____PHPBENCHMARKS_PHP71_ENABLED____;
37+
}
38+
39+
public static function isPhp72Enabled(): bool
40+
{
41+
return ____PHPBENCHMARKS_PHP72_ENABLED____;
42+
}
43+
44+
public static function isPhp73Enabled(): bool
45+
{
46+
return ____PHPBENCHMARKS_PHP73_ENABLED____;
47+
}
48+
49+
public static function getBenchmarkUrl(): string
50+
{
51+
return '____PHPBENCHMARKS_BENCHMARK_URL____';
52+
}
53+
54+
public static function getCoreDependencyName(): string
55+
{
56+
return '____PHPBENCHMARKS_CORE_DEPENDENCY_NAME____';
57+
}
58+
59+
public static function getCoreDependencyMajorVersion(): int
60+
{
61+
return ____PHPBENCHMARKS_CORE_DEPENDENCY_MAJOR_VERSION____;
62+
}
63+
64+
public static function getCoreDependencyMinorVersion(): int
65+
{
66+
return ____PHPBENCHMARKS_CORE_DEPENDENCY_MINOR_VERSION____;
67+
}
68+
69+
public static function getCoreDependencyPatchVersion(): int
70+
{
71+
return ____PHPBENCHMARKS_CORE_DEPENDENCY_PATCH_VERSION____;
72+
}
73+
74+
public static function getBenchmarkType(): int
75+
{
76+
return 1;
77+
}
78+
79+
public static function getSourceCodeUrls(): array
80+
{
81+
return [
82+
'entryPoint' => '____PHPBENCHMARKS_ENTRY_POINT_URL____',
83+
'template' => '____PHPBENCHMARKS_TEMPLATE_URL____'
84+
];
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public static function getSourceCodeUrls(): array
2+
{
3+
return [
4+
'entryPoint' => '____PHPBENCHMARKS_ENTRY_POINT_URL____',
5+
'template' => '____PHPBENCHMARKS_TEMPLATE_URL____'
6+
];
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World !

0 commit comments

Comments
 (0)