intellex/stopwatch 问题修复 & 功能扩展

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

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

intellex/stopwatch

最新稳定版本:v1.1.0

Composer 安装命令:

composer require intellex/stopwatch

包简介

README 文档

README

Useful for debugging slow scripts and optimizing frameworks.

  • Simple start / stop stopwatch
  • Supports nested measurements
  • Aggregated measurements for repeating functions
  • Plain text report

Exception handling

The Stopwatch will throw an exception on any anomaly. However, by default these exceptions are silently ignored.

In order to set a custom handler use:

StopwatchException::setExceptionHandler(StopwatchExceptionHandling $handler);

For complete list of exceptions, see /src/Exception directory.

API

Simple usage

Use the pair of methods Stopwatch::start(string $name) and Stopwatch::stop(string $name) to measure elapsed time.
The string $name is used to name and identify the measurement and it must match across the two methods.

Stopwatch::stop(string $name) returns an instance Measurement, which holds the information about the measurement.

Example:

Stopwatch::start('Init');
// ...
$measurement = Stopwatch::stop('Init'); // Returns an instance of Measurement

// Available options
$measurement->getStart();    // The start time of the measurement, in milliseconds 
$measurement->getEnd();      // The end time of the measurement in milliseconds 
$measurement->getTime();     // The total time elapsed in milliseconds 

Nested

Nested measurements allow you to get more details where the time is spent, but also to easily know the total time spent. Level of nesting is not limited.

Example:

Stopwatch::start('Total');
    // ...
    Stopwatch::start('Init');
        // ...
    Stopwatch::stop('Init');
    
    Stopwatch::start('Database');
        Stopwatch::start('Connection');
            // ...
        Stopwatch::stop('Connection');
        
        Stopwatch::start('Read');
            // ...
            Stopwatch::start('Cache');
            	// ...
            Stopwatch::stop('Cache');
        Stopwatch::stop('Read');
    Stopwatch::stop('Database');
$measurement = Stopwatch::stop('Total');

// The total time elapsed in 'Total', in milliseconds
$measurement->getTime();                       

// The list of direct children measurements ('Database' and 'Rendering'), with same API as this measurement
$subMeasurements = $measurement->getChildren();

Aggregated

If a certain function is called multiple times, measure the total time.

$loops = 10;
while ($loops--) {
	Stopwatch::startAggregate('loop');
		// ...
	Stopwatch::pauseAggregate('loop');
}

// Get the instance of AggregateMeasurment
$aggregateMeasurement = Stopwatch::getAggregatedMeasurements()['loop'];

$aggregateMeasurement->getTotal()	// The total elapsed time, in milliseconds
$aggregateMeasurement->getCount()	// The number of loops

Print summary

Print the summary of all measurements as plain text: $measurement->report().

<pre><?php echo $measurement->report() ?></pre>

From the nested example above, will produce:

Total: 100
├───Init: 9
└───Database: 91
	├───Connection: 76
	└───Read: 15
		└───Cache: 3

TODO

  1. Tests.
  2. Improve the print summary.

Licence

MIT License

Copyright (c) 2019 Intellex

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.

Credits

Script has been written by the Intellex team.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固