Metrics

A metric is a collection of costs (memory, network, CPU, ...) associated with some function calls. Blackfire offers some built-in metrics, but you can also create your own custom metrics.

You can define the performance expectations of your projects by setting an expected metric value, on a given metric dimension, in assertions. To write an assertion on a specific dimension, just append the dimension name to the metric name, seperated with a ..

For instance, for the sql.queries metric, the number of calls is stored in the metrics.sql.queries.count and the memory usage in metrics.sql.queries.peak_memory. To check that you don't have more than 10 SQL queries in an assertion, you would write metrics.sql.queries.count <= 10.

The available dimensions for metrics are the following ones.

  • count
  • wall_time
  • cpu_time
  • memory
  • peak_memory
  • io
  • network_in
  • network_out

Built-in Metrics

Here is the list of built-in metrics that are always available in assertions:

  • main
  • metrics.amqp
  • metrics.amqp.config
  • metrics.amqp.connections
  • metrics.amqp.messages
  • metrics.amqp.phpamqp.config
  • metrics.amqp.phpamqp.connections
  • metrics.amqp.phpamqp.messages
  • metrics.amqp.phpamqplib.config
  • metrics.amqp.phpamqplib.connections
  • metrics.amqp.phpamqplib.messages
  • metrics.api_platform.installed
  • metrics.api_platform2.installed
  • metrics.assetic.controller.calls
  • metrics.aura.installed
  • metrics.cache
  • metrics.cache.delete
  • metrics.cache.flush
  • metrics.cache.get
  • metrics.cache.has
  • metrics.cache.set
  • metrics.code_igniter.installed
  • metrics.compile
  • metrics.composer.autoload.find_file
  • metrics.crypto
  • metrics.crypto.mcrypt
  • metrics.cs_cart.installed
  • metrics.doctrine.annotations.parsed
  • metrics.doctrine.annotations.read
  • metrics.doctrine.cache.delete
  • metrics.doctrine.cache.flush
  • metrics.doctrine.cache.get
  • metrics.doctrine.cache.has
  • metrics.doctrine.cache.set
  • metrics.doctrine.dql.parsed
  • metrics.doctrine.entities.created
  • metrics.doctrine.entities.hydrated
  • metrics.doctrine.entities.metadata
  • metrics.doctrine.event_manager.dispatch
  • metrics.doctrine.orm.flush
  • metrics.doctrine.proxies.generated
  • metrics.doctrine.proxies.loaded
  • metrics.drupal8.block.access
  • metrics.drupal8.cache.null
  • metrics.drupal8.cache.views
  • metrics.drupal8.cron.drush
  • metrics.drupal8.cron.requested
  • metrics.drupal8.cron.run
  • metrics.drupal8.dblog
  • metrics.drupal8.drush
  • metrics.drupal8.entity_load.content
  • metrics.drupal8.fast_cache
  • metrics.drupal8.installed
  • metrics.drupal8.module.loaded.contrib
  • metrics.drupal8.page_cache.construct
  • metrics.drupal8.php.load
  • metrics.drupal8.preprocess.css
  • metrics.drupal8.preprocess.js
  • metrics.drupal8.twig.debug
  • metrics.elasticsearch
  • metrics.elasticsearch.elastica.logs
  • metrics.elasticsearch.elastica.queries
  • metrics.elasticsearch.elasticsearch.queries
  • metrics.elasticsearch.queries
  • metrics.emails
  • metrics.emails.sent
  • metrics.emails.sent.mandrill
  • metrics.emails.sent.php
  • metrics.emails.sent.php_mailer
  • metrics.emails.sent.swiftmailer
  • metrics.fos_elastica_bundle
  • metrics.fos_http_cache_bundle
  • metrics.fos_message_bundle
  • metrics.fos_rest_bundle
  • metrics.fos_user_bundle
  • metrics.graph_ql
  • metrics.http.curl.requests
  • metrics.http.php_stream.requests
  • metrics.http.requests
  • metrics.http.socket.requests
  • metrics.hwi_oauth_bundle
  • metrics.jms.serializer.deserialize
  • metrics.jms.serializer.serialize
  • metrics.kohana.installed
  • metrics.laravel
  • metrics.laravel.database.hydration
  • metrics.laravel.debug_bar.enable
  • metrics.laravel.debug_bar.middleware
  • metrics.laravel.event_dispatcher
  • metrics.laravel.installed
  • metrics.laravel.kernel.bootstrap
  • metrics.laravel.middleware.add_queued_cookies_to_response
  • metrics.laravel.middleware.authenticate
  • metrics.laravel.middleware.authenticate_basic
  • metrics.laravel.middleware.authorize
  • metrics.laravel.middleware.check_for_maintenance
  • metrics.laravel.middleware.csrf
  • metrics.laravel.middleware.encrypt_cookie
  • metrics.laravel.middleware.session
  • metrics.laravel.middleware.share_errors
  • metrics.laravel.middleware.substiture_bindings
  • metrics.laravel.middleware.throttle_requests
  • metrics.laravel.router.dispatch
  • metrics.laravel.router.prepare_response
  • metrics.laravel.router.route_run
  • metrics.laravel.view.render
  • metrics.laravel.workbench_starter
  • metrics.logs.browser
  • metrics.logs.log_call
  • metrics.magento1.all.installed
  • metrics.magento1.ee.installed
  • metrics.magento2.all.installed
  • metrics.magento2.ee.installed
  • metrics.main
  • metrics.markdown
  • metrics.markdown.parse
  • metrics.markdown.parse.parsedown
  • metrics.memcache
  • metrics.memcache.connection
  • metrics.memcache.queries
  • metrics.memcached
  • metrics.memcached.queries
  • metrics.michelf.parse.parsedown
  • metrics.mongodb
  • metrics.mongodb.config
  • metrics.mongodb.connections
  • metrics.mongodb.queries
  • metrics.monolog.browser_console
  • metrics.monolog.calls
  • metrics.monolog.chromephp
  • metrics.monolog.firephp
  • metrics.monolog.php_console
  • metrics.nette.installed
  • metrics.output
  • metrics.oxid.rendered.article.widgets
  • metrics.oxid.rendered.categorytree
  • metrics.oxid.seo.link.generate
  • metrics.oxid.seo.link.uncached
  • metrics.pear.firephp
  • metrics.pear.log.log_call
  • metrics.php
  • metrics.php.autoloaded
  • metrics.php.eval
  • metrics.php.gc
  • metrics.php.shell_exec
  • metrics.php_markdown.parse.parsedown
  • metrics.phpfastcache.cache.delete
  • metrics.phpfastcache.cache.flush
  • metrics.phpfastcache.cache.get
  • metrics.phpfastcache.cache.has
  • metrics.phpfastcache.cache.set
  • metrics.propel2
  • metrics.psr3.calls
  • metrics.redis
  • metrics.redis.config
  • metrics.redis.connections
  • metrics.redis.phpredis.config
  • metrics.redis.phpredis.connections
  • metrics.redis.phpredis.queries
  • metrics.redis.predis.connections
  • metrics.redis.predis.queries
  • metrics.redis.queries
  • metrics.riak
  • metrics.riak.basho.queries
  • metrics.riak.php_riak.queries
  • metrics.riak.queries
  • metrics.run
  • metrics.serializer
  • metrics.serializer.deserialize
  • metrics.serializer.serialize
  • metrics.shopware.events
  • metrics.shopware.filtered_events
  • metrics.shopware.get_resources
  • metrics.shopware.link_generations
  • metrics.shopware.rendering
  • metrics.shopware.routing
  • metrics.silex.installed
  • metrics.slim.installed
  • metrics.smarty
  • metrics.smarty.compile
  • metrics.smarty.is_cached
  • metrics.smarty.read_file
  • metrics.smarty.template_base
  • metrics.solarium.queries
  • metrics.sql
  • metrics.sql.connections
  • metrics.sql.db2.connections
  • metrics.sql.db2.queries
  • metrics.sql.msql.connections
  • metrics.sql.msql.queries
  • metrics.sql.mssql.connections
  • metrics.sql.mssql.queries
  • metrics.sql.mysql.connections
  • metrics.sql.mysql.queries
  • metrics.sql.mysqli.connections
  • metrics.sql.mysqli.queries
  • metrics.sql.oci.connections
  • metrics.sql.oci.queries
  • metrics.sql.odbc.connections
  • metrics.sql.odbc.queries
  • metrics.sql.pdo.connections
  • metrics.sql.pdo.queries
  • metrics.sql.postgres.connections
  • metrics.sql.postgres.queries
  • metrics.sql.queries
  • metrics.sql.sqlite.connections
  • metrics.sql.sqlite.queries
  • metrics.sql.sqlsrv.connections
  • metrics.sql.sqlsrv.queries
  • metrics.sql.sybase.connections
  • metrics.sql.sybase.queries
  • metrics.symfony.authentication_provider_manager
  • metrics.symfony.config_check
  • metrics.symfony.console
  • metrics.symfony.console.application_run
  • metrics.symfony.controllers
  • metrics.symfony.controllers.action
  • metrics.symfony.controllers.argument_resolver
  • metrics.symfony.controllers.controller_resolver
  • metrics.symfony.debug
  • metrics.symfony.events
  • metrics.symfony.firewall
  • metrics.symfony.http_cache
  • metrics.symfony.kernel
  • metrics.symfony.kernel.boot
  • metrics.symfony.kernel.debug
  • metrics.symfony.kernel.handle
  • metrics.symfony.kernel.initialize_bundles
  • metrics.symfony.kernel.initialize_container
  • metrics.symfony.kernel.load_class_cache
  • metrics.symfony.kernel.shutdown
  • metrics.symfony.kernel.terminate
  • metrics.symfony.logger.console
  • metrics.symfony.logger.monolog_bridge
  • metrics.symfony.param_converter
  • metrics.symfony.process.cmds
  • metrics.symfony.processes
  • metrics.symfony.routing.loader
  • metrics.symfony.routing.route_collection
  • metrics.symfony.routing.router
  • metrics.symfony.security.access_decision_manager
  • metrics.symfony.security.authorization_checker
  • metrics.symfony.serializer.decode
  • metrics.symfony.serializer.denormalize
  • metrics.symfony.serializer.deserialize
  • metrics.symfony.serializer.encode
  • metrics.symfony.serializer.normalize
  • metrics.symfony.serializer.serialize
  • metrics.symfony.session_listener
  • metrics.symfony.subrequests
  • metrics.symfony.yaml.reads
  • metrics.symfony_framework
  • metrics.threshold
  • metrics.twig.attributes.ext.get
  • metrics.twig.attributes.get
  • metrics.twig.attributes.php.get
  • metrics.twig.compile
  • metrics.twig.display
  • metrics.twig.render
  • metrics.typo3.application_run
  • metrics.typo3.cache.flush
  • metrics.typo3.cache.get
  • metrics.typo3.cache.has
  • metrics.typo3.cache.remove
  • metrics.typo3.cache.set
  • metrics.typo3.cache.set_cache
  • metrics.typo3.content_object_renderer
  • metrics.typo3.controller
  • metrics.typo3.dispatcher
  • metrics.typo3.load.configuration
  • metrics.typo3.load.ext_loaded
  • metrics.typo3.load.extension_tables
  • metrics.typo3.load.table_configuration_array_load
  • metrics.typo3.logger
  • metrics.typo3.mvc_dispatcher
  • metrics.typo3.sql.count
  • metrics.typo3.sql.delete
  • metrics.typo3.sql.insert
  • metrics.typo3.sql.select
  • metrics.typo3.sql.truncate
  • metrics.typo3.sql.update
  • metrics.woocommerce.installed
  • metrics.wordpress
  • metrics.wordpress.apply_filters
  • metrics.wordpress.do_action
  • metrics.wordpress.get_footer
  • metrics.wordpress.get_header
  • metrics.wordpress.get_sidebar
  • metrics.wordpress.get_the_excerpt
  • metrics.wordpress.i18n.import_i18n
  • metrics.wordpress.load_all_options
  • metrics.wordpress.load_template
  • metrics.wordpress.rewrite_flush_rules
  • metrics.wordpress.wp_cache_delete
  • metrics.wordpress.wp_cache_flush
  • metrics.wordpress.wp_cache_get
  • metrics.wordpress.wp_cache_set
  • metrics.wordpress.wp_head
  • metrics.wordpress.wp_nav_menu
  • metrics.wordpress.wp_print_footer_scripts
  • metrics.wordpress.wp_print_head_scripts
  • metrics.wordpress.wp_print_styles
  • metrics.xdebug
  • metrics.yii.installed
  • metrics.zend.config_items
  • metrics.zend.front_controller
  • metrics.zend.log.chromephp
  • metrics.zend.log.firephp
  • metrics.zend.log.log_call
  • metrics.zend.reflection
  • metrics.zend.resources
  • metrics.zend.routes
  • metrics.zend.search.lucene.find
  • metrics.zend.table_metadata
  • metrics.zend.view_renderer

