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

JMeter 函数与变量

探索 JMeter 的内置函数和用户定义变量,提升脚本的灵活性

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

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

Dynamic Data in JMeter

Welcome! In performance testing, it's rare for all users to do the exact same thing or use the exact same data.

To simulate real-world scenarios, your test scripts need to be flexible and dynamic. This lesson explores how to achieve this using JMeter's built-in functions and user-defined variables.

What are JMeter Variables?

A JMeter Variable is a placeholder that stores a value. Think of it like a temporary name tag for a piece of information.

  • They make your test scripts more readable.
  • They allow you to reuse values throughout your test plan.
  • Changing a value once updates it everywhere it's used.

This is crucial for maintainability and flexibility.

User Defined Variables (UDVs)

The simplest way to create variables in JMeter is using the User Defined Variables element.

You can add this element under your Test Plan or a Thread Group. Here, you define a variable name and its corresponding value.

This is perfect for static values you want to manage centrally, like a base URL or a default username.

Add Element:
  Test Plan > Add > Config Element > User Defined Variables

Configuration:
  Name: BASE_URL
  Value: http://example.com

  Name: USER_COUNT
  Value: 100

Using UDVs in Requests

Once defined, you can reference your User Defined Variables anywhere in your test plan using the syntax ${variableName}.

JMeter will replace this placeholder with the actual value of the variable during test execution.

HTTP Request Sampler:
  Protocol: http
  Server Name or IP: ${BASE_URL}
  Path: /login

HTTP Header Manager:
  Name: X-User-Count
  Value: ${USER_COUNT}

Introducing JMeter Functions

While variables store fixed or manually updated values, JMeter Functions generate dynamic values during runtime.

Functions start with ${__functionName(...)} and can take parameters. They help simulate more realistic user behavior, like generating random numbers or unique IDs.

The __Random Function

The __Random function generates a random integer within a specified range.

Its syntax is ${__Random(min, max, variableName)}:

  • min: The smallest number to generate.
  • max: The largest number to generate.
  • variableName: (Optional) A variable to store the generated value.

This is great for picking random items from a list or simulating varying input.

Example:
  ${__Random(1, 10,)}
  // Generates a random number between 1 and 10

  ${__Random(100, 999, myRandomID)}
  // Generates a random number between 100 and 999
  // and stores it in a variable named 'myRandomID'

Using __Random in a Test

Let's say you want to simulate users accessing random product IDs on an e-commerce site. You can use __Random directly in your HTTP Request path.

This helps distribute the load across different resources, mimicking real user behavior.

HTTP Request Sampler:
  Protocol: http
  Server Name or IP: ${BASE_URL}
  Path: /products/${__Random(1001, 2000,)}

  // This would access paths like:
  // /products/1543
  // /products/1987
  // etc.

The __counter Function

The __counter function generates a sequence of unique numbers. It's useful when you need to provide unique identifiers or sequential data.

Its syntax is ${__counter(true/false, variableName)}:

  • true: Counter is global across all thread groups.
  • false: Counter is local to each thread.
  • variableName: (Optional) A variable to store the generated value.
Example:
  ${__counter(false,)}
  // Generates 1, 2, 3... for each thread independently

  ${__counter(true, globalCount)}
  // Generates 1, 2, 3... globally across all threads
  // and stores it in 'globalCount'

Using __counter for Unique IDs

Imagine you're testing an order creation API where each order needs a unique ID. Using __counter ensures that every request sends a distinct order ID.

This is vital for avoiding data conflicts and accurately simulating transactions.

HTTP Request Sampler:
  Method: POST
  Path: /orders

  Body Data:
  {
    "orderId": "ORDER-${__counter(true,)}",
    "customerName": "Test User"
  }

  // Example Body Data:
  // {"orderId": "ORDER-1", ...}
  // {"orderId": "ORDER-2", ...}

Functions & Variables Quiz

Which of the following statements about JMeter variables and functions are TRUE?

Recap: Functions & Variables

Great job! You've learned how to make your JMeter tests more dynamic and realistic:

  • JMeter Variables provide placeholders for reusable values like base URLs.
  • User Defined Variables (UDVs) are a simple way to define these static variables.
  • JMeter Functions generate values dynamically during test runtime.
  • The __Random function creates random numbers for varied inputs.
  • The __counter function generates unique, sequential numbers.

Mastering these will significantly improve the flexibility and accuracy of your load tests!

常见问题解答

「JMeter 函数与变量」课时是免费的吗?

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

「JMeter 函数与变量」这节课中我会学到什么?

探索 JMeter 的内置函数和用户定义变量,提升脚本的灵活性 你通过在浏览器中直接运行的动手代码来练习 Load Testing & Performance Benchmarking (JMeter & k6),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「JMeter 函数与变量」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 使用 CSV 数据进行参数化
  2. 处理动态值(关联)
  3. JMeter 函数与变量
  4. 使用 JDBC 进行数据库测试
← 返回 Load Testing & Performance Benchmarking (JMeter & k6)