定制 ctw/ctw-middleware-responsetime 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ctw/ctw-middleware-responsetime

Composer 安装命令:

composer require ctw/ctw-middleware-responsetime

包简介

This PSR-15 middleware adds an X-Response-Time in milliseconds to the Response header.

README 文档

README

Latest Stable Version GitHub Actions Scrutinizer Build Scrutinizer Quality Code Coverage

PSR-15 middleware that adds an X-Response-Time header showing request processing duration in milliseconds.

Introduction

Why This Library Exists

Understanding how long your application takes to process requests is essential for performance monitoring and optimization. While external tools can measure total response time, they include network latency. Measuring at the application level provides the true processing time.

This middleware adds an X-Response-Time header to every response showing:

  • Processing duration: Time from request arrival to response generation
  • Millisecond precision: Three decimal places for accurate measurement
  • Consistent format: Standardized header for easy parsing by monitoring tools
  • Zero overhead: Uses PHP's native microtime() for minimal performance impact

Problems This Library Solves

  1. Invisible performance: Without timing data, slow requests go unnoticed
  2. External tool inaccuracy: Network-based measurements include latency, not just processing time
  3. Manual instrumentation: Adding timing code to every handler is tedious and error-prone
  4. Inconsistent measurement: Different timing implementations across the application
  5. Missing production visibility: Development profilers aren't available in production

Where to Use This Library

  • Performance monitoring: Track response times across your application
  • SLA verification: Ensure responses meet required time thresholds
  • Debugging slowness: Quickly identify slow requests from response headers
  • Load testing: Analyze response time distribution under load
  • Client-side monitoring: JavaScript can read the header for real-user monitoring
  • Log correlation: Include response time in access logs for analysis

Design Goals

  1. High precision: Milliseconds with three decimal places (microsecond resolution)
  2. Accurate timing: Uses REQUEST_TIME_FLOAT as start time for precision
  3. Standard format: X.XXX ms format is human-readable and parseable
  4. Minimal overhead: Simple subtraction and header addition
  5. Universal application: Works for HTML, JSON, and all response types

Requirements

  • PHP 8.3 or higher
  • ctw/ctw-middleware ^4.0

Installation

Install by adding the package as a Composer requirement:

composer require ctw/ctw-middleware-responsetime

Usage Examples

Basic Pipeline Registration (Mezzio)

use Ctw\Middleware\ResponseTimeMiddleware\ResponseTimeMiddleware;

// In config/pipeline.php - place early in the pipeline for accurate timing
$app->pipe(ResponseTimeMiddleware::class);

ConfigProvider Registration

// config/config.php
return [
    // ...
    \Ctw\Middleware\ResponseTimeMiddleware\ConfigProvider::class,
];

Response Header Output

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
X-Response-Time: 45.123 ms

Inspecting with cURL

curl -I https://example.com/

# Response includes:
# X-Response-Time: 45.123 ms

Inspecting in Browser DevTools

  1. Open Developer Tools (F12)
  2. Navigate to the Network tab
  3. Select a request
  4. View Response Headers
  5. Look for X-Response-Time

Header Format

Component Description
Value Processing time in milliseconds
Precision Three decimal places (microsecond resolution)
Format X.XXX ms
Example 45.123 ms

Timing Calculation

The middleware calculates response time as:

Response Time = (end_time - start_time) × 1000

Where:

  • start_time = $_SERVER['REQUEST_TIME_FLOAT'] (request arrival)
  • end_time = microtime(true) (response generation complete)

Use Cases

Log Analysis

# Extract response times from access logs
grep "X-Response-Time" /var/log/nginx/access.log | awk '{print $NF}'

Performance Alerting

// Client-side monitoring
const responseTime = parseFloat(
  response.headers.get('X-Response-Time')
);
if (responseTime > 1000) {
  console.warn('Slow response:', responseTime, 'ms');
}

Load Testing

# Apache Bench with response time analysis
ab -n 1000 -c 10 https://example.com/ | grep "Time per request"

ctw/ctw-middleware-responsetime 适用场景与选型建议

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

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

围绕 ctw/ctw-middleware-responsetime 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-03-20