A comprehensive benchmarking tool for API performance testing with two modes:
- Comparison Mode: Compare latency between direct OpenAI API calls and Portkey proxy calls
- Load Test Mode: Dedicated load testing for Portkey endpoints
This tool helps you understand Portkey performance characteristics and optimize your API usage patterns.
This benchmark tool:
- Compares response times between OpenAI direct API and Portkey proxy (comparison mode)
- Performs dedicated load testing for Portkey endpoints (load test mode)
- Measures network latency overhead and processing times
- Tests performance under concurrent load with configurable parameters
- Provides detailed statistical analysis and reports
- Identifies OpenAI processing time vs. network latency breakdown
- Node.js (version 14 or higher)
- npm or yarn package manager
- Valid OpenAI API key
- Valid Portkey API key
-
Install dependencies:
npm install
-
Configure your API keys: Edit
config.jsonand replace the placeholder values:{ "portkeyApiKey": "your-actual-portkey-api-key", "portkeyProviderSlug": "your-provider-slug-from-model-catalog", "openaiApiKey": "sk-your-actual-openai-api-key", }If you have a default config attached with the Portkey API key, which has a virtual key inside, you can skip the Portkey provider slug. Provider slug should be prefixed be `@`. -
Run the benchmark:
npm start
or
node benchmark.js
The config.json file contains all benchmarking parameters:
mode: Test mode to run (default: "comparison")"comparison": Compare OpenAI vs Portkey latency (requires both API keys)"loadtest": Load test Portkey only (requires Portkey API key)
openaiApiKey: Your OpenAI API key (required for comparison mode)portkeyApiKey: Your Portkey API key (required for both modes)
portkeyBaseURL: Portkey API endpoint (default: "https://api.portkey.ai/v1")portkeyConfigId: Optional Portkey configuration IDmodel: OpenAI model to test (default: "gpt-3.5-turbo")
concurrency: Number of concurrent requests (default: 5)maxRequests: Maximum number of requests to send (default: 100)testDuration: Maximum test duration in seconds (default: 60)maxTokens: Maximum tokens per response (default: 150)temperature: Model temperature setting (default: 0.7)
β οΈ Important for Accurate Results: We strongly recommend running at least 500 requests to get statistically meaningful results and avoid skewed measurements. Small sample sizes can lead to unreliable latency comparisons due to network variability and cold start effects.
The prompt can be either:
- String format:
"Your prompt text here" - Message array format:
[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Your question here"} ]
Your existing config.json already has all the necessary fields. Just change the mode field to switch between test types:
{
"mode": "loadtest",
// ... rest of your existing config
// Only portkeyApiKey is required for this mode
}{
"mode": "comparison",
// ... rest of your existing config
// Both portkeyApiKey and openaiApiKey are required
}π‘ Tip: You can also omit the
modefield entirely to default to comparison mode.
node benchmark.jsSimply edit your config.json file and change the mode field:
- Edit
config.jsonand set"mode": "loadtest" - Ensure
portkeyApiKeyis configured - Run:
node benchmark.js
- Edit
config.jsonand set"mode": "comparison"(or omit mode for default) - Ensure both
portkeyApiKeyandopenaiApiKeyare configured - Run:
node benchmark.js
node benchmark.js custom-config.jsonThe benchmark provides real-time progress updates:
- Preflight test results
- Worker progress with request timings
- Success/failure rates for each provider
- Periodic progress summaries
π PORTKEY LATENCY BENCHMARK REPORT
==================================================
π TEST SUMMARY:
Total Request Pairs: 500
Test Duration: 2023-12-01T10:30:00.000Z
π PERFORMANCE COMPARISON:
βββββββββββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ¬ββββββββββββββ
β Metric β OpenAI β Portkey β Difference β
βββββββββββββββββββββββΌβββββββββββββββΌβββββββββββββββΌββββββββββββββ€
β Avg Total Time β 1247.20ms β 1267.30ms β +20.10ms β
β Avg Network Latency β 182.50ms β 202.40ms β +19.90ms β
β Success Rate β 100% β 100%% β - β
β Median Time β 1198.00ms β 1219.00ms β +21.00ms β
β P95 Time β 1785.00ms β 1809.00ms β +24.00ms β
β P99 Time β 2087.00ms β 2115.00ms β +28.00ms β
βββββββββββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ΄ββββββββββββββ
π― KEY INSIGHTS:
β’ Portkey adds an average of 20.10ms latency (1.6% increase)
β’ Network latency difference: 19.90ms
β’ Success rate difference: -0.2%
Detailed JSON reports are saved to the results/ directory with:
- Complete configuration used
- Raw timing data for all requests
- Statistical summaries
- Success/failure details
- Total Time: End-to-end request time including network and processing
- OpenAI Processing Time: Time spent by OpenAI servers processing the request
- Network Latency: Time spent in network transmission (Total - Processing)
- Success Rate: Percentage of requests that completed successfully
- P95/P99: 95th and 99th percentile response times (worst-case scenarios)
"Both OpenAI and Portkey preflight tests failed"
- Check your API keys are valid and properly formatted
- Verify network connectivity
- Ensure API quotas are not exceeded
"Processing time extraction failed"
- Some OpenAI responses may not include processing time headers
- The benchmark will continue but network latency calculations will be limited
"Config file not found"
- Ensure
config.jsonexists in the same directory - Use the correct path if specifying a custom config file
High failure rates
- Check API rate limits and quotas
- Reduce concurrency if hitting rate limits
- Verify the model specified is available
For detailed header information and debugging:
- Check console output during preflight testing
- Review the raw results in the generated JSON reports
- Headers and processing times are logged during preflight tests
benchmark/
βββ benchmark.js # Main benchmark script
βββ config.json # Configuration file (supports all modes)
βββ package.json # Node.js dependencies
βββ package-lock.json # Dependency lock file
βββ results/ # Generated benchmark reports
βββ README.md # This file
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly with different configurations
- Submit a pull request
MIT License - see package.json for details.
- API Costs: This tool makes real API calls that will consume your OpenAI/Portkey credits
- Rate Limits: Be mindful of API rate limits when setting high concurrency
- Fair Testing: Use identical prompts and settings for accurate comparisons
- Processing Time: Accuracy depends on API providers returning processing time headers