Tools: Grafana k6 Has a Free API That Load Tests Your APIs With JavaScript - Full Analysis

Tools: Grafana k6 Has a Free API That Load Tests Your APIs With JavaScript - Full Analysis

Quick Start

Basic Load Test

Ramping (Realistic Traffic Patterns)

Thresholds (Pass/Fail Criteria)

API Testing

k6 vs JMeter vs Artillery

The Bottom Line k6 is a load testing tool that uses JavaScript for test scripts. Run locally, in CI/CD, or in the cloud. Write tests like you write code — not XML configs. Use in CI/CD: k6 exits with non-zero if thresholds fail. k6 is the developer-friendly load testing tool. JavaScript scripts, low resource usage, CI/CD native, and thresholds that fail your pipeline when performance degrades. Need to automate data collection or build custom scrapers? Check out my Apify actors for ready-made tools, or email [email protected] for custom solutions. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

# Install -weight: 500;">brew -weight: 500;">install k6 # macOS -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install k6 # Ubuntu # Run a test k6 run script.js # Install -weight: 500;">brew -weight: 500;">install k6 # macOS -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install k6 # Ubuntu # Run a test k6 run script.js # Install -weight: 500;">brew -weight: 500;">install k6 # macOS -weight: 600;">sudo -weight: 500;">apt -weight: 500;">install k6 # Ubuntu # Run a test k6 run script.js import http from 'k6/http' import { check, sleep } from 'k6' export const options = { vus: 50, // 50 virtual users duration: '30s', // for 30 seconds } export default function () { const res = http.get('https://api.example.com/posts') check(res, { '-weight: 500;">status is 200': (r) => r.-weight: 500;">status === 200, 'response time < 500ms': (r) => r.timings.duration < 500, }) sleep(1) } import http from 'k6/http' import { check, sleep } from 'k6' export const options = { vus: 50, // 50 virtual users duration: '30s', // for 30 seconds } export default function () { const res = http.get('https://api.example.com/posts') check(res, { '-weight: 500;">status is 200': (r) => r.-weight: 500;">status === 200, 'response time < 500ms': (r) => r.timings.duration < 500, }) sleep(1) } import http from 'k6/http' import { check, sleep } from 'k6' export const options = { vus: 50, // 50 virtual users duration: '30s', // for 30 seconds } export default function () { const res = http.get('https://api.example.com/posts') check(res, { '-weight: 500;">status is 200': (r) => r.-weight: 500;">status === 200, 'response time < 500ms': (r) => r.timings.duration < 500, }) sleep(1) } export const options = { stages: [ { duration: '2m', target: 100 }, // ramp up to 100 users { duration: '5m', target: 100 }, // stay at 100 { duration: '2m', target: 200 }, // spike to 200 { duration: '5m', target: 200 }, // stay at 200 { duration: '2m', target: 0 }, // ramp down ], } export const options = { stages: [ { duration: '2m', target: 100 }, // ramp up to 100 users { duration: '5m', target: 100 }, // stay at 100 { duration: '2m', target: 200 }, // spike to 200 { duration: '5m', target: 200 }, // stay at 200 { duration: '2m', target: 0 }, // ramp down ], } export const options = { stages: [ { duration: '2m', target: 100 }, // ramp up to 100 users { duration: '5m', target: 100 }, // stay at 100 { duration: '2m', target: 200 }, // spike to 200 { duration: '5m', target: 200 }, // stay at 200 { duration: '2m', target: 0 }, // ramp down ], } export const options = { thresholds: { http_req_duration: ['p(95)<500'], // 95% of requests < 500ms http_req_failed: ['rate<0.01'], // <1% error rate 'http_req_duration{-weight: 500;">status:200}': ['p(99)<1000'], }, } export const options = { thresholds: { http_req_duration: ['p(95)<500'], // 95% of requests < 500ms http_req_failed: ['rate<0.01'], // <1% error rate 'http_req_duration{-weight: 500;">status:200}': ['p(99)<1000'], }, } export const options = { thresholds: { http_req_duration: ['p(95)<500'], // 95% of requests < 500ms http_req_failed: ['rate<0.01'], // <1% error rate 'http_req_duration{-weight: 500;">status:200}': ['p(99)<1000'], }, } import http from 'k6/http' import { check } from 'k6' export default function () { // POST with JSON const payload = JSON.stringify({ name: 'test', email: '[email protected]' }) const params = { headers: { 'Content-Type': 'application/json' } } const res = http.post('https://api.example.com/users', payload, params) check(res, { 'created': (r) => r.-weight: 500;">status === 201, 'has id': (r) => JSON.parse(r.body).id !== undefined, }) } import http from 'k6/http' import { check } from 'k6' export default function () { // POST with JSON const payload = JSON.stringify({ name: 'test', email: '[email protected]' }) const params = { headers: { 'Content-Type': 'application/json' } } const res = http.post('https://api.example.com/users', payload, params) check(res, { 'created': (r) => r.-weight: 500;">status === 201, 'has id': (r) => JSON.parse(r.body).id !== undefined, }) } import http from 'k6/http' import { check } from 'k6' export default function () { // POST with JSON const payload = JSON.stringify({ name: 'test', email: '[email protected]' }) const params = { headers: { 'Content-Type': 'application/json' } } const res = http.post('https://api.example.com/users', payload, params) check(res, { 'created': (r) => r.-weight: 500;">status === 201, 'has id': (r) => JSON.parse(r.body).id !== undefined, }) }