Skip to main content

HTTP RESPONSES

Response Time

Monitor and validate API response times for performance testing and SLA compliance using the responseTime variable.

On this page:

Basic Response Time

Access response time after any HTTP request using the responseTime variable:

Gherkin
Feature: Response time monitoring

Scenario: Simple response time check
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
And assert responseTime < 2000
* karate.log('Response time:', responseTime, 'ms')

The responseTime variable contains the response time in milliseconds and is available immediately after each HTTP request completes.

Response Time Assertions

Validate API performance against SLA requirements:

Gherkin
Feature: SLA compliance

Scenario: SLA compliance check
Given url 'https://jsonplaceholder.typicode.com'
And path 'posts'
And param userId = 1
When method get
Then status 200
And assert responseTime < 2000
* match response == '#[_ > 0]'
* karate.log('Search completed in', responseTime, 'ms')

Use assertions to enforce performance requirements and fail tests when APIs respond too slowly.

Performance Categorization

Categorize API performance using conditional logic:

Gherkin
Feature: Performance categories

Scenario: Performance category validation
Given url 'https://jsonplaceholder.typicode.com'
And path 'users'
When method get
Then status 200
* def category = responseTime < 500 ? 'excellent' : responseTime < 1000 ? 'good' : responseTime < 2000 ? 'acceptable' : 'poor'
* karate.log('Performance category:', category)
* assert category != 'poor'

Ternary operators provide concise performance categorization for different response time thresholds.

Tracking Multiple Endpoints

Monitor performance across different API endpoints:

Gherkin
Feature: Multi-endpoint monitoring

Scenario: Track timing across endpoints
* def timings = []

Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* timings.push({ endpoint: 'users', time: responseTime })

Given url 'https://jsonplaceholder.typicode.com'
And path 'posts', 1
When method get
Then status 200
* timings.push({ endpoint: 'posts', time: responseTime })

* karate.log('Timings:', timings)

Store timing data in arrays to analyze performance across multiple endpoints.

Request Timestamp

The requestTimeStamp variable captures the Java system time when the request was initiated. Use it for correlating with external timing measurements:

Gherkin
Feature: Request timestamp tracking

Scenario: Timing correlation analysis
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* karate.log('Request initiated at:', requestTimeStamp)
* karate.log('Response time:', responseTime, 'ms')
Key Points
  • responseTime measures network round-trip time in milliseconds
  • Timing variables reset after each HTTP request
  • Store values in custom variables if you need them after subsequent requests
  • Use requestTimeStamp for detailed timing correlation with external systems

Common Gotchas

Timing includes network latency: The responseTime variable measures total round-trip time (network latency + server processing), not just server processing time.

Variables reset after requests: Store timing values in variables before making new requests if you need to compare them:

Gherkin
Feature: Preserve timing values

Scenario: Store timing before next request
Given url 'https://jsonplaceholder.typicode.com'
And path 'users', 1
When method get
Then status 200
* def firstTiming = responseTime

Given url 'https://jsonplaceholder.typicode.com'
And path 'posts', 1
When method get
Then status 200
* karate.log('First request:', firstTiming, 'ms')
* karate.log('Second request:', responseTime, 'ms')

Next Steps