PHPackages                             ctw/ctw-middleware-responsetime - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. ctw/ctw-middleware-responsetime

ActiveLibrary[HTTP &amp; Networking](/categories/http)

ctw/ctw-middleware-responsetime
===============================

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

4.0.4(5mo ago)1163BSD-3-ClausePHPPHP ^8.3CI passing

Since Mar 13Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/jonathanmaron/ctw-middleware-responsetime)[ Packagist](https://packagist.org/packages/ctw/ctw-middleware-responsetime)[ RSS](/packages/ctw-ctw-middleware-responsetime/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (33)Used By (0)

Package "ctw/ctw-middleware-responsetime"
=========================================

[](#package-ctwctw-middleware-responsetime)

[![Latest Stable Version](https://camo.githubusercontent.com/84967611e9d6a3b6d31fee47507f8ef8096dfa536a38843b95159b24366e6ed7/68747470733a2f2f706f7365722e707567782e6f72672f6374772f6374772d6d6964646c65776172652d726573706f6e736574696d652f762f737461626c65)](https://packagist.org/packages/ctw/ctw-middleware-responsetime)[![GitHub Actions](https://github.com/jonathanmaron/ctw-middleware-responsetime/actions/workflows/tests.yml/badge.svg)](https://github.com/jonathanmaron/ctw-middleware-responsetime/actions/workflows/tests.yml)[![Scrutinizer Build](https://camo.githubusercontent.com/3e70e5c2baab2d2c83843600c3230ce6e3b4967720d6ced416918644f738d160/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d726573706f6e736574696d652f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-responsetime/build-status/master)[![Scrutinizer Quality](https://camo.githubusercontent.com/b3e0ff44f0650bdd4715cbda8619158619cdf19794010e2f3c8d2c2c59ee9bbb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d726573706f6e736574696d652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-responsetime/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/1a64c2ec7d1ac44972d9592991fa471339c9a4cc9fbb7a0e52a85153fa92ab1b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d726573706f6e736574696d652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-responsetime/?branch=master)

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

Introduction
------------

[](#introduction)

### Why This Library Exists

[](#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

[](#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

[](#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

[](#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
------------

[](#requirements)

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

Installation
------------

[](#installation)

Install by adding the package as a [Composer](https://getcomposer.org) requirement:

```
composer require ctw/ctw-middleware-responsetime
```

Usage Examples
--------------

[](#usage-examples)

### Basic Pipeline Registration (Mezzio)

[](#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

[](#configprovider-registration)

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

### Response Header Output

[](#response-header-output)

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

### Inspecting with cURL

[](#inspecting-with-curl)

```
curl -I https://example.com/

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

### Inspecting in Browser DevTools

[](#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

[](#header-format)

ComponentDescriptionValueProcessing time in millisecondsPrecisionThree decimal places (microsecond resolution)Format`X.XXX ms`Example`45.123 ms`### Timing Calculation

[](#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

[](#use-cases)

#### Log Analysis

[](#log-analysis)

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

#### Performance Alerting

[](#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

[](#load-testing)

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

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance76

Regular maintenance activity

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~55 days

Recently: every ~131 days

Total

32

Last Release

165d ago

Major Versions

1.0.2 → 2.0.02022-02-14

2.0.0 → 3.0.02022-07-07

3.0.22 → 4.0.02024-06-18

PHP version history (4 changes)1.0.0PHP ^7.4 || ^8.0

3.0.0PHP ^8.0

3.0.9PHP ^8.1

4.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/18d8bc9bdee8ab1b0cfccce6a06d2438acbed3a58b0046c4834c5678f6769342?d=identicon)[jonathanmaron](/maintainers/jonathanmaron)

---

Top Contributors

[![jonathanmaron](https://avatars.githubusercontent.com/u/298462?v=4)](https://github.com/jonathanmaron "jonathanmaron (51 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ctw-ctw-middleware-responsetime/health.svg)

```
[![Health](https://phpackages.com/badges/ctw-ctw-middleware-responsetime/health.svg)](https://phpackages.com/packages/ctw-ctw-middleware-responsetime)
```

###  Alternatives

[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.3k63.6M232](/packages/nelmio-api-doc-bundle)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k48.1M234](/packages/api-platform-core)[api-platform/state

API Platform state interfaces

223.4M57](/packages/api-platform-state)[akrabat/ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

1702.5M18](/packages/akrabat-ip-address-middleware)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
