浏览代码

adding REPORT env var, adding xhprof image with php 5.6

vpoturaev 7 年之前
父节点
当前提交
b21284b657
共有 6 个文件被更改,包括 84 次插入4 次删除
  1. 3 1
      Dockerfile
  2. 18 0
      Dockerfile.xhprof_php56
  3. 7 1
      Makefile
  4. 11 0
      README.md
  5. 6 2
      prepend_v4.php
  6. 39 0
      prepend_xhprof.php

+ 3 - 1
Dockerfile

@@ -11,7 +11,7 @@ RUN curl "https://github.com/tideways/php-profiler-extension/archive/v4.1.5.tar.
      cd .. && rm -rf ./a.tar.gz ./php-xhprof-extension-4.1.5 && \
      docker-php-ext-enable tideways
 
-RUN apt-get update && apt-get install -y graphviz
+RUN apt-get update && apt-get install -y graphviz && apt-get clean
 
 COPY ./prepend_v4.php /prepend.php
 
@@ -21,4 +21,6 @@ COPY ./xhprof/xhprof_lib/utils /utils
 
 ENV SVG=$SVG
 
+ENV REPORT=$REPORT
+
 WORKDIR /code

+ 18 - 0
Dockerfile.xhprof_php56

@@ -0,0 +1,18 @@
+FROM php:5.6-cli
+
+RUN apt-get update && apt-get install -y graphviz && apt-get clean
+
+RUN pecl install xhprof-0.9.4 \
+ && docker-php-ext-enable xhprof
+
+COPY ./prepend_xhprof.php /prepend.php
+
+COPY ./php.ini /usr/local/etc/php/
+
+COPY ./xhprof/xhprof_lib/utils /utils
+
+ENV SVG=$SVG
+
+ENV REPORT=$REPORT
+
+WORKDIR /code

+ 7 - 1
Makefile

@@ -2,4 +2,10 @@ build:
 	docker build -t phperf/php-profiler .
 
 push:
-	docker push phperf/php-profiler
+	docker push phperf/php-profiler:latest
+
+build56:
+	docker build -t phperf/php-profiler:5.6-xhprof -f Dockerfile.xhprof_php56 .
+
+push56:
+	docker push phperf/php-profiler:5.6-xhprof

+ 11 - 0
README.md

@@ -17,6 +17,7 @@ docker run --rm -e SVG=1 -v $(pwd):/code phperf/php-profiler php test_me.php
 ```
 
 After your script is finished `xhprof` report and SVG graph will be saved:
+
 ```
 Performance index: 0.00037335238456726
 Nodes in report: 33
@@ -27,6 +28,7 @@ Saving graph to xhprof_report.1518411787.448.serialized.svg
 ```
 
 You can analyze profiling report with [`xh-tool`](https://github.com/phperf/xh-tool)
+
 ```
 xh-tool top xhprof_report.1518411787.448.serialized --limit 5 --strip-nesting
 name                           wallTime   wallTime%   wallTime1   ownTime    ownTime%   ownTime1   count
@@ -38,6 +40,7 @@ preg_match                     23.71ms    1.27        4.7us       23.71ms    1.2
 ```
 
 Or you can upload report to https://blackfire.io (`blackfire` cli tool needed) for analysis
+
 ```
 blackfire upload xhprof_report.1517989322.4259.serialized
 ```
@@ -54,4 +57,12 @@ I/O Time     5.31ms
 Memory       47.4KB
 Network         n/a     n/a       -
 SQL             n/a       -
+```
+
+If you want to save profile data to a particular file, you can provide it with `REPORT=<name>`, you can use `JSON` format if name ends with `.json`
+
+```
+docker run --rm -e REPORT=rep.json -v $(pwd):/code phperf/php-profiler php t2.php
+Nodes in report: 12
+Saving report to rep.json
 ```

+ 6 - 2
prepend_v4.php

@@ -5,10 +5,14 @@ register_shutdown_function(
         register_shutdown_function(function () {
             $data = tideways_disable();
             echo 'Nodes in report: ' . count($data) . "\n";
-            $name = 'xhprof_report.' . microtime(1) . '.serialized';
+            $name = getenv('REPORT');
+            if (empty($name)) {
+                $name = 'xhprof_report.' . microtime(1) . '.serialized';
+            }
             echo 'Saving report to ' . $name, "\n";
+
             file_put_contents('/code/' . $name,
-                serialize($data));
+                '.json' === substr($name, -5) ? json_encode($data) : serialize($data));
 
             if (getenv('SVG')) {
                 include_once "/utils/callgraph_utils.php";

+ 39 - 0
prepend_xhprof.php

@@ -0,0 +1,39 @@
+<?php
+
+register_shutdown_function(
+    function () {
+        register_shutdown_function(function () {
+            $data = xhprof_disable();
+            echo 'Nodes in report: ' . count($data) . "\n";
+            $name = getenv('REPORT');
+            if (empty($name)) {
+                $name = 'xhprof_report.' . microtime(1) . '.serialized';
+            }
+            echo 'Saving report to ' . $name, "\n";
+
+            file_put_contents('/code/' . $name,
+                '.json' === substr($name, -5) ? json_encode($data) : serialize($data));
+
+            if (getenv('SVG')) {
+                include_once "/utils/callgraph_utils.php";
+                include_once "/utils/xhprof_lib.php";
+                include_once "/utils/xhprof_runs.php";
+
+                echo "Generating dot script\n";
+                $threshold = 0;
+                if (count($data) > 1000) {
+                    $threshold = 0.001;
+                }
+                $script = xhprof_generate_dot_script($data, $threshold, null, null, null, null);
+                echo "Generating dot image\n";
+                $content = xhprof_generate_image_by_dot($script, 'svg');
+                echo 'Saving graph to ' . $name . '.svg', "\n";
+                file_put_contents('/code/' . $name . '.svg',
+                    $content);
+            }
+        });
+    }
+);
+
+xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
+