Improve D8 performance to access a custom REST controller Context

I'm currently upgrading a site from Drupal 7 to Drupal 8.

In the D7 version, there was a custom "proxy" module which served back-end services. Requests starting with a /service prefix, such as http://my-drupal-domain/service/service-name?service-args, were redirected to a custom index.php inside a D7 module. This alternative index.php called the minimal Drupal code needed : includes and drupal_bootstrap(). Then, the custom module's function handled the request, sometimes using Drupal native functions (to manipulate users for example) and passing it to the target service. Finally, it returned the service response to the client.

Now I want to migrate this to Drupal 8.

I managed to code a custom REST controller, using a custom route :

my_module.rest_services:
  path: '/service/{endpoint}'
  defaults:
    _controller: '\Drupal\my_module\Controller\ProxyController::index'
  methods: [GET, POST, PUT, DELETE]
  requirements:
    _access: 'TRUE'


The ProxyController, which extends Drupal\Core\Controller\ControllerBase, is basically the same code that handled the requests in the previous site, converted to the OOP-way of D8.

Initial problem

My problem is that, using D8 routes and Symfony2 components, the response time for any request increase proportionally with the number of concurrent users compared to the D7 version. For 20 users, this latency is +1s with D8 than with a D7 minimal bootstrap... That's huge! (see chart below, for the exact same call to D7 with a custom minimal bootstrap and D8).



Is there a way to bypass some of D8 code to redirect requests directly to my controller ? Some sort of "minimal D8 bootstrap".

Or a way to improve D8 routes performance ?

Edit: need to cache file parsing

I just discovered Drupal 8 is not the only responsible for my performance loss! during the request handle process, in my module's code, I read and parse a YAML configuration file, using file_get_contents() and Drupal\Component\Serialization\Yaml::decode(). But Drupal does not cache the value by default : the file is read and parsed on each call. With 20 concurrent users, it can take up to 600ms...

I need to find a way to tell D8 "Read this config file once, parse it and cache the content so you don't have to read it again unless your cache is cleared".
Given I did not have many responses, I answer myself to share the bits of explanation and solution I found.

Below the performance loss sources I've identified :


The use of D8 full bootstrap, including modules loading, routes resolving, etc.
Reading and parsing a YAML config file each time the module handles a request


And the solutions I applied :

Custom D8 minimal bootstrap

Based on this post and the default D8 index.php, I created a custom Bootstrap in my proxy module :

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\my_module\Controller\ProxyController;

chdir( $_SERVER['DOCUMENT_ROOT'] );

$autoloader = require_once 'autoload.php';

$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel-&gt;boot();
$container = $kernel-&gt;getContainer();

$response = ProxyController::create( $container )-&gt;index( $request );
$response-&gt;send();

$kernel-&gt;terminate($request, $response);


Like it was made in the D7 version, I redirected all request with the /service prefix to this file.

PHP config instead of YAML

I didn't find a way to load and parse the YAML config file once and cache the result.
(I'm still interested if someone know how to do that by the way)

So i converted the config file content myself into PHP, put it into a .php file and
use include instead of file_get_contents() and Yaml::decode().

Result

You can see here the response time obtained combining the 2 solutions above.



I assume the remaining perf loss is due to the use of OOP instead of procedural PHP. But if I want to stick to the D8 standards, I can't do much about it...