ayesh/php-timer 问题修复 & 功能扩展

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

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

ayesh/php-timer

Composer 安装命令:

composer require ayesh/php-timer

包简介

High-resolution and monotonic stop-watch for all your needs. Supports timer start, pause, resume, stop, read, and minimal conversion.

README 文档

README

Latest Stable Version License Scrutinizer Code Quality CI codecov Too many badges

Synopsis

A helper class to calculate how long a particular task took.

This class is similar to phpunit/php-timer, but not a fork, nor mimic its functionality.

  • Multiple timers by a given key.
  • Read the current time elapsed without stopping the timer.
  • Stop the timer, and continue from where it left (stopwatch).
  • Dead simple API with only 4 static methods.
  • 100% unit test coverage.
  • Gets you precise time in milliseconds (with options to convert to seconds)
  • Individual Stopwatch class for serialization and other use cases.

Prerequisites

  • PHP 8.2 or later.

Installing

The simplest way would be to install using composer.

    composer require ayesh/php-timer

Usage

It is pretty simple to use the timer, with all methods being static, and allowing only 4 methods.

Start timer

    <?php
    use Ayesh\PHP_Timer\Timer;
    Timer::start();

This starts the timer (actually keeps the current time stored) with the key default. Throughout the library, if you do not provide a specific key, this default key is used.

Alternately, you can start the timer with a given key:

    Timer::start('something');

Once you start the time with a given key, you can use the same key to refer to that particular timer. You can of course use PHP magic constants to make things easier:

    Timer::start(__FUNCTION__);

Attempting to start the timer with a non-string key will throw a \TypeError exception. You can call the start method multiple times even if the timer has started. It will not reset the timer.

Read timer

After starting the timer, you can read the elapsed time at any time. Reading the time will not stop the timer. You can read the timer, do some expensive calculations, and read again to get the cumulative time.

    Timer::read(); // Default timer.
    Timer::read('default'); // Default timer.
    Timer::read('something'); // Timer started with key "something".

Attempting to read a timer that is not started will throw an \LogicException exception.

Formats

You can pass a second argument to let this library make minimal processing for you:

    Timer::read('something', Timer::FORMAT_PRECISE); // 0.10180473327637

See the formats section below for the formats supported.

Stop timer

You can stop the timer anytime as well. This makes the library store the stop time, and your further Timer::read() calls will always return the time it took between start and stop.

    Timer::stop(); // Default timer.
    Timer::stop('something'); // Timer started with key "something"

Attempting to stop a timer that is not started will throw an \LogicException exception.

Reset timer

By default, starting the timer after stopping it will continue it from where it left off. For example, if you have 3 seconds on the timer when you stop it, and start it again, the total time will start from 3 seconds. You can explicitly reset the timer to make it start from 0. Resetting the timer will not make the timer start again. You need to explicitly start the timer again with a Timer::start() call.

    Timer::reset(); // Default timer.
    Timer::reset('something');
    Timer::resetAll(); // Resets all timers.

Formats

Currently, the following formats are provided:

  • FORMAT_PRECISE: Precise timer value, without rounding it. e.g. 0.10180473327637
  • FORMAT_MILLISECONDS: Time in milliseconds, rounded to 2 decimals.
  • FORMAT_SECONDS: Time in seconds, rounded to 3 decimals.
  • FORMAT_HUMAN: Time in human-readable format, for example 1.05 minutes.

Examples

Calculate the timer one-off:

    <?php
    use Ayesh\PHP_Timer\Timer;

    Timer::start();
    // do your processing here.
    $time = Timer::read('default', Timer::FORMAT_SECONDS);
    echo "Script took {$time} second(s)";

Stop watch functionality, with stop-and-go timer calculated separately.

    <?php
    use Ayesh\PHP_Timer\Timer;

    Timer::start('full');

    Timer::start('laps');
    sleep(1);
    Timer::stop('laps');

    sleep(2); // This time is not calculated under 'laps'

    Timer::start('laps');
    sleep(1);
    Timer::stop('laps');

    echo Timer::read('full', Timer::FORMAT_SECONDS); // 4 seconds.
    echo "<br />";
    echo Timer::read('laps', Timer::FORMAT_SECONDS); // 2 seconds (1 + 1)

Development and tests

All issues are PRs are welcome. Travis CI and PHPUnit tests are included. If you are adding new features, please make sure to add the test coverage.

Credits

By Ayesh Karunaratne and contributors.

kthxbye

ayesh/php-timer 适用场景与选型建议

ayesh/php-timer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 235.06k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2017 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「time」 「timer」 「benchmark」 「stopwatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 ayesh/php-timer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 235.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 37
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-12