vpoturaev 8 rokov pred
rodič
commit
40ea584bda
7 zmenil súbory, kde vykonal 123 pridanie a 0 odobranie
  1. 2 0
      .gitignore
  2. 18 0
      Dockerfile
  3. 5 0
      Makefile
  4. 35 0
      README.md
  5. 1 0
      php.ini
  6. 15 0
      prepend_v4.php
  7. 47 0
      test_me.php

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+/.idea
+/xhprof_report.*

+ 18 - 0
Dockerfile

@@ -0,0 +1,18 @@
+FROM php:7.2-cli
+
+RUN curl "https://github.com/tideways/php-profiler-extension/archive/v4.1.4.tar.gz" -fsL -o ./a.tar.gz  && \
+     tar xf ./a.tar.gz && \
+     cd ./php-profiler-extension-4.1.4 && \
+     ls -la && \
+     phpize && \
+     ./configure && \
+     make && \
+     make install && \
+     cd .. && rm -rf ./a.tar.gz ./php-profiler-extension-4.1.4 && \
+     docker-php-ext-enable tideways
+
+COPY ./prepend_v4.php /prepend.php
+
+COPY ./php.ini /usr/local/etc/php/
+
+WORKDIR /code

+ 5 - 0
Makefile

@@ -0,0 +1,5 @@
+build:
+	docker build -t phperf/php-profiler .
+
+push:
+	docker push phperf/php-profiler

+ 35 - 0
README.md

@@ -0,0 +1,35 @@
+# Dockerized command-line PHP 7 profiler (tideways)
+
+Portable docker image with PHP 7.2 and `tideways` extension installed.
+
+Profiling is started automatically with `auto_prepend_file` configuration.
+
+## Usage
+
+```
+docker run --rm -v $(pwd):/code phperf/php-profiler php test_me.php
+```
+
+After your script is finished `xhprof` report will be saved:
+```
+Saving report to xhprof_report.1517907620.7139.serialized
+```
+
+You can upload it to https://blackfire.io (cli tool needed) for analysis
+```
+blackfire upload xhprof_report.1517907620.7139.serialized
+```
+
+```
+Blackfire upload completed
+Graph URL https://blackfire.io/profiles/e4372950-a0a6-407a-95e2-a939e40dade2/graph
+No tests! Create some now https://blackfire.io/docs/cookbooks/tests
+No recommendations
+
+Wall Time     2.93s
+CPU Time      2.91s
+I/O Time     16.3ms
+Memory       47.4KB
+Network         n/a     n/a       -
+SQL             n/a       -
+```

+ 1 - 0
php.ini

@@ -0,0 +1 @@
+auto_prepend_file = "/prepend.php"

+ 15 - 0
prepend_v4.php

@@ -0,0 +1,15 @@
+<?php
+
+tideways_enable(TIDEWAYS_FLAGS_MEMORY | TIDEWAYS_FLAGS_CPU);
+
+register_shutdown_function(
+    function () {
+        register_shutdown_function(function () {
+            $data = tideways_disable();
+            $name = 'xhprof_report.' . microtime(1) . '.serialized';
+            echo 'Saving report to ' . $name, "\n";
+            file_put_contents('/code/' . $name,
+                serialize($data));
+        });
+    }
+);

+ 47 - 0
test_me.php

@@ -0,0 +1,47 @@
+<?php
+
+class PerformanceIndex
+{
+    public static function measure()
+    {
+        $start = microtime(1);
+        $numIterations = 5000;
+
+        for ($i = 0; $i < $numIterations; ++$i) {
+            $a = md5('abracadabra');
+            $a = eval('md5($a);');
+            $a = sha1($a);
+
+            $group = strstr('group#foobar', '#', true);
+            $foobar = substr('group#foobar', strlen($group) + 1);
+            self::checkParts($group, $foobar);
+            preg_match('{^(.*?)#(.*)$}', 'group#foobar', $matches);
+            self::checkParts($matches[1], $matches[2]);
+            $parts = explode('#', 'group#foobar');
+            self::checkParts($parts[0], $parts[1]);
+
+
+            self::euler(20, 10);
+            self::euler(2166, 6099);
+            self::euler(1239432166, 2221248099);
+        }
+
+        return (microtime(1) - $start) / $numIterations;
+    }
+
+    private static function euler($x, $y)
+    {
+        $r = $x % $y;
+        if ($r == 0) {
+            return $y;
+        }
+        return self::euler($y, $r);
+    }
+
+    private static function checkParts($group, $foobar)
+    {
+        return $group === 'group' && $foobar === 'foobar';
+    }
+}
+
+echo 'Performance index: ', PerformanceIndex::measure(), "\n";