0Pricing
Load Testing & Performance Benchmarking (JMeter & k6) · 课时

安装 k6 与使用 CLI

安装 k6,并熟悉其命令行界面以执行测试

安装 k6 与使用 CLI 是 CoddyKit 上的免费 Load Testing & Performance Benchmarking (JMeter & k6) 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Load Testing & Performance Benchmarking (JMeter & k6) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Meet k6: Load Testing Tool

Welcome! In this lesson, we'll get started with k6, a powerful open-source load testing tool. k6 is built with Go and allows you to write performance tests using JavaScript.

It's designed for developers and QA engineers who want to integrate performance testing into their modern development workflows.

Why Choose k6?

k6 offers several advantages for performance testing:

  • JavaScript API: Write tests in familiar JavaScript.
  • CLI-focused: Easy to integrate into CI/CD pipelines.
  • Performance: Built on Go, it's efficient and fast.
  • Modern Approach: Treats performance tests as code, enabling version control and code reviews.

Installation Overview

Before we write our first test, we need to install k6 on your system. k6 supports various operating systems like Windows, macOS, and Linux.

The recommended way to install k6 is through package managers, which makes the process straightforward and quick.

macOS and Linux Setup

For macOS users, the easiest way to install k6 is using Homebrew. Open your terminal and run:

brew install k6

On Debian/Ubuntu Linux, you can install k6 from its official repository with these commands:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E34A0A echo "deb https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list sudo apt update sudo apt install k6

Windows Installation

Windows users have a couple of easy options using popular package managers.

Using Chocolatey (run in an elevated PowerShell or CMD):

choco install k6

Using Scoop (run in PowerShell):

scoop install k6

Check Your k6 Version

After installation, it's important to verify that k6 is correctly installed and accessible from your command line. Open a new terminal or command prompt and type:

k6 version

You should see the installed k6 version number displayed, confirming a successful setup.

The `k6 run` Command

The core of k6 is its powerful Command Line Interface (CLI). The most frequently used command is k6 run.

This command is used to execute your k6 test scripts. You simply point it to your JavaScript file, and k6 takes care of the rest.

k6 run your_script.js

A Minimal k6 Test

Let's create a super simple k6 script. This script will make a single HTTP GET request to example.com. We'll save this as script.js.

Notice the import http statement – k6 provides built-in modules like http for making requests.

import http from 'k6/http';
import { sleep } from 'k6/execution';

export default function () {
  http.get('https://test.k6.io');
  sleep(1);
}

Run Your k6 Test

Now that you have your script.js file, open your terminal in the same directory where you saved the file. Then, execute the script using the k6 run command:

k6 run script.js

k6 will execute the test, and you'll see output in your terminal showing metrics like requests, duration, and checks.

Customizing Your Run

The k6 run command supports various options to customize your test. Two common ones are:

  • --vus: Sets the number of Virtual Users.
  • --duration: Defines how long the test should run.

For example, to run with 5 VUs for 10 seconds:

k6 run --vus 5 --duration 10s script.js

CLI Command Check

You've learned how to install k6 and run a basic script. Let's test your understanding of the essential k6 CLI command.

k6 Ready: What's Next?

Fantastic! You've successfully installed k6 and run your very first performance test script. We covered:

  • What k6 is and its benefits.
  • How to install k6 on different operating systems.
  • Verifying your installation.
  • Using the essential k6 run command to execute a JavaScript test script.

In the next lesson, we'll dive deeper into writing more complex k6 scripts and understanding their structure.

常见问题解答

「安装 k6 与使用 CLI」课时是免费的吗?

是的 — 「安装 k6 与使用 CLI」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Load Testing & Performance Benchmarking (JMeter & k6) 课程的其余内容,请升级到 CoddyKit PRO。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。

「安装 k6 与使用 CLI」这节课中我会学到什么?

安装 k6,并熟悉其命令行界面以执行测试 你通过在浏览器中直接运行的动手代码来练习 Load Testing & Performance Benchmarking (JMeter & k6),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Load Testing & Performance Benchmarking (JMeter & k6) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Load Testing & Performance Benchmarking (JMeter & k6) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「安装 k6 与使用 CLI」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Load Testing & Performance Benchmarking (JMeter & k6) 课中编写并运行代码吗?

能。每节 Load Testing & Performance Benchmarking (JMeter & k6) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 安装 k6 与使用 CLI
  2. 编写基础 k6 脚本
  3. 检查、阈值与指标
  4. 使用组和标签组织测试
← 返回 Load Testing & Performance Benchmarking (JMeter & k6)