كتابة نصوص k6 الأساسية
أنشئ أول نص k6 لإرسال طلبات HTTP ومحاكاة المستخدمين الافتراضيين.
كتابة نصوص k6 الأساسية درس مجاني في Load Testing & Performance Benchmarking (JMeter & k6) على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Load Testing & Performance Benchmarking (JMeter & k6)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Load Testing & Performance Benchmarking (JMeter & k6) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Start Your k6 Script Journey
Welcome to writing k6 scripts! k6 uses JavaScript to define your load tests, making it familiar for many developers.
A k6 script describes what your virtual users will do and how the load should be simulated.
- Define user actions like visiting pages or logging in.
- Specify the number of virtual users (VUs) and test duration.
- Validate server responses to ensure correctness.
Anatomy of a k6 Script
Every k6 script typically has two main parts:
- The
optionsobject: Here you configure test-wide settings like virtual users (VUs) and test duration. - The
defaultfunction: This is where your virtual users' actions are defined. It's executed repeatedly by each VU.
Think of the default function as the "main loop" for each virtual user.
Sending HTTP GET Requests
The most common action in a performance test is sending an HTTP request. k6 provides the k6/http module for this.
To send a simple GET request, you'll use http.get('URL'). This simulates a browser fetching a web page or an API endpoint.
Remember to import the http module at the top of your script.
Code: Basic GET Request
Let's write our first k6 script to make a GET request to example.com. Copy and run this script using the k6 CLI.
k6 run your_script.js
import http from 'k6/http';
import { sleep } from 'k6/execution';
export default function () {
http.get('https://test.k6.io');
sleep(1); // Wait for 1 second between requests
}What are Virtual Users (VUs)?
In k6, a Virtual User (VU) simulates a real user interacting with your system. Each VU executes the default function independently.
If you have 10 VUs, it's like 10 people simultaneously using your application. k6 efficiently manages these VUs.
By controlling the number of VUs, you can simulate different levels of load on your application.
Configuring Test Load with Options
The options object is crucial for defining your test scenario. It sits at the top level of your script.
Two key options are:
vus: The total number of virtual users to run concurrently.duration: How long the test should run (e.g., '30s', '5m', '2h').
These settings tell k6 how to distribute and execute your default function.
Code: VUs and Test Duration
This script runs with 5 virtual users for a duration of 10 seconds, hitting the test website repeatedly.
Observe how k6 reports the VUs and duration when you run this script.
import http from 'k6/http';
import { sleep } from 'k6/execution';
export const options = {
vus: 5,
duration: '10s',
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
}Sending Data with POST Requests
Beyond GET, you often need to send data to the server, like submitting a form or creating a resource. For this, you use http.post().
The http.post() function takes three arguments:
- The URL.
- The request body (usually a JSON string or form data).
- Optional parameters like headers.
Code: Simple POST Request
Here's an example of a POST request sending JSON data. Notice how we specify the Content-Type header to tell the server we're sending JSON.
import http from 'k6/http';
import { sleep } from 'k6/execution';
export const options = {
vus: 1,
duration: '5s',
};
export default function () {
const url = 'https://httpbin.org/post';
const payload = JSON.stringify({
name: 'Coddy',
job: 'Learner',
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};
http.post(url, payload, params);
sleep(1);
}Test Your Scripting Skills
Which of the following statements about basic k6 scripting are TRUE?
Recap: Your First k6 Scripts
Great job! You've learned the fundamentals of writing k6 scripts:
- The basic structure with
optionsand thedefaultfunction. - How to send simple HTTP GET and POST requests using
k6/http. - Configuring test load with
vusandduration.
In the next lesson, we'll dive into validating responses with Checks and setting performance targets with Thresholds!
الأسئلة الشائعة
هل درس «كتابة نصوص k6 الأساسية» مجاني؟
نعم — نص درس «كتابة نصوص k6 الأساسية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Load Testing & Performance Benchmarking (JMeter & k6)، انتقل إلى CoddyKit PRO. تتضمن دورة Load Testing & Performance Benchmarking (JMeter & k6) 4 دروس في المجموع.
ماذا ستتعلم في «كتابة نصوص k6 الأساسية»؟
أنشئ أول نص k6 لإرسال طلبات HTTP ومحاكاة المستخدمين الافتراضيين. تتمرن على Load Testing & Performance Benchmarking (JMeter & k6) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Load Testing & Performance Benchmarking (JMeter & k6)؟
لا تُشترط خبرة سابقة. Load Testing & Performance Benchmarking (JMeter & k6) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «كتابة نصوص k6 الأساسية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Load Testing & Performance Benchmarking (JMeter & k6) هذا؟
نعم. كل درس في Load Testing & Performance Benchmarking (JMeter & k6) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تثبيت k6 وواجهة CLI
- كتابة نصوص k6 الأساسية
- عمليات التحقق والحدود والمقاييس
- المجموعات والوسوم لتنظيم الاختبارات