-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from eeliu/optimize_class_loader
Optimize class loader
- Loading branch information
Showing
54 changed files
with
2,278 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.