blackfire.io:深入研究更快的PHP应用程序的性能分析
>没有人喜欢缓慢,笨拙的应用程序,尤其是在处理快速扩展的数据库和数百万日常请求时。 分析提供了一个解决方案,提供了对程序资源消耗(时间和内存)的见解。这允许识别和解决性能瓶颈。 存在许多分析工具,每种工具都采用不同的方法。
密钥功能:
零影响仪器:与传统介绍者不同,黑火不会减慢您的应用程序。
> 通过Chrome扩展名或命令行工具。>通过vagrant ssh
访问您的流浪框后,创建一个Blackfire帐户(如果您还没有一个帐户)。 从Blackfire配置文件设置中检索您的凭据(客户端和服务器)。 使用以下凭据更新您的homestead.yaml
>文件(位于Vagrant Box的根目录中)
<code class="language-yaml">blackfire: - id: "Server Id here" token: "Server token here" client-id: "Client Id here" client-token: "Client token here"</code>
黑火体系结构:
黑火包括五个核心组件:
基本术语:
参考资料:比较的基线性能测量。
1。虚拟数据生成(userproviderjson.php):
>使用
运行此脚本。这会创建。
<code class="language-php"><?php require_once('vendor/autoload.php'); $num = isset($_GET['num']) ? $_GET['num'] : 1000; $data = []; $faker = Faker\Factory::create(); if(!file_exists('data')) { mkdir('data'); } for ($i = 0; $i < $num; $i++) { $data[] = ['name' => $faker->name, 'email' => $faker->email, 'city' => $faker->city,]; } file_put_contents('data/users.json', json_encode($data)); echo 'JSON file generated.';</code>2。数据库设置:
php UserProviderJSON.php
data/users.json
创建一个具有适当列的MySQL数据库(
)。
3。主脚本(Benchmark-before.php):blackfire_tutorial
sample_users
4。初始分析:
<code class="language-php"><?php $db = new PDO('mysql:host=localhost;dbname=blackfire_tutorial;charset=utf8', 'homestead', 'secret'); function SaveCustomers($db) { $users = json_decode(file_get_contents('data/users.json'), true); foreach ($users as $user) { $stmt = $db->prepare("INSERT INTO sample_users (name, email, city) VALUES (?, ?, ?)"); $stmt->execute([$user['name'], $user['email'], $user['city']]); } } SaveCustomers($db); echo 'Users imported successfully.';</code>使用Blackfire Chrome扩展名,配置文件
,创建一个新的参考配置文件。
>Blackfire Web界面提供了详细的分析:工具栏汇总了关键指标,可视化执行流,具有详细定时信息的功能列表以及各种指标(SQL查询,内存使用等)。
benchmark-before.php
5。优化和重新封建:
>优化脚本以减少数据库调用(例如,使用具有多个值的单个插入语句)。 用优化的代码创建benchmark-after.php
。重新构图,与参考配置文件进行比较。
比较突出了绩效的改进。
命令行接口:
)允许通过终端进行分析:blackfire
blackfire curl http://your-url
blackfire run php your-script.php
创建一个参考资料:blackfire --new-reference curl http://your-url
blackfire --reference=profile-id curl http://your-url
>
性能测试:
blackfire(高级用户)允许在>文件中创建性能测试,从而定义了各种指标的断言。.blackfire.yml
结论:
以上是与黑火的增压应用程序的深入演练的详细内容。更多信息请关注PHP中文网其他相关文章!