You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter中创建基于用户选择的可复用API请求

Reusable Solution for Dynamic API Request Body Generation

Great question! Here's a clean, maintainable approach to generate your API request bodies dynamically based on user-selected metrics. This solution is easy to extend and works with any variable name you use for user input.

Core Idea

First, we'll map each user-friendly metric name (like network, cpu) to its corresponding entry in the metrics object. Then, we'll build the full request body by combining fixed base properties with only the metrics the user selected.

Step 1: Define Mappings & Base Configuration

Set up a reusable mapping for metrics and the static base part of your request (since most fields don't change between scenarios):

// Mapping of user-friendly metric names to their API metric entries
const METRIC_MAPPINGS = {
  network: { "com.cixsoft.agent.metric.NetworkMetric": "NetworkMetric.json" },
  cpu: { "com.cixsoft.agent.metric.CpukMetric": "CpukMetric.json" },
  process: { "com.cixsoft.agent.metric.ProcessInfoMetric": "ProcessInfoMetric.json" }
};

// Static base properties for all requests
const BASE_REQUEST_BODY = {
  os: "Linux",
  timeOffset: "0",
  submanagerlogs: "false",
  preDefinedAgents: {}
};

Step 2: Reusable Function to Generate Request Body

Create a function that takes the user's input (a comma-separated string or array of metrics) and returns the complete API request body:

function generateApiRequestBody(userInput) {
  // Convert input to an array (handle both comma-separated strings and arrays)
  const selectedMetrics = Array.isArray(userInput) 
    ? userInput 
    : userInput.split(",").map(metric => metric.trim().toLowerCase());

  // Build the metrics object by combining entries from our mapping
  const metrics = selectedMetrics.reduce((acc, metric) => {
    if (METRIC_MAPPINGS[metric]) {
      return { ...acc, ...METRIC_MAPPINGS[metric] };
    }
    return acc;
  }, {});

  // Combine base properties with the dynamic metrics
  return { ...BASE_REQUEST_BODY, metrics };
}

Step 3: Test the Function with Your Scenarios

You can use this function with any of your input scenarios:

Scenario 1: Input network,cpu,process

const input1 = "network,cpu,process";
console.log(generateApiRequestBody(input1));
// Returns exactly the request body you specified for Scenario 1

Scenario 2: Input cpu,process

const input2 = "cpu,process";
console.log(generateApiRequestBody(input2));
// Returns the Scenario 2 request body

Scenario 3: Input network,cpu

const input3 = "network,cpu";
console.log(generateApiRequestBody(input3));
// Returns the Scenario 3 request body

Scenario 4: Input process

const input4 = "process";
console.log(generateApiRequestBody(input4));
// Returns the Scenario 4 request body

Why This Works (and Is Reusable)

  • Easy to extend: If you add a new metric (like memory), just add a new entry to METRIC_MAPPINGS—no need to modify the function logic.
  • Flexible input: Handles both comma-separated strings (like user input from a text field) and arrays of metric names.
  • Clean separation: Static request properties are kept in one place, making it simple to update values like os or timeOffset later.
  • Works with any variable name: Replace userInput with whatever variable name your system uses (e.g., selectedMetrics, userSelectedMetrics)—the function only cares about the input value.

内容的提问来源于stack exchange,提问作者Ralph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:29:11