Magento Built-in Metrics

Magento Metrics are only available to Blackfire's Magento Add-on subscribers.

The following metrics are Magento specific and are only available to Magento Enterprise Cloud for Business and Blackfire's Magento Add-on subscribers.

  • metrics.magento1.action.catalog_category_view
  • metrics.magento1.action.catalog_product_view
  • metrics.magento1.action.checkout_cart_index
  • metrics.magento1.action.cms_index_index
  • metrics.magento1.action.cms_page_view
  • metrics.magento1.block.cache.load.get_cache_key
  • metrics.magento1.collection.cache.unserialize
  • metrics.magento1.collection.load
  • metrics.magento1.collection.product.load
  • metrics.magento1.config.read
  • metrics.magento1.controller.checkout_onepage
  • metrics.magento1.controller.customer_account
  • metrics.magento1.customer.logged_in
  • metrics.magento1.events
  • metrics.magento1.layout.update.save_cache
  • metrics.magento1.model.load
  • metrics.magento1.model.product.load
  • metrics.magento1.translate.cache.unserialize
  • metrics.magento2.all.action.catalog_category_view
  • metrics.magento2.all.action.catalog_product_view
  • metrics.magento2.all.action.checkout_cart_index
  • metrics.magento2.all.action.cms_index_index
  • metrics.magento2.all.action.cms_page_view
  • metrics.magento2.all.cache.any.save
  • metrics.magento2.all.cache.block.save.action
  • metrics.magento2.all.cache.block.save.call
  • metrics.magento2.all.cache.clean.action
  • metrics.magento2.all.cache.clean.call
  • metrics.magento2.all.cache.ddl.read
  • metrics.magento2.all.cache.eav.read
  • metrics.magento2.all.cache.full_page.builtin.hit
  • metrics.magento2.all.cache.layout.read
  • metrics.magento2.all.cache.reflection.read
  • metrics.magento2.all.cache.translate.read
  • metrics.magento2.all.collection.load
  • metrics.magento2.all.collection.product.load
  • metrics.magento2.all.config.css.merge_css_files.enabled
  • metrics.magento2.all.config.di.read
  • metrics.magento2.all.config.js.enable_js_bundling.enable
  • metrics.magento2.all.config.js.merge_js_files.enabled
  • metrics.magento2.all.controller.checkout
  • metrics.magento2.all.controller.customer_account
  • metrics.magento2.all.frontend.blocks.load_cache.hit
  • metrics.magento2.all.frontend.blocks.to_html
  • metrics.magento2.all.frontend.blocks.with_lifetime
  • metrics.magento2.all.model.cms_page.load
  • metrics.magento2.all.model.load
  • metrics.magento2.all.model.product.load
  • metrics.magento2.events

