interfaz centos7 + php7.4 API se recogió overhead montado xhprof

xhprof es una muy buena herramienta PHP perfilador Facebook 09 fuera Nian, pero más tarde se trasladó a Facebook hhvm, ya no se mantiene, tiene muchos bug en PHP7.

Tal como se usa en este documento, es alguien que lo haga en PHP7 proyecto github de xprof compatibles.

instalar

cd ~/source
git clone https://github.com/longxinH/xhprof
cd xhprof/extension/

Encuentra ubicación php-config

which php-config

instalar el tapón

phpize
./configure --with-php-config=/usr/local/php/bin/php-config  --enable-xhprof
make
make install

Modificar php.ini, php --ini que en primer lugar encontrar un archivo php.ini ubicación, y luego añadió en la siguiente:

[xhprof]
extension=xhprof.so;
xhprof.output_dir=/var/tmp/xhprof

En donde xhprof.output_dir es xhprof directorio de salida generará un método de archivo save_run xhprof run_id.project_name.xhprof cada ejecución. Este directorio no importa dónde está.

Reinicio php-FPM

service php-fpm restart
php -m | grep xhprof

En este punto se puede ver que, xhprof extensión se ha instalado con éxito

acceso a la configuración nginx

Cuando se genera el archivo .xhprof, podemos utilizar xhprof_lib para mostrar los resultados.

server {
    listen 80;
    root /var/www/html/xhprof/xhprof_html;
    server_name your_host;
    location = / {
        index index.php;
    }
    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

El xhprof_lib XHProf y los dos carpetas a xhprof_html / var / www / html / xhprof / bajar, a continuación, visite http: //your_host/xhprof_html/index.php puede verse como se muestra en el archivo de análisis de datos generado .

Guardar la ruta del archivo para la configuración de la parte superior:

uso

método uno

Utilizando el código siguiente para ser envuelto puede comprobar el código:

<?php
//header
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
 
// 要检查性能的代码


//footer
$xhprof_data = xhprof_disable();
include_once  '/var/www/html/xhprof/xhprof_lib/utils/xhprof_lib.php';
include_once  '/var/www/html/xhprof/xhprof_lib/utils/xhprof_runs.php';
$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, 'test');//test为生成文件内包含的一个关键字区分哪个接口的,可自定义

También pueden ser separados antes y después de que el código escrito en PHP archivo separado: header.php y footer.php

// header.php
<?php
if (extension_loaded('xhprof')) {
    include_once 'xhprof_lib/utils/xhprof_lib.php';
    include_once 'xhprof_lib/utils/xhprof_runs.php';
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
?>
// footer.php
<?php
if (extension_loaded('xhprof')) {
    $profiler_namespace = 'your_project';
    $xhprof_data = xhprof_disable();
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
}
?>

Los dos archivos y luego envolver el código de la siguiente manera:

<?php
include_once  '/var/www/html/xhprof/header.php';

// 要检查性能的代码

include_once  '/var/www/html/xhprof/footer.php';

Ejemplo:

include_once  '/var/www/html/xhprof/header.php';

function hallo() {
}

function simpleucall($n) {
  for ($i = 0; $i < $n; $i++)
    hallo();
}

function simpleudcall($n) {
  for ($i = 0; $i < $n; $i++)
    hallo2();
}

function hallo2() {
}

function simpleicall($n) {
  for ($i = 0; $i < $n; $i++)
    func_num_args();
}

class Foo {
    static $a = 0;
    public $b = 0;
    const TEST = 0;

    static function read_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = self::$a;
        }
    }

    static function write_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            self::$a = 0;
        }
    }

    static function isset_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = isset(self::$a);
        }
    }

    static function empty_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = empty(self::$a);
        }
    }

    static function f() {
    }

    static function call_static($n) {
        for ($i = 0; $i < $n; ++$i) {
            self::f();
        }
    }

    function read_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = $this->b;
        }
    }

    function write_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b = 0;
        }
    }

    function assign_add_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b += 2;
        }
    }

    function pre_inc_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            ++$this->b;
        }
    }

    function pre_dec_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            --$this->b;
        }
    }

    function post_inc_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b++;
        }
    }

    function post_dec_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->b--;
        }
    }

    function isset_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = isset($this->b);
        }
    }

    function empty_prop($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = empty($this->b);
        }
    }

    function g() {
    }

    function call($n) {
        for ($i = 0; $i < $n; ++$i) {
            $this->g();
        }
    }

    function read_const($n) {
        for ($i = 0; $i < $n; ++$i) {
            $x = $this::TEST;
        }
    }

}

function read_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = Foo::$a;
    }
}

function write_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        Foo::$a = 0;
    }
}

function isset_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = isset(Foo::$a);
    }
}

function empty_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = empty(Foo::$a);
    }
}

