使用 k6 在云端执行
探索在云端运行 k6 测试,以生成分布式负载并获得高级报告
使用 k6 在云端执行 是 CoddyKit 上的免费 Load Testing & Performance Benchmarking (JMeter & k6) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Load Testing & Performance Benchmarking (JMeter & k6) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Run k6 Tests in the Cloud
Welcome to cloud execution with k6! So far, you've run k6 tests locally on your machine. While great for development, local tests have limits.
When you need to simulate thousands of users or test from different global locations, running tests from a single machine isn't enough. This is where k6 Cloud comes in!
Cloud Advantages for Load Tests
Running your k6 tests in the cloud offers several key benefits:
- Scalability: Easily generate massive loads beyond what a single machine can handle.
- Global Distribution: Simulate users from various geographic regions (load zones).
- Managed Infrastructure: No need to set up or maintain your own load generators.
- Advanced Reporting: Get rich, real-time dashboards and historical data analysis.
- Collaboration: Share test results and collaborate with your team easily.
Connect to k6 Cloud
To get started with k6 Cloud, you'll need an account. Once you sign up, you'll obtain an API Token from your k6 Cloud dashboard. This token authenticates your k6 CLI with the cloud service.
You can set it as an environment variable or use the k6 login cloud command in your terminal.
Execute Tests Remotely
Running a k6 script in the cloud is straightforward. Instead of using k6 run, you'll use the k6 cloud command followed by your script's filename.
This command uploads your local script to the k6 Cloud, where it's then executed by the managed load generators. You'll get a URL to view your live test results.
Your First Cloud Test
Let's take a simple k6 script and run it in the cloud. This script makes a single HTTP GET request to a test site.
To run it, save it as cloud_test.js, then execute k6 cloud cloud_test.js in your terminal.
import http from 'k6/http';
import { sleep } from 'k6/execution';
export const options = {
vus: 1,
duration: '10s',
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
}Global Load Generation
A powerful feature of k6 Cloud is the ability to generate load from multiple geographic locations, known as load zones. This lets you test how your application performs for users around the world.
You can specify which load zones to use directly in your k6 script, ensuring your tests accurately reflect your user base.
Define Your Load Zones
You can configure load zones within the options object of your k6 script. Under ext.cloud.load_zones, you define the VUs and max VUs for each desired zone.
This example sends load from Ashburn (USA) and London (UK) simultaneously.
import http from 'k6/http';
import { sleep } from 'k6/execution';
export const options = {
ext: {
cloud: {
load_zones: {
'ashburn': {
vus: 5,
max_vus: 10,
duration: '30s',
},
'london': {
vus: 5,
max_vus: 10,
duration: '30s',
},
},
},
},
};
export default function () {
http.get('https://test.k6.io/public/crocodiles/');
sleep(1);
}Insights from Cloud Reports
After running a test in k6 Cloud, you'll get a link to a detailed web dashboard. This dashboard provides:
- Real-time Metrics: See response times, throughput, and error rates as the test runs.
- Trend Analysis: Compare current test runs with historical data.
- Detailed Breakdowns: Drill down into individual request performance.
- Visualizations: Graphs and charts make it easy to spot performance bottlenecks.
Organize Cloud Tests
k6 Cloud helps you keep your tests organized. You can group tests into projects, making it easier to manage different applications or teams.
Each test run is saved, allowing you to review historical data, track performance over time, and collaborate with team members by sharing access to your test results.
Cloud Execution Benefits
Which of the following are key benefits of running k6 tests in the cloud?
Summary: Cloud Testing
Congratulations! You've learned about the power of running k6 tests in the cloud.
k6 Cloud allows you to overcome the limitations of local testing by providing scalable, globally distributed load generation, managed infrastructure, and rich reporting. You can easily push your scripts to the cloud and configure load zones for realistic global testing. This makes it an essential tool for comprehensive performance validation.
常见问题解答
「使用 k6 在云端执行」课时是免费的吗?
是的 — 「使用 k6 在云端执行」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Load Testing & Performance Benchmarking (JMeter & k6) 课程的其余内容,请升级到 CoddyKit PRO。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。
「使用 k6 在云端执行」这节课中我会学到什么?
探索在云端运行 k6 测试,以生成分布式负载并获得高级报告 你通过在浏览器中直接运行的动手代码来练习 Load Testing & Performance Benchmarking (JMeter & k6),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Load Testing & Performance Benchmarking (JMeter & k6) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Load Testing & Performance Benchmarking (JMeter & k6) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「使用 k6 在云端执行」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Load Testing & Performance Benchmarking (JMeter & k6) 课中编写并运行代码吗?
能。每节 Load Testing & Performance Benchmarking (JMeter & k6) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 虚拟用户场景(VUs)
- k6 中的数据参数化
- 使用 k6 在云端执行
- k6 中的自定义指标与趋势