Custom Metrics

By default, Blackfire gives access to a large number of built-in metrics, most of them being specific to the language or popular Open-Source libraries. Based on project requirements, custom metrics can also be defined for a project.

Let's create the cache.write metric. It will aggregate the costs of the Cache::write() calls, an hypothetical class of my project that is memory intensive.

Besides a name (cache.write), a metric must be associated with some function calls, via a metric selector; here we want to match all calls to Cache::write(), so the selector would be =Cache::write.

Custom metrics can be used in assertions like any other metrics: the name is made of the metrics. prefix, then the metric name (cache.write in our example), and it ends with one of the available dimensions (.peak_memory in the example):

1
"metrics.foo.bar.peak_memory < 10mb"

Note

As a matter of fact, built-in metrics are defined in the exact same way as custom ones.

Custom metrics can be defined in a .blackfire.yml file or via the PHP SDK.

Defining a Metric Selector

Defining a metric is all about selecting a sub-set of the profile function calls. A selector first character defines how to interpret the rest of the expression:

  • =: Matches the expression exactly;
  • ^: Matches everything starting with the expression;
  • /: Interprets the expression as a regex that should match;
  • !: Interprets the expression as a regex that should not match;

The second part of the selector defines which function calls to match.

Here are some selector examples:

  • =Class::method: Matches the method function calls on Class;
  • =func: Matches the func function calls;
  • ^ArrayObject::: Matches all function calls from the ArrayObject class;
  • ^exif_: Matches all function calls for which the function starts with exif_;
  • /^Class::(method1|method2)$/: Matches method1 and method2 calls from Class;
  • !^spl_autoload_call$!: Matches any function/method calls but spl_autoload_call ones.

Capturing Arguments

Besides matching nodes, a metric can also be used to gather function call arguments (to be precise, you can get at most one argument for a function call).

Blackfire aggregates all calls to a function into one node to make it easy to reason about resources consumed by it. But sometimes, begin able to have different nodes for a function call depending on one argument can help if the argument makes the function behave very differently (think of a database function call that is very sensitive to the SQL statement that needs to be executed for instance).

When defining argument capturing, you determine which argument you want to capture (1-based index) and when to capture it (^ means that Blackfire needs to always get the argument; ^http would only get the argument if it starts with the http string, all other calls would be aggregated into one node).

Argument capturing is only available in .blackfire.yml file.

Tip

Arguments are displayed in the function call table and in the call graph. As Blackfire displays separate nodes for each unique argument, it's also a great way to better understand how the code behaves (for instance, one node per event for an event dispatcher handle method instead of one big node for all events).