DEV Community

Alex Spinov
Alex Spinov

Posted on

Grafana k6 Has a Free API That Load Tests Your APIs With JavaScript

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.

Quick Start

# Install
brew install k6           # macOS
sudo apt install k6       # Ubuntu

# Run a test
k6 run script.js
Enter fullscreen mode Exit fullscreen mode

Basic Load Test

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, {
    'status is 200': (r) => r.status === 200,
    'response time < 500ms': (r) => r.timings.duration < 500,
  })

  sleep(1)
}
Enter fullscreen mode Exit fullscreen mode

Ramping (Realistic Traffic Patterns)

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
  ],
}
Enter fullscreen mode Exit fullscreen mode

Thresholds (Pass/Fail Criteria)

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{status:200}': ['p(99)<1000'],
  },
}
Enter fullscreen mode Exit fullscreen mode

Use in CI/CD: k6 exits with non-zero if thresholds fail.

API Testing

import http from 'k6/http'
import { check } from 'k6'

export default function () {
  // POST with JSON
  const payload = JSON.stringify({ name: 'test', email: 'test@example.com' })
  const params = { headers: { 'Content-Type': 'application/json' } }

  const res = http.post('https://api.example.com/users', payload, params)

  check(res, {
    'created': (r) => r.status === 201,
    'has id': (r) => JSON.parse(r.body).id !== undefined,
  })
}
Enter fullscreen mode Exit fullscreen mode

k6 vs JMeter vs Artillery

Feature k6 JMeter Artillery
Language JavaScript XML/GUI YAML
Resource usage Low (Go) High (Java) Medium (Node)
CI/CD Native Possible Native
Cloud option k6 Cloud BlazeMeter Artillery.io
Learning curve Easy Steep Easy

The Bottom Line

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 spinov001@gmail.com for custom solutions.

Top comments (0)