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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
/.github export-ignore
/phpstan.neon export-ignore
/phpstan-baseline.neon export-ignore
/NOTICE export-ignore
/README.md export-ignore
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"autoload-dev": {
"psr-4": {
"Neomerx\\Tests\\JsonApi\\": "tests/",
"Neomerx\\Samples\\JsonApi\\": "sample/"
"Neomerx\\Samples\\JsonApi\\": "sample/",
"JsonApiPhp\\JsonApi\\Perf\\": "perf/src"
}
},
"config": {
Expand All @@ -74,6 +75,8 @@
"test-cs-fixer": "./vendor/bin/php-cs-fixer fix --diff --dry-run -v",
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
"test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text"
"test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"bench-01": "docker compose -f ./perf/docker-compose.yml run --rm cli_php blackfire --samples 10 run php -d zend.assertions=-1 01-encode-simple-single-resource.php",
"bench-02": "docker compose -f ./perf/docker-compose.yml run --rm cli_php blackfire --samples 10 run php -d zend.assertions=-1 02-encode-long-list-of-simple-resources.php"
}
}
18 changes: 18 additions & 0 deletions perf/01-encode-simple-single-resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use JsonApiPhp\JsonApi\Perf\City;
use JsonApiPhp\JsonApi\Perf\CitySchema;
use Neomerx\JsonApi\Encoder\Encoder;

require_once \dirname(__DIR__) . '/vendor/autoload.php';

$data = new City(1, 'Warsaw');
$encoder = Encoder::instance(
[
City::class => CitySchema::class,
]
);
$encoder->encodeData($data);
21 changes: 21 additions & 0 deletions perf/02-encode-long-list-of-simple-resources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use JsonApiPhp\JsonApi\Perf\City;
use JsonApiPhp\JsonApi\Perf\CitySchema;
use Neomerx\JsonApi\Encoder\Encoder;

require_once \dirname(__DIR__) . '/vendor/autoload.php';

$data = \array_map(
static fn(int $id): City => new City($id, 'Warsaw'),
\range(1, 1000),
);
$encoder = Encoder::instance(
[
City::class => CitySchema::class,
]
);
$encoder->encodeData($data);
21 changes: 12 additions & 9 deletions perf/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
FROM php:7.3-cli

ARG DEBIAN_FRONTEND=noninteractive
FROM php:8.1.7-cli-alpine

RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
&& architecture=$(uname -m) \
&& curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/alpine/$architecture/$version \
&& mkdir -p /tmp/blackfire \
&& curl -A "Docker" -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version | tar zxp -C /tmp/blackfire \
&& curl -A "Docker" -L -s https://blackfire.io/api/v1/releases/client/linux_static/amd64 | tar zxp -C /tmp/blackfire \
&& tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get ('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8307\n" > $PHP_INI_DIR/conf.d/blackfire.ini \
&& rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz

RUN mkdir -p /tmp/blackfire \
&& architecture=$(uname -m) \
&& curl -A "Docker" -L https://blackfire.io/api/v1/releases/cli/linux/$architecture | tar zxp -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire /usr/bin/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > /usr/local/etc/php/conf.d/blackfire.ini \
&& rm -Rf /tmp/blackfire \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
&& rm -Rf /tmp/blackfire
28 changes: 16 additions & 12 deletions perf/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
version: '3.7'

services:
cli_php:
build:
context: ./
dockerfile: Dockerfile
container_name: cli_php_json_api_blackfire
env_file: blackfire.io.env
volumes:
- type: bind
source: ./../
target: /app
working_dir: /app
tty: true
cli_php:
build:
context: ./
dockerfile: Dockerfile
container_name: cli_php_json_api_blackfire
env_file: 'blackfire.io.env'
volumes:
- ./../:/app:ro
working_dir: /app/perf/
stop_grace_period: 1s
command: >
sh -c "
mkdir -p /var/log/php && \
touch /var/log/php/error.log && \
tail -f /var/log/php/error.log
"
27 changes: 27 additions & 0 deletions perf/src/City.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Perf;

final class City
{
private int $id;
private string $name;

public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}

public function id(): int
{
return $this->id;
}

public function name(): string
{
return $this->name;
}
}
35 changes: 35 additions & 0 deletions perf/src/CitySchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Perf;

use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\BaseSchema;

final class CitySchema extends BaseSchema
{
public function getType(): string
{
return 'cities';
}

/** @param City $resource */
public function getId($resource): ?string
{
return (string) $resource->id();
}

/** @param City $resource */
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'name' => $resource->name(),
];
}

public function getRelationships($resource, ContextInterface $context): iterable
{
return [];
}
}