GithubHelp home page GithubHelp logo

mnapoli / bref-bootstrap-benchmarks Goto Github PK

View Code? Open in Web Editor NEW
22.0 10.0 3.0 110 KB

Benchmark of possible solutions for Bref and the new AWS Lambda runtime API

Home Page: https://github.com/mnapoli/bref/issues/100

Makefile 6.83% PHP 67.71% HTML 25.38% Shell 0.08%

bref-bootstrap-benchmarks's Introduction

For more up to date benchmarks check out github.com/brefphp/bref-benchmark.


This is a benchmark of possible solutions for Bref to run PHP on AWS Lambda.

The issue in Bref is here: mnapoli/bref#100.

I will use Symfony in the examples just for illustration but this applies to all frameworks. Jump at the end for the numbers.

Solutions

Solution A

We run the PHP code in the same process as bootstrap, very similarly to what can be done with ReactPHP/Amp/Aerys/Swoole...

This is very fast, both for cold starts and warm requests! We can get response times below 10ms with that. However, just like when using such frameworks outside of lambda we have disadvantages: the memory is shared between all requests. That means we can have memory leaks, we have to be careful about global state, etc. Also a fatal error will kill the whole lambda (a new lambda will be started by AWS but that means a new cold start).

This is a very interesting option that can be worth proposing as an option, but it cannot be the default solution that will work with all apps/frameworks.

Click here for code examples

Example of a bootstrap:

<?php
// ...
require __DIR__ . '/vendor/autoload.php';

// BOOT Symfony BEFORE a request comes in!
$kernel = new Kernel('prod', false);
$kernel->boot();
$symfonyAdapter = new SymfonyAdapter($kernel);

while (true) {
    $event = waitForEventFromLambdaApi(); // This is a blocking HTTP call until an event is available

    $request = RequestFactory::fromLambdaEvent($event);
    // REUSE the same Symfony Kernel, meaning fast response time!
    $response = $symfonyAdapter->handle($request);
    $lambdaResponse = LambdaResponse::fromPsr7Response($response);

    signalSuccessToLambdaApi($lambdaResponse);
}

Solution B

The bootstrap starts a sub-process (exec) every time an event needs to be processed.

That allows to protect the bootstrap process from failures of the children. This is basically what Bref does at the moment.

This is similar too to how PHP-FPM works (in the spirit at least).

Click here for code examples

Example of a bootstrap:

<?php
// ...
while (true) {
    $event = waitForEventFromLambdaApi(); // This is a blocking HTTP call until an event is available

    $process = new Process(['/opt/bin/php', 'index.php', /* pass the event as an argument */]);
    $process->run(); // This waits for the process to finish

    // [fetch response ...]

    signalSuccessToLambdaApi($lambdaResponse);
}

Example of a index.php that could be executed by that:

<?php
// ...
require __DIR__ . '/vendor/autoload.php';

// [fetch event from process args]

$kernel = new Kernel('prod', false);
$kernel->boot();
$symfonyAdapter = new SymfonyAdapter($kernel);
$request = RequestFactory::fromLambdaEvent($event);
$response = $symfonyAdapter->handle($request);
$lambdaResponse = LambdaResponse::fromPsr7Response($response);

// [return response to bootstrap somehow]

exit(0); // DIE!

Solution C

