Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProfilerOutputStats
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 log
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7use MediaWiki\MediaWikiServices;
8
9/**
10 * Flush profiling data to StatsD.
11 *
12 * @ingroup Profiler
13 * @since 1.25
14 */
15class ProfilerOutputStats extends ProfilerOutput {
16
17    /**
18     * Flush profiling data to the current profiling context's stats buffer.
19     *
20     * @param array[] $stats
21     */
22    public function log( array $stats ) {
23        $prefix = $this->params['prefix'] ?? '';
24        $statsFactory = MediaWikiServices::getInstance()->getStatsFactory();
25        $posCounter = $statsFactory->getCounter( 'ProfilerOutputStats_calls_total' );
26        $posCpuTimer = $statsFactory->getTiming( 'ProfilerOutputStats_cpu_seconds' );
27        $posRealTimer = $statsFactory->getTiming( 'ProfilerOutputStats_real_seconds' );
28
29        foreach ( $stats as $stat ) {
30            $key = $prefix ? "{$prefix}_{$stat['name']}" : "{$stat['name']}";
31
32            // Convert fractional seconds to whole milliseconds
33            $cpu = round( $stat['cpu'] * 1000 );
34            $real = round( $stat['real'] * 1000 );
35
36            $posCounter->setLabel( 'key', $key )->increment();
37            $posCpuTimer->setLabel( 'key', $key )->observe( $cpu );
38            $posRealTimer->setLabel( 'key', $key )->observe( $real );
39        }
40    }
41}