Skip to content

Commit

Permalink
Merge pull request #166 from eeliu/optimize_class_loader
Browse files Browse the repository at this point in the history
Optimize class loader
  • Loading branch information
eeliu authored Apr 29, 2020
2 parents dd5c650 + 6a83460 commit 55cad50
Show file tree
Hide file tree
Showing 54 changed files with 2,278 additions and 127 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
- ROOT=$PWD
script:
- set -x
- cd $ROOT/PHP/pinpoint_php_ext/
- phpize && ./configure && make
# - make test TESTS=-m
- make test
8 changes: 8 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Changes

## v0.2.3

- Support Yii framework classloader [(How to use pinpoint-php into Yii?)](PHP/pinpoint_php_example/demo/yii-demo/Readme.md)
- Add some out-of-box plugins for flraum [ (How to use pinpoint-php into Flarum?)](PHP/pinpoint_php_example/demo/Flarum-demo/Reademe.md)
- Fix bug in reference parameters
- Supporting the return type is object
110 changes: 110 additions & 0 deletions DOC/PHP/User Manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

> Contents
[toc]

## 1 How pinpoint-php agent works?

Pinpoint php agent employs [php_simple_aop](https://github.com/eeliu/php_simple_aop) as its aspect programming library, it helps converting origin class.php to proxied_class.php which includes plugins.php and origin.php without affecting any function of origin class.php.

``` php
+--------------+ +-----------------------+
| origin.php | | origin.php |
| | | proxied_origin.php |
+--------------+ | require_origin.php |
| entry for plugins.php |
^ +-----------------------+
|
| Before ^
| | After
| |
+----+ vender/class_loader +-------+

```

## 1.1 Performance Result

> TPS loss and TPR Loss
![FlarumPerformanceTest](../images/FlarumPerformanceTest.png)

> TPS: Time per request
> TPR: Requests per second
[How to use pinpoint-php into Flarum?](../../PHP/pinpoint_php_example/demo/Flarum-demo/Reademe.md)

> Call Tree
![CallTree](../images/Flarum-callstack.png)

> Summary
* Less than 5% loss, when Flarum add PHP Agent.

If you care about the performance most, you can call pinpoint-php agent module API directly, which is written by C&C++. [(pinpoint-php api)](../../PHP/pinpoint_php_ext/pinpoint_php_api.php)

## 2 How to hook a object ?

```
use A\a as a;
class Foo{
public function foo(): a
{
return new a::factory_create_new();
}
}
```

> As foo returns a new object and this scenario can't be detected by php_simple_aop easily.
### 2.1 Use a decorator

Replace the return object with a decorated object in onEnd(). There are some [magic methods](https://www.php.net/manual/en/language.oop5.magic.php) to help "hacking" the object.

[ [How it works ☚]](../../PHP/pinpoint_php_example/Plugins/InstancePlugins.php)


### 2.2 Examples

#### 2.2.1 Hook a generator.

> https://github.com/naver/pinpoint-c-agent/issues/100
``` php
...
function generator($start, $limit, $step=1){
if($start > $limit){
throw new LogicException("start cannot bigger that limit.");
}
usleep(120000);
for($i = $start; $i<=$limit; $i += $step){
try{
yield $i;
}catch (Exception $exception){
echo $exception->getMessage();
}
}
}
...
```
[ [Goto GeneratorPlugin ☚] ](../../PHP/pinpoint_php_example/Plugins/GeneratorPlugin.php)

#### 2.2.2 PDO::prepare return a PDOStatement

``` php
Reference: https://www.php.net/manual/zh/pdo.prepare.php
<?php

$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchAll();
?>

```

Replace $dbh->prepare return value with [Plugins\PDOStatement](../../PHP/pinpoint_php_example/Plugins/PDOStatement.php).

Binary file added DOC/images/Flarum-callstack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added DOC/images/FlarumPerformanceTest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 60 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,63 @@ software, affording one all of the rights of Public Domain with the
minor nuisance of being required to keep the above copyright notice
and license text in the source code. Note also that by accepting the
Public Domain "license" you can re-license your copy using whatever
license you like.
license you like.

=======================================================================
flarum/flarum from https://github.com/flarum/flarum
=======================================================================

The MIT License (MIT)

Copyright (c) 2019-2020 Stichting Flarum (Flarum Foundation)
Copyright (c) 2014-2019 Toby Zerner ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=======================================================================
yiisoft/yii from https://github.com/yiisoft/yii
=======================================================================
Copyright (c) 2008 by Yii Software LLC (http://www.yiisoft.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yii Software LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
30 changes: 18 additions & 12 deletions PHP/Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## v0.1.x VS v0.2.x


> Why we refactor the pinpoint_c_agent (v0.1.x)
> Why do we refactor the pinpoint_c_agent (v0.1.x)?
1. Hard to support all php versions.
2. Slow, v0.1.x version hooks all functions.
Expand Down Expand Up @@ -32,13 +32,14 @@ v0.2.x|✘|✘|✔|✔
Framework|v0.1.x|v0.2.x
----|-----|----|
Laravel|✔|✔
ThinkPHP|✔|✔
EasySwoole|✘|✘
ThinkPHP|✔|✘
YII|✔|✔
Workerman|✘|✔
EasySwoole|✘|✔

> Note
1. We are trying to support PHP asynchrnous Framework.Such as Workerman and EasySwoole
2. If your php application not support composer(such as woredpress,phpwind etc),the only choise is v0.1.x.
1. If your php application does not support composer(such as woredpress,phpwind etc), the only choice is v0.1.x.

#### Stability

Expand All @@ -49,18 +50,21 @@ EasySwoole|✘|✘

```
✔: Not block user's application.
✘: Block user's application: php-fpm/apache should be restart
✘: Block user's application: php-fpm/apache should be restart.
```

Functional|v0.1.x|v0.2.x
----|-----|----
1.Update plugins(CRUD) |✘|✔ [How to ?](https://github.com/eeliu/php_simple_aop#how-to-reload-all-plugins)
2.Update pinpoint collector|✘|✔
3.Update pinpiont_php.so(pinpoint.so)|✘|✘
3.Update pinpoint_php.so(pinpoint.so)|✘|✘



#### Performance Loss Under Stress Test



#### Performance Loss Under Stress Test

> Test Scene
Expand All @@ -84,11 +88,13 @@ phpmyadmin with pinpoint-php v0.2|398.26ms
MTT: Mean RTT Time
```

> By the way,pinpoint is an APM system, not a performance enhancement system. Loss can't avoiding from now on.
> [ pinpoint-php on Flraum test result ](../DOC/PHP/User%20Manual.md#11-performance-result)
> By the way, pinpoint is an APM system but not a performance enhancement system. The loss can't be avoided at present.

### Contributors's words

As composer is widedly used, v0.2.x is our long-time support version,
v0.1.x could be obsoleted in future.
While, we could fix some fatal error into v0.1.x.
As composer is widely used, v0.2.x is our long-time support version,
v0.1.x could be obsoleted in the future.
But we could fix some fatal error into v0.1.x.
9 changes: 7 additions & 2 deletions PHP/pinpoint_php_example/.github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ about: Create a report to help us improve
---

**Describe the bug**

A clear and concise description of what the bug is.

**To Reproduce**

Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.
**Expected behavior**

A clear and concise description of what you expect to happen.

**Screenshots**

If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
Expand All @@ -32,4 +36,5 @@ If applicable, add screenshots to help explain your problem.
- Version [e.g. 22]

**Additional context**

Add any other context about the problem here.
62 changes: 62 additions & 0 deletions PHP/pinpoint_php_example/Plugins/ClassMapInFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
#-------------------------------------------------------------------------------
# Copyright 2019 NAVER Corp
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#-------------------------------------------------------------------------------

namespace Plugins;
use pinpoint\Common\AopClassMap;

class ClassMapInFile extends AopClassMap
{
private $mustHoldClasses = ['app\AccessRemote'=>null,'app\Proxied_AccessRemote'=>null,'Plugins\PerRequestPlugins'=>null];
public function __construct()
{
parent::__construct();
}

public function findFile($classFullName)
{
$file = parent::findFile($classFullName);
if($file){
if(!PerRequestPlugins::instance()->traceLimit())
{
return $file;
}elseif (array_key_exists($classFullName ,$this->mustHoldClasses) == 1){
return $file;
}else{
echo "reject $$classFullName <br>";
return null;
}
}
return $file;

// if(!PerRequestPlugins::instance()->traceLimit())
// {
// return parent::findFile($classFullName);
// }
//
// if (array_key_exists($classFullName ,$this->mustHoldClasses) == 1)
// {
// if(PerRequestPlugins::instance()->traceLimit() && )
//
// return parent::findFile($classFullName);
// }else {
// echo "reject".$classFullName.'<br>';
// return null;
// }
}

}
require __DIR__ . '/PerRequestPlugins.php';
1 change: 0 additions & 1 deletion PHP/pinpoint_php_example/Plugins/CommonPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function onBefore(){

///@hook:app\DBcontrol::getData1 app\DBcontrol::\array_push
public function onEnd(&$ret){
var_dump($ret);
pinpoint_add_clues(PHP_RETURN,print_r($this->ret,true));
}

Expand Down
Loading

0 comments on commit 55cad50

Please sign in to comment.