Skip to content

Commit fa282bb

Browse files
committed
Close #186
1 parent fbe87b5 commit fa282bb

File tree

5 files changed

+65
-27
lines changed

5 files changed

+65
-27
lines changed

sample/Application/EncodeSamples.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ public function getDynamicSchemaExample()
183183
*
184184
* @param int $iterations
185185
*
186-
* @return mixed
186+
* @return array
187187
*/
188-
public function runPerformanceTestForSmallNestedResources($iterations)
188+
public function runPerformanceTestForSmallNestedResources(int $iterations): array
189189
{
190190
$closure = function () use ($iterations) {
191191
$options = new EncodingParameters(
@@ -217,20 +217,21 @@ public function runPerformanceTestForSmallNestedResources($iterations)
217217
}
218218
};
219219

220-
$timeSpent = null;
221-
$this->getTime($closure, $timeSpent);
220+
$timeSpent = 0;
221+
$bytesUsed = 0;
222+
$this->getTime($closure, $timeSpent, $bytesUsed);
222223

223-
return $timeSpent;
224+
return [$timeSpent, $bytesUsed];
224225
}
225226

226227
/**
227228
* Run performance test one time for big collection of resources.
228229
*
229230
* @param int $numberOfItems
230231
*
231-
* @return mixed
232+
* @return array
232233
*/
233-
public function runPerformanceTestForBigCollection($numberOfItems)
234+
public function runPerformanceTestForBigCollection(int $numberOfItems): array
234235
{
235236
$closure = function() use ($numberOfItems) {
236237
$sites = [];
@@ -262,26 +263,32 @@ public function runPerformanceTestForBigCollection($numberOfItems)
262263
$encoder->encodeData($sites, $options);
263264
};
264265

265-
$timeSpent = null;
266-
$this->getTime($closure, $timeSpent);
266+
$timeSpent = 0;
267+
$bytesUsed = 0;
268+
$this->getTime($closure, $timeSpent, $bytesUsed);
267269

268-
return $timeSpent;
270+
return [$timeSpent, $bytesUsed];
269271
}
270272

271273
/**
272274
* @param Closure $closure
273275
* @param float &$time
276+
* @param int &$memory
274277
*
275278
* @return mixed
276279
*/
277-
private function getTime(Closure $closure, &$time)
280+
private function getTime(Closure $closure, float &$time, int &$memory)
278281
{
279-
$time_start = microtime(true);
282+
$timeStart = microtime(true);
283+
$bytesStart = memory_get_usage();
280284
try {
281285
return $closure();
282286
} finally {
283-
$time_end = microtime(true);
284-
$time = $time_end - $time_start;
287+
$bytesEnd = memory_get_usage();
288+
$timeEnd = microtime(true);
289+
290+
$time = $timeEnd - $timeStart;
291+
$memory = $bytesEnd - $bytesStart;
285292
}
286293
}
287294
}

sample/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
],
1111
"autoload": {
1212
"psr-4": {
13+
"Neomerx\\JsonApi\\": "./../src",
1314
"Neomerx\\Samples\\JsonApi\\": "./"
1415
}
1516
},
1617
"require": {
17-
"neomerx/json-api": "dev-develop"
18+
"php": ">=7.1.0",
19+
"psr/log": "^1.0"
1820
}
1921
}

sample/docker-compose.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Usefull links
2+
# =============
3+
# https://hub.docker.com/_/php/
4+
# https://docs.docker.com/compose/overview/
5+
# https://docs.docker.com/compose/compose-file/
6+
7+
cli_7_1_php:
8+
image: php:7.1-cli
9+
container_name: cli_php_7_1_json_api
10+
volumes:
11+
- ./..:/app
12+
working_dir: /app
13+
tty: true
14+
15+
cli_7_2_php:
16+
image: php:7.2-cli
17+
container_name: cli_php_7_2_json_api
18+
volumes:
19+
- ./..:/app
20+
working_dir: /app
21+
tty: true
22+
23+
cli_7_3_php:
24+
image: php:7.3-cli
25+
container_name: cli_php_7_3_json_api
26+
volumes:
27+
- ./..:/app
28+
working_dir: /app
29+
tty: true

sample/sample.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
* limitations under the License.
1717
*/
1818

19-
use \Neomerx\JsonApi\Schema\Link;
20-
use \Neomerx\JsonApi\Encoder\Encoder;
21-
use \Neomerx\JsonApi\Encoder\EncoderOptions;
22-
use \Neomerx\Samples\JsonApi\Application\EncodeSamples;
23-
use \Neomerx\JsonApi\Http\Parameters\EncodingParameters;
19+
use Neomerx\JsonApi\Http\Parameters\EncodingParameters;
20+
use Neomerx\JsonApi\Schema\Link;
21+
use Neomerx\Samples\JsonApi\Application\EncodeSamples;
2422

2523
require './vendor/autoload.php';
2624

27-
/**
25+
/** @noinspection PhpIllegalPsrClassPathInspection
2826
* @package Neomerx\Samples\JsonApi
2927
*/
3028
class Application
@@ -98,8 +96,9 @@ private function dynamicSchemaExample()
9896
private function runPerformanceTestForSmallNestedResources($num)
9997
{
10098
echo "Neomerx JSON API performance test ($num iterations for small resources)... ";
101-
$time = $this->samples->runPerformanceTestForSmallNestedResources($num);
102-
echo $time . ' seconds' . PHP_EOL;
99+
[$time, $bytes] = $this->samples->runPerformanceTestForSmallNestedResources($num);
100+
$bytes = number_format($bytes);
101+
echo "$time seconds ($bytes bytes used)." . PHP_EOL;
103102
}
104103

105104
/**
@@ -110,8 +109,9 @@ private function runPerformanceTestForSmallNestedResources($num)
110109
private function runPerformanceTestForBigCollection($num)
111110
{
112111
echo "Neomerx JSON API performance test (1 iteration for $num resources)... ";
113-
$time = $this->samples->runPerformanceTestForBigCollection($num);
114-
echo $time . ' seconds' . PHP_EOL;
112+
[$time, $bytes] = $this->samples->runPerformanceTestForBigCollection($num);
113+
$bytes = number_format($bytes);
114+
echo "$time seconds ($bytes bytes used)." . PHP_EOL;
115115
}
116116

117117
/**

tests/Sample/EncodeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@ public function testDynamicSchemaExample()
295295
*/
296296
public function testPerformanceTestForSmallNestedResources()
297297
{
298-
$this->assertGreaterThan(0, $this->samples->runPerformanceTestForSmallNestedResources(10));
298+
$this->assertGreaterThan(0, $this->samples->runPerformanceTestForSmallNestedResources(10)[0]);
299299
}
300300

301301
/**
302302
* Test performance sample.
303303
*/
304304
public function testPerformanceTestForBigCollection()
305305
{
306-
$this->assertGreaterThan(0, $this->samples->runPerformanceTestForBigCollection(10));
306+
$this->assertGreaterThan(0, $this->samples->runPerformanceTestForBigCollection(10)[0]);
307307
}
308308

309309
/**

0 commit comments

Comments
 (0)