function call_static($n) {
    for ($i = 0; $i < $n; ++$i) {
        Foo::f();
    }
}

function create_object($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = new Foo();
    }
}

define('TEST', null);

function read_const($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = TEST;
    }
}

function read_auto_global($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = $_GET;
    }
}

$g_var = 0;

function read_global_var($n) {
    for ($i = 0; $i < $n; ++$i) {
        $x = $GLOBALS['g_var'];
    }
}

function read_hash($n) {
    $hash = array('test' => 0);
    for ($i = 0; $i < $n; ++$i) {
        $x = $hash['test'];
    }
}

function read_str_offset($n) {
    $str = "test";
    for ($i = 0; $i < $n; ++$i) {
        $x = $str[1];
    }
}

function issetor($n) {
    $val = array(0,1,2,3,4,5,6,7,8,9);
    for ($i = 0; $i < $n; ++$i) {
        $x = $val ?: null;
    }
}

function issetor2($n) {
    $f = false; $j = 0;
    for ($i = 0; $i < $n; ++$i) {
        $x = $f ?: $j + 1;
    }
}

function ternary($n) {
    $val = array(0,1,2,3,4,5,6,7,8,9);
    $f = false;
    for ($i = 0; $i < $n; ++$i) {
        $x = $f ? null : $val;
    }
}

function ternary2($n) {
    $f = false; $j = 0;
    for ($i = 0; $i < $n; ++$i) {
        $x = $f ? $f : $j + 1;
    }
}

/*****/

function empty_loop($n) {
    for ($i = 0; $i < $n; ++$i) {
    }
}

function gethrtime()
{
  $hrtime = hrtime();
  return (($hrtime[0]*1000000000 + $hrtime[1]) / 1000000000);
}

function start_test()
{
  ob_start();
  return gethrtime();
}

function end_test($start, $name, $overhead = null)
{
  global $total;
  global $last_time;
  $end = gethrtime();
  ob_end_clean();
  $last_time = $end-$start;
  $total += $last_time;
  $num = number_format($last_time,3);
  $pad = str_repeat(" ", 24-strlen($name)-strlen($num));
  if (is_null($overhead)) {
    echo $name.$pad.$num."\n";
  } else {
    $num2 = number_format($last_time - $overhead,3);
    echo $name.$pad.$num."    ".$num2."\n";
  }
  ob_start();
  return gethrtime();
}

function total()
{
  global $total;
  $pad = str_repeat("-", 24);
  echo $pad."\n";
  $num = number_format($total,3);
  $pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
  echo "Total".$pad.$num."\n";
}

const N = 5000000;

$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
simpleucall(N);
$t = end_test($t, 'func()', $overhead);
simpleudcall(N);
$t = end_test($t, 'undef_func()', $overhead);
simpleicall(N);
$t = end_test($t, 'int_func()', $overhead);
Foo::read_static(N);
$t = end_test($t, '$x = self::$x', $overhead);
Foo::write_static(N);
$t = end_test($t, 'self::$x = 0', $overhead);
Foo::isset_static(N);
$t = end_test($t, 'isset(self::$x)', $overhead);
Foo::empty_static(N);
$t = end_test($t, 'empty(self::$x)', $overhead);
read_static(N);
$t = end_test($t, '$x = Foo::$x', $overhead);
write_static(N);
$t = end_test($t, 'Foo::$x = 0', $overhead);
isset_static(N);
$t = end_test($t, 'isset(Foo::$x)', $overhead);
empty_static(N);
$t = end_test($t, 'empty(Foo::$x)', $overhead);
Foo::call_static(N);
$t = end_test($t, 'self::f()', $overhead);
call_static(N);
$t = end_test($t, 'Foo::f()', $overhead);
$x = new Foo();
$x->read_prop(N);
$t = end_test($t, '$x = $this->x', $overhead);
$x->write_prop(N);
$t = end_test($t, '$this->x = 0', $overhead);
$x->assign_add_prop(N);
$t = end_test($t, '$this->x += 2', $overhead);
$x->pre_inc_prop(N);
$t = end_test($t, '++$this->x', $overhead);
$x->pre_dec_prop(N);
$t = end_test($t, '--$this->x', $overhead);
$x->post_inc_prop(N);
$t = end_test($t, '$this->x++', $overhead);
$x->post_dec_prop(N);
$t = end_test($t, '$this->x--', $overhead);
$x->isset_prop(N);
$t = end_test($t, 'isset($this->x)', $overhead);
$x->empty_prop(N);
$t = end_test($t, 'empty($this->x)', $overhead);
$x->call(N);
$t = end_test($t, '$this->f()', $overhead);
$x->read_const(N);
$t = end_test($t, '$x = Foo::TEST', $overhead);
create_object(N);
$t = end_test($t, 'new Foo()', $overhead);
read_const(N);
$t = end_test($t, '$x = TEST', $overhead);
read_auto_global(N);
$t = end_test($t, '$x = $_GET', $overhead);
read_global_var(N);
$t = end_test($t, '$x = $GLOBALS[\'v\']', $overhead);
read_hash(N);
$t = end_test($t, '$x = $hash[\'v\']', $overhead);
read_str_offset(N);
$t = end_test($t, '$x = $str[0]', $overhead);
issetor(N);
$t = end_test($t, '$x = $a ?: null', $overhead);
issetor2(N);
$t = end_test($t, '$x = $f ?: tmp', $overhead);
ternary(N);
$t = end_test($t, '$x = $f ? $f : $a', $overhead);
ternary2(N);
$t = end_test($t, '$x = $f ? $f : tmp', $overhead);
total($t0, "Total");

