Mastering The Faster Web with PHP,MySQL,and JavaScript
上QQ阅读APP看书,第一时间看更新

Performance testing with Blackfire.io

Before we start, please note that this feature is available only to premium and enterprise users and that, therefore, it requires a paid subscription.

In order to automate performance testing, we will start by creating a very simple blackfire.yml file in our repository. This file will contain our tests. A test should be composed of a name, a regular expression and a set of assertions. It is always preferable to avoid creating volatile time tests as these make for very brittle tests that might yield very different results from one profiling session to the next. Examples of strong performance tests would be to check CPU or memory consumption, number of SQL queries or testing results by profile comparisons. In our case, we will create a very basic and volatile time test just for the sake of giving a short and simple example. Here is the content of our .blackfire.yml file:

tests: 
    "Pages should be fast enough": 
        path: "/.*" # run the assertions for all HTTP requests 
        assertions: 
            - "main.wall_time < 10ms" # wall clock time is less than 10ms 

The final step would be to integrate this performance test with a continuous integration tool. To select the tool of your choice, please consult the documentation at the following URL: https://blackfire.io/docs/integrations/index.

In our case, we will integrate with Travis CI. To do so, we must create two files. One will include our credentials and must be encrypted (.blackfire.travis.ini.enc). The other will include our Travis instructions (.travis.yml).

Here is the content of our .blackfire.travis.ini file before encryption (replace the credentials with your own):

[blackfire] 
 
server-id=BLACKFIRE_SERVER_ID 
server-token=BLACKFIRE_SERVER_TOKEN 
client-id=BLACKFIRE_CLIENT_ID 
client-token=BLACKFIRE_CLIENT_TOKEN 
endpoint=https://blackfire.io/ 
collector=https://blackfire.io/ 

This file must then be encrypted before being committed to your repository. To do so, please issue the following commands inside the Linux for PHP container:

# gem install travis
# travis encrypt-file /srv/www/.blackfire.travis.ini -r [your_Github_repository_name_here]

Here is the content of our .travis.yml file:

language: php 
 
matrix: 
    include: 
        - php: 5.6 
        - php: 7.0 
          env: BLACKFIRE=on 
 
sudo: false 
 
cache: 
    - $HOME/.composer/cache/files 
 
before_install: 
    - if [[ "$BLACKFIRE" = "on" ]]; then 
        openssl aes-256-cbc -K [ENCRYPT_KEY_HERE] -iv [ENCRYPT_IV_HERE] -in .blackfire.travis.ini.enc -out ~/.blackfire.ini -d 
        curl -L https://blackfire.io/api/v1/releases/agent/linux/amd64 | tar zxpf - 
        chmod 755 agent && ./agent --config=~/.blackfire.ini --socket=unix:///tmp/blackfire.sock & 
      fi 
 
install: 
    - travis_retry composer install 
 
before_script: 
    - phpenv config-rm xdebug.ini || true 
    - if [[ "$BLACKFIRE" = "on" ]]; then 
        curl -L https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$(php -r "echo PHP_MAJOR_VERSION . PHP_MINOR_VERSION;")-zts | tar zxpf - 
        echo "extension=$(pwd)/$(ls blackfire-*.so | tr -d '[[:space:]]')" > ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/blackfire.ini 
        echo "blackfire.agent_socket=unix:///tmp/blackfire.sock" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/blackfire.ini 
      fi 
 
script: 
    - phpunit 

Once committed, this configuration will ensure that the performance tests will run on each git push to your Github repository. Thus, performance becomes a feature and is continuously tested like any other of your application's features. The next step is to monitor your code's performance after deployment on a production server. Let's discover some of the available tools in order to do so.