ethereum_test.js
Overview
The `ethereum_test.js` file is a performance/load testing script designed specifically for Ethereum blockchain API endpoints. It uses the [k6](https://k6.io/) load testing framework to simulate high throughput GET requests against the Ethereum account API provided by ShapeShift’s Ethereum service.
Its main purpose is to validate the API's ability to handle sustained, concurrent traffic by:
Sending a large number of requests per second (1000 RPS) over a 5-minute duration.
Distributing requests evenly over a large dataset of Ethereum addresses.
Enforcing performance thresholds such as maximum acceptable latency and failure rates.
Providing detailed result summaries for integration with CI pipelines or performance dashboards.
This script plays a critical role in continuous performance validation and ensuring the Ethereum API's robustness under load.
Detailed Explanation
Imported Modules
httpfromk6/http: Provides HTTP request functions to simulate client calls.execfromk6/execution: Gives access to execution context, such as the iteration count in the scenario.
Constants
addresses
An array of 150+ Ethereum address strings (checksummed) used to distribute GET requests among different accounts. This variety mimics realistic traffic patterns by querying diverse accounts rather than repeatedly hitting the same endpoint.
Exported Variables and Functions
options
export const options = {
scenarios: {
rps: {
executor: 'constant-arrival-rate',
duration: "5m",
rate: 1000,
preAllocatedVUs: 2500,
maxVUs: 10000,
},
},
thresholds: {
'http_req_duration{scenario:rps}': ['p(95) < 800'],
'http_req_failed{scenario:rps}': ['rate<0.01'],
'http_req_duration{status:200}': ['max>=0'],
'http_req_duration{status:429}': ['max>=0'],
},
summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'],
}
Purpose: Defines the performance test configuration for k6.
Key fields:
scenarios.rps: Defines a single scenario named "rps" using aconstant-arrival-rateexecutor.duration: Test runs for 5 minutes.rate: Generates 1000 iterations (requests) per second.preAllocatedVUs: Pre-allocates 2500 virtual users (VUs) to efficiently handle the load.maxVUs: Caps maximum VUs to 10,000 to avoid resource exhaustion.
thresholds: Performance goals and failure criteria.95th percentile HTTP request duration should be below 800ms.
Failure rate (requests that failed) should be under 1%.
Tracking max durations for HTTP 200 and 429 status codes.
summaryTrendStats: Specifies which statistical metrics to include in the summary report.
Default Exported Function
export default function () {
http.get(`https://api.ethereum.shapeshift.com/api/v1/account/${addresses[exec.scenario.iterationInTest % addresses.length]}`)
}
Purpose: Defines the main test iteration function executed by each virtual user.
Functionality:
Performs an HTTP GET request to the Ethereum account endpoint.
Uses modulo arithmetic on
exec.scenario.iterationInTestto rotate through theaddressesarray, ensuring requests target a variety of account addresses evenly.
Parameters: None.
Returns: None. The HTTP request response is handled internally by k6 to collect metrics.
Usage: This function is invoked repeatedly by k6 according to the scenario configuration to generate load.
handleSummary Function
export function handleSummary(data) {
return {
"/out/ethereum/results/result.json": JSON.stringify(data),
}
}
Purpose: Custom summary handler to write test results to a JSON file after the test completes.
Parameters:
data(Object): The summary data collected by k6 after test execution, containing all metrics and thresholds.
Returns: An object mapping output file paths to stringified data.
Usage: Enables easy integration with CI/CD or performance dashboards by saving structured results to a known location.
Important Implementation Details
Address Cycling Algorithm:
The test cycles through a large set of Ethereum addresses by using the modulo operator on the current iteration count (exec.scenario.iterationInTest % addresses.length). This distributes the load evenly over many unique accounts, simulating more realistic user access patterns and avoiding caching or hotspot bias.Load Pattern - Constant Arrival Rate:
Theconstant-arrival-rateexecutor maintains a steady flow of requests per second, independent of virtual user speed or response times. This ensures the API is tested under sustained pressure, matching real-world high-traffic scenarios.High Concurrency Settings:
Using a high number of pre-allocated and maximum VUs allows the script to scale up concurrency as needed to maintain the 1000 RPS rate, preventing bottlenecks in the test itself.Thresholds for Performance Validation:
The thresholds enforce strict latency and error rate targets, ensuring that any regressions or performance degradations are flagged during automated testing.Summary Statistics:
The selected stats provide comprehensive insight into latency distribution and request counts, useful for detailed performance analysis.
Interaction with Other System Components
Target API Service:
This script targets the Ethereum API server hosted by ShapeShift (https://api.ethereum.shapeshift.com/api/v1/account/). It validates the Ethereum account endpoint's performance and reliability.Integration with Developer Tooling:
It complements the Docker Compose-based local development and testing environments by enabling performance benchmarking against local or staging Ethereum API deployments.Continuous Integration:
The output JSON file generated byhandleSummarycan be consumed by CI pipelines or performance dashboards for automated monitoring and regression detection.Dependencies:
Requires k6 runtime for execution. The list of addresses is hardcoded but could be adapted to load dynamically or from external files.
Usage Example
To run this load test locally (assuming k6 is installed):
k6 run ethereum_test.js
This will execute the constant arrival rate scenario sending 1000 requests per second for 5 minutes against the Ethereum account API, cycling through the predefined addresses.
After completion, a summary JSON report will be saved to `/out/ethereum/results/result.json`.
Mermaid Diagram: Flowchart of Main Functions and Their Relationships
flowchart TD
A[Start of Test] --> B[Initialize Scenario Options]
B --> C[For each iteration in scenario]
C --> D[Calculate address index: iterationInTest % addresses.length]
D --> E[Perform HTTP GET request to Ethereum account endpoint]
E --> F[Collect metrics: duration, status, errors]
F --> G{Thresholds met?}
G -->|Yes| C
G -->|No| H[Log threshold violation]
C --> I{Test duration reached?}
I -->|No| C
I -->|Yes| J[Invoke handleSummary]
J --> K[Write result JSON to /out/ethereum/results/result.json]
K --> L[End of Test]
Summary
The `ethereum_test.js` is a focused k6 load testing script that simulates sustained high request rates against the ShapeShift Ethereum API. By cycling through a comprehensive list of Ethereum addresses, it realistically mimics user traffic patterns. Its configuration enforces strict latency and failure thresholds to ensure API reliability and performance. The script integrates smoothly with local development environments and CI workflows through detailed result reporting, making it a vital tool for continuous performance validation of the Ethereum API service.