green-elephpant/code-bench 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

green-elephpant/code-bench

Composer 安装命令:

composer require green-elephpant/code-bench

包简介

Code benchmark tool

README 文档

README

Green ElePHPant

Green ElePHPant - CodeBench

The Green ElePHPant wants to help you to reduce the carbon emissions of your software.

One way of doing so is to optimize the performance. Less CPU, RAM and Storage means reduced energy consumption, fewer carbon emissions, and less hardware to be produced. And, last but not least, the users of your software will benefit from a faster application.

GreenElephpant\CodeBench is a simple benchmark tool that can help you to improve the performance of your code by benchmarking several possible solutions and helps you to decide which code to chose.

Key concept

GreenElephpant\CodeBench simply measures the execution time and memory consumption of callables, which contain the code you want to measure.

Shout-out to PhpBench, which is a much better solution if you need super accurate measurement, because it runs the code to be measured in isolation. It also is meant to be used as test suite similar to PHPUnit and can thus be used to catch performance regressions during CI/CD, which makes it extremely useful . However, GreenElephpant\CodeBench is your coding compadre if a quick and rough measurement is enough to decide between several options.

Installation

Simply use composer to add GreenElephpant\CodeBench to your project:

composer require green-elephpant/code-bench --dev

Note: we use require-dev here, since GreenElephpant\CodeBench should not be used in production code.

Note: for Laravel, you can use the package CodeBench Laravel.

Note: GreenElephpant\CodeBench works with PHP 7.4+ to support most code bases. However, only since PHP 8.2 the functionmemory_reset_peak_usage is available. With earlier versions, you will not be able to monitor the peak memory usage with this tool.

Configuration

GreenElephpant\CodeBench is designed to require no other dependencies to keep its footprint small, so there is nothing much to do.

Set Logger callable

By default, GreenElephpant\CodeBench uses print for logging. However, you can set a callable which takes the output and you can log it where you want.

For example, in Laravel you could do

CodeBench::$loggerCallable = function (string $text) {
    Log::debug($text);
};

to use whatever logger you have defined.

Note: the CodeBench Laravel package does that for you.

Usage

Basic Usage

In its purest form, the usage is as easy as the following function call:

use GreenElephpant\CodeBench\CodeBench;

CodeBench::benchmark([
    function () {
        // The first option we want to test    
        date('D M d Y H:m:s');
    },
    function () {
        // The second option we want to test
        (new DateTime())->format('D M d Y H:m:s');
    }
]);

The result should be something similar to this:

Function 1
Time (seconds): 0.00000310 (Reference)
Memory (MB): 0.00000000 (Reference)
Memory Peak (MB): 0.00000000 (Reference)

Function 2
Time (seconds): 0.00000405 (=> 1.31x)
Memory (MB): 0.00000000 (=> 0.00x)
Memory Peak (MB): 0.00000000 (=> 0.00x)

What does the output mean?

  • Time (seconds) tells us the time in seconds that the callable needed to execute. Since the example is just a simple function call, it's just a fraction of a second.
  • Memory (MB) is the difference of the used memory before and after the callable was executed
  • Memory Peak (MB) is the biggest amount of memory (aka the peak) the callable required during its execution. Since PHP frees memory during the execution time, this measurement is much more important if you want to find out if the callable can hit the configured PHP memory limit.

Iterations

Results can vary between several executions of the callable. To balance out any outliers, we can run several iterations and take the average. This behaviour is controlled by the $iterations parameter. The following example will run the each callable 100 times:

GreenElephpant\CodeBench\CodeBench::benchmark([
    function () {
        date('D M d Y H:m:s');
    },
    function () {
        (new DateTime())->format('D M d Y H:m:s');
    }
], 100);

Pre-run callable

By default, GreenElephpant\CodeBench executes the callable one time before starting with the actual measurement. This is to avoid side effects by e.g. cache warmups. To disable this behaviour, run the benchmark with $preRunCallable set to false:

GreenElephpant\CodeBench\CodeBench::benchmark([
    function () {
        date('D M d Y H:m:s');
    },
    function () {
        (new DateTime())->format('D M d Y H:m:s');
    }
], 100, false);

Stopwatch

You can use the start() and stop() functions to measure the performance of a code block.

use GreenElephpant\CodeBench\CodeBench;

CodeBench::start();

// Your code block here
for ($i = 0; $i < 1000; $i++) {
    expensive_function_call();
}

CodeBench::stop();

green-elephpant/code-bench 适用场景与选型建议

green-elephpant/code-bench 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 green-elephpant/code-bench 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 green-elephpant/code-bench 我们能提供哪些服务?
定制开发 / 二次开发

基于 green-elephpant/code-bench 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-19