OBSOLETE See (#12)

Just like B except bootstrap does not handle events: it immediately executes a sub-process. The PHP sub-process will call the integration HTTP API and wait for an event. That means that we can run code before waiting for an event. E.g. we can bootstrap Composer's autoloader and Symfony before a request comes in!

Click here for code examples

Example of a bootstrap:

<?php
// ...
while (true) {
    $process = new Process(['/opt/bin/php', 'index.php']);
    $process->run(); // This waits for the process to finish (i.e. waits until an event has been processed)
}

Example of a index.php that could be executed by that:

<?php
// ...
require __DIR__ . '/vendor/autoload.php';

// BOOT Symfony BEFORE a request comes in!
$kernel = new Kernel('prod', false);
$kernel->boot();
$symfonyAdapter = new SymfonyAdapter($kernel);

$event = waitForEventFromLambdaApi(); // This is a blocking HTTP call until an event is available

$request = RequestFactory::fromLambdaEvent($event);
$response = $symfonyAdapter->handle($request);
$lambdaResponse = LambdaResponse::fromPsr7Response($response);

signalSuccessToLambdaApi($lambdaResponse);

exit(0); // DIE!

Solution D

How about instead of creating a new process we fork the bootstrap process? The app would bootstrap once in total, but still there is no shared state between events (because each event is processed by a fork).

Click here for code examples

Example of bootstrap:

<?php
// ...
require __DIR__ . '/vendor/autoload.php';
// BOOT Symfony ONLY ONCE for all the requests!
$kernel = new Kernel('prod', false);
$kernel->boot();
$symfonyAdapter = new SymfonyAdapter($kernel);

while (true) {
    $pid = pcntl_fork();
    if ($pid) { // Root process
        pcntl_wait($status); // Wait for the child to process the event
    } else {    // Child process
        // Here the autoloader is already loaded and Symfony initialized!
        $event = waitForEventFromLambdaApi(); // This is a blocking HTTP call until an event is available

        $request = RequestFactory::fromLambdaEvent($event);
        $response = $symfonyAdapter->handle($request);
        $lambdaResponse = LambdaResponse::fromPsr7Response($response);

        signalSuccessToLambdaApi($lambdaResponse);

        exit(0); // The fork DIES! The root process will resume its execution and loop
    }
}

Solution E

Solution E is about starting PHP-FPM and run it with only one PHP worker. The bootstrap would be responsible for forwarding Lambda events using the FastCGI protocol.

Solution F

Solution F is about starting the PHP built-in webserver. The bootstrap would be responsible for forwarding Lambda events to the webserver via HTTP.

Solution G

Solution G is about writing a custom PHP SAPI (in C) that is inspired from PHP-FPM and the built-in webserver. This SAPI is run by bootstrap and executes a PHP script in a loop, but resets the memory on every loop.

The PHP script would wait for the event, receive it, process it and send a response. The custom SAPI resets the memory every time.

This is basically like solution A except the memory is reset on every loop, meaning we keep the request isolation that exists in PHP since its beginning. It would also be better than solution B/C because by running everything in a single PHP process we avoid the overhead of booting a process for every event.

Solution H

Solution H is like solution C except it uses PHP CGI instead of PHP CLI. The bootstrap would be responsible for executing php-cgi on every event/request and forward the event data via the CGI protocol.

Results

Those are Lambda execution time (not HTTP response time because you would have to account API Gateway).

Solution Framework Average Minimum URL
LAMP stack PHP 1ms 0ms
LAMP stack Symfony 6ms 4ms
Bref 0.2 (baseline) PHP 21ms 15ms url
Bref 0.2 (baseline) Symfony 42ms 22ms url
A ☢ PHP 4ms 1ms url
A ☢ Symfony 6ms 2ms url
B
D ☢ PHP 11ms 6ms url
D ☢ Symfony 20ms 12ms url
E PHP 5ms 1ms url
E Symfony 18ms 11ms url
F ☢ PHP 4ms 1ms url
F ☢ Symfony 17ms 8ms url
G ☢ PHP 5ms 2ms url
G ☢ Symfony 8ms 5ms url
H PHP 18ms 10ms url
H Symfony 33ms 18ms url

☢: Experimental solution that does not guarantee the same level of stability as a classic LAMP stack.

The LAMP stack is a baseline of running the same code but on a classic server with Apache or Nginx. This will help compare performances between LAMP and PHP on Lambda.

Conclusion

The fastest stable runtime seems to be solution E, i.e. PHP-FPM.

The fastest experimental runtime seems to be solution A. However solution G might be a good alternative to A: it provides similar performances but keeps isolation between requests/events.

How to run

  • clone the repository
  • make install
  • go into a subdirectory and run make preview to test it locally (you'll need to install AWS SAM, the lambda will run in Docker automatically, try it out it's magic!)
  • run make deploy in a subdirectory to deploy that lambda

To deploy you will need to create a bucket and update the bucket name everywhere in the scripts. I also used the us-east-2 region because I don't have anything in that region so it's easy to delete everything afterwards. If you want to let that be configured by an env variable or some other config file send a pull request!

To benchmark: run ab -c 1 -n 100 <the url of the lambda> (check the URL responds correctly). Check out the execution time of the lambda in Cloudwatch.

The first time you deploy, if it fails, you will need to delete the stack in CloudFormation manually. This is how CloudFormation works.

bref-bootstrap-benchmarks's People

Contributors

barryvdh avatar bubba-h57 avatar mnapoli avatar staabm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bref-bootstrap-benchmarks's Issues

Coldstart overhead

We're measuring performance, which is interesting obviously, but perhaps we should also look at the (additional) bootstrap time.

Eg. the php webserver has a sleep of 1 sec, which can be significant if it's run not so often.
I'm not sure what realistic numbers are in the percentage of cold starts, but should we take them into account?

We could benchmark them using a fresh deploy with a higher concurrency, right?

So something like ab -c 100 -n 100 https://.. would spawn 100 fresh workers (after a new deploy), right?

Solution C is built on wrong assumptions

I thought that the duration of a lambda was the time spend between these events:

  • the lambda asks for an event to process (via the HTTP runtime API)
  • the lambda sends a response for the event (via the API again)

From what I've seen in some tests the duration is actually between:

  • the lambda asks for an event to process
  • the lambda asks for the next event to process

It doesn't matter if we return a response early and keep processing things. It also doesn't matter if we pre-load stuff before a request comes in, because this is actually time spent in the previous lambda execution…

With that in mind, solution C doesn't actually save any time by booting Symfony and then waiting for an event.

I'm not sure though that C should be removed/marked as obsolete as I believe it can still be faster than B (there is not the overhead of passing the event via CLI parameters). However I think opcache is not enabled in C so current results are not up to date (I'll open a separate issue for that).

Unable to Conenct to Socket

Hi,
Sometimes when we make a request the requests fails with a 502.

The Log show us that

Fatal error: Uncaught ErrorException: stream_socket_client(): unable to connect to unix:///tmp/php-fpm.sock (Connection refused) in /opt/vendor/hollodotme/fast-cgi-client/src/Socket.php:183
Stack trace:
#0 /opt/vendor/hollodotme/fast-cgi-client/src/Socket.php(191): hollodotme\FastCGI\Socket->handleFailedResource(111, 'Connection refu...')
#1 /opt/vendor/hollodotme/fast-cgi-client/src/Socket.php(162): hollodotme\FastCGI\Socket->connect()
#2 /opt/vendor/hollodotme/fast-cgi-client/src/Client.php(105): hollodotme\FastCGI\Socket->sendRequest(Object(class@anonymous))
#3 /opt/vendor/hollodotme/fast-cgi-client/src/Client.php(77): hollodotme\FastCGI\Client->sendAsyncRequest(Object(class@anonymous))
#4 /opt/bootstrap(161): hollodotme\FastCGI\Client->sendRequest(Object(class@anonymous))
#5 /opt/bootstrap(225): sendToApp('/v2/cd/32', Array, Object(hollodotme\FastCGI\Client))
#6 {main}

Next hollodotme\FastCGI\Exceptions\ConnectException: Unable to connect to FastCGI application: Connection refused in /opt/vendor/hollodotme/fast-cgi-c in /opt/vendor/hollodotme/fast-cgi-client/src/Socket.php on line 226

This Happens in Solutions E and H

I Will try to debug more and see if I can fix this problem

Solution E

Solution E currently states that it would be forwarding Lambda events to PHP-FPM via FastCGI. I wrote a nodejs shim last summer that is similar. However, it simply forwards directly to php-cgi are you looking for something similar to this nodejs code or is Solution E really about trying to leverage PHP-FPM?

/*jslint node: true */

const spawn = require("child_process").spawnSync;
const parser = require("http-string-parser");
var path = require("path");

exports.handler = function(event, context) {
    // Sets some sane defaults here so that this function doesn't fail
    // when it's not handling a HTTP request from API Gateway.
    var requestMethod = event.httpMethod || 'GET';
    var requestBody = event.body || '';
    var serverName = event.headers ? event.headers.Host : 'lambda_test.dev';
    var requestUri = event.path || '';
    var headers = {};
    var queryParams = '';

    // Convert all headers passed by API Gateway into the correct format for PHP CGI.
    // This means converting a header such as "X-Test" into "HTTP_X-TEST".
    if (event.headers) {
        Object.keys(event.headers).map(function (key) {
            headers['HTTP_' + key.toUpperCase().replace(/-/g, '_')] = event.headers[key];
            headers[key.toUpperCase().replace(/-/g, '_')] = event.headers[key];
        });
    }

    // Convert query parameters passed by API Gateway into the correct format for PHP CGI.
    if (event.queryStringParameters) {
        var parameters = Object.keys(event.queryStringParameters).map(function(key) {
            var obj = key + "=" + event.queryStringParameters[key];
            return obj;
        });
        queryParams = parameters.join("&");
    }

    // Spawn the PHP CGI process with a bunch of environment variables that describe the request.
    var scriptPath = path.resolve(__dirname + '/../../public/index.php')

    var php = spawn('php-cgi', ['-f', scriptPath], {
        env: Object.assign({
            REDIRECT_STATUS: 200,
            REQUEST_METHOD: requestMethod,
            SCRIPT_FILENAME: scriptPath,
            SCRIPT_NAME: '/index.php',
            PATH_INFO: '/',
            SERVER_NAME: serverName,
            SERVER_PROTOCOL: 'HTTP/1.1',
            REQUEST_URI: requestUri,
            QUERY_STRING: queryParams,
            AWS_LAMBDA: true,
            CONTENT_LENGTH: Buffer.byteLength(requestBody, 'utf-8')
        }, headers, process.env),
        input: requestBody
    });

    // When the process exists, we should have a compvare HTTP response to send back to API Gateway.
    var parsedResponse = parser.parseResponse(php.stdout.toString('utf-8'));

    // Signals the end of the Lambda function, and passes the provided object back to API Gateway.
    context.succeed({
        statusCode: parsedResponse.statusCode || 200,
        headers: parsedResponse.headers,
        body: parsedResponse.body
    });
};

Opcache not enabled

When I'm calling opcache_get_status() I get: Call to undefined function opcache_get_status(). When checking phpinfo() it shows that opcache is enabled in the compilation, but did you also include the opcache.so extension? And it should be enabled + enabled for cli + validate timestamps should be disabled.

Enable the Twig cache

Currently the Twig cache is disabled in the Symfony test. This doesn't allow to compare with actual Symfony performances outside of AWS Lambda, this should be fixed.

Opcache is not enabled in C

$process = new Process(['/opt/bin/php', 'index.php']);

The php.ini is not provided here.

I think what would be great is compile PHP so that php.ini is automatically loaded, rather than having to provide it explicitly every time. It would also prevent any mistake in PHP scripts running sub-processes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.