include_once  '/var/www/html/xhprof/footer.php';

ejecutar:

php index.php

访问 http: //your_host/xhprof_html/index.php:

Haga clic sobre la marcha, es el programa se está ejecutando diversos métodos estadísticos tales como los datos de tiempo y el recuento de ejecución de las tablas, puede ordenar Vista:

Seleccione: [Ver callgraph Full] Ver generar un diagrama de temporización, la pantalla roja es la más grande, seguido por el amarillo, el cual comprende un método realizado por las + + invocaciones:

Opción dos: En el marco

Xhprof_lib introduce directamente en el cuadro como una tercera carpeta lib biblioteca parte llamada. 

En el marco de todos los archivos de entrada del código de index.php código de contenido envase exterior en los archivos de cabecera y pie de página a la parte superior, y que garantizan la correcta y referencias a xhprof_lib.php xhprof_runs.php.

Entrada al ejemplo yaf:

<?php
//header
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);


//code
define('APPLICATION_PATH', dirname(__FILE__)."/../");

$application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini");

$application->bootstrap()->run();


//footer
$XHPROF_PATH = APPLICATION_PATH . "library/ThirdParty/";
include_once $XHPROF_PATH . 'xhprof_lib/utils/xhprof_lib.php';
include_once $XHPROF_PATH . 'xhprof_lib/utils/xhprof_runs.php';
$profiler_namespace = 'test';
$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);

?>

resumen

Más que las palabras pueden hacer alguna prueba para probar el entorno de desarrollo, o en línea, a continuación, el código debe ser borrado.

  1. Si se produce un error al ejecutar cmd fallido El: "DOT -Tpng" stderr :. `( Proceso: 24220 ): Pango-ADVERTENCIA **: no válida UTF-8 cadena que se pasa a pango_layout_set_text ()'. Temporalmente no aclarar cómo resolver, se puede optar por evitarlo. Imprimirá 121,122 filas xhprof_lib / utils / callgraph_utils.php y salida comentada.
  2. Si se produce un error de error: o bien que no podemos encontrar perfiles de datos para run_id xxx o el umbral de 0,01 es demasiado pequeño o no lo hace utilidad de generación de imagen instalada, tienen 'punto' no puede generar imágenes PNG, archivo, probablemente porque no se genera reconocimiento de caracteres, la reparación de la siguiente 3 :
    $cmd = " dot -T".$type;
    // 在 cmd 之后添加一个转码工作就可以了
    $dot_script = iconv("UTF-8", "ASCII//IGNORE", $dot_script);
    
  3. Si usted quiere verse mejor interfaz de usuario, puede hacer referencia a la siguiente enlace 1 , enlace 2 , enlace 3  (manual de paseo libre).
  4. Éstos son algunos Parámetro Descripción
puntos de rendimiento descripción
Tiempo incluido Incluyendo todas las funciones de tiempo de ejecución
Exclusivo Tiempo / Auto Pasar ejecución de la función el tiempo mismo, sin incluir el tiempo de ejecución del sub-árbol
Tiempo pared tiempo o el tiempo de reloj de pared
Tiempo de CPU El usuario transcurrir tiempo núcleo consumo +
CPU incluido Incluyendo subfunciones juntos ocuparon la CPU
CPU exclusiva Función en sí ocupada por la CPU
  1. xhprof tema  y una entrada de blog mencionado algunas de la cesión temporal alternativa, además de  Tideways  etc. 

  2. configuración de referencia nginx  de un gitbook  y  nginx Wiki  

  3. Esto es difícil de encontrar una solución en Internet, me encontré en el PHP plataforma de error: Enlace  

Referencia: http://wulfric.me/2017/08/php7-xhprof/

Publicados 115 artículos originales · ganado elogios 101 · vistas 370 000 +

Supongo que te gusta

Origin blog.csdn.net/Alen_xiaoxin/article/details/105309562
Recomendado
Clasificación