Testing

k6

A load testing tool: scenarios in JavaScript, a Go core, metrics and thresholds.

What it is

k6 was created by Load Impact (later k6), which Grafana Labs acquired in 2021. Scenarios are written in JavaScript while the core is in Go, so one process runs thousands of virtual users (VUs) with low resource use.

You define scenarios (VU count, duration, load profile) and thresholds: pass conditions such as the 95th percentile response time under 500 ms. If a threshold is breached k6 exits with an error, which makes it handy in CI.

Results can go to the console, JSON, Prometheus or Grafana. It is not a browser: k6 works at the protocol level (HTTP, WebSocket, gRPC), though there is a separate module for browser tests.

Use it when

  • Checking API performance before a release or a traffic peak.
  • Automated performance tests in CI with thresholds.
  • The team writes JavaScript and wants a code-first approach to load.

Avoid it when

  • Functional UI tests: that is not its job.
  • You need GUI scenario recording for non-technical people.

Key concepts

Virtual users (VU)
Parallel executors of the scenario that simulate users.
Scenarios and executors
Load profiles: constant, ramp-up, constant arrival rate.
Thresholds
Success criteria that decide the exit code.
Checks
Response assertions that do not stop the test but are counted in metrics.
Metrics
http_req_duration, http_req_failed and custom counters and trends.

Example

50 users for 30 seconds with a p95 threshold.

Interview questions

  1. 1. What types of load tests are there?

    Smoke (check that it works), load (expected load), stress (beyond normal up to the limit), spike (a sudden surge), soak (long under steady load to find leaks).

  2. 2. What are thresholds in k6?

    Pass conditions over metrics, for example p(95)<500 or http_req_failed<0.01. If breached, k6 returns a non-zero code and CI fails.

  3. 3. Why percentiles instead of the average?

    The average hides slow requests. p95 or p99 show how long the worst 5 or 1 percent of users wait, which is closer to real experience.

  4. 4. How does k6 differ from JMeter?

    k6 is JavaScript code with a Go core and light resource use, fitting CI and Git well. JMeter has a GUI and XML plans, and on the JVM needs more resources.

Interview questions from the bank

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).