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

如何使用登录信息对服务器指定端口进行负载测试?

Hey there! Let's figure out how to get your authenticated load testing sorted out. You mentioned Apache Bench (ab) didn't work for you when trying to hit a specific port with login credentials—let's start with fixing that, then cover some alternative tools that might make this easier.

Making Apache Bench (ab) Work With Authenticated Requests

Most authenticated flows rely on either cookies or bearer tokens, and ab can handle both with a bit of setup:

  1. Grab your authentication cookie/token first
    Use curl to simulate a login request and capture the session cookie (or token) you need. For example, if your login is a POST form:

    curl -c cookies.txt -X POST -d "username=your_username&password=your_password" http://your-server:your-port/login
    

    The -c flag saves the response cookies to cookies.txt. If your API uses a bearer token, extract it from the login response instead (e.g., using jq to parse JSON output).

  2. Run ab with authentication

    • For cookies: Extract the relevant cookie from the file and pass it to ab using the -C flag:
      ab -n 1000 -c 10 -C "$(grep -v '#' cookies.txt | awk '{print $6"="$7}')" http://your-server:your-port/target-endpoint
      
      Here, -n is total requests, -c is concurrent requests, and -C sets the cookie header.
    • For bearer tokens: Use the -H flag to add an Authorization header:
      ab -n 1000 -c 10 -H "Authorization: Bearer your_auth_token" http://your-server:your-port/target-endpoint
      
Alternative Tools for Authenticated Load Testing

If ab feels clunky for authenticated flows, these tools are built to handle more complex scenarios with less friction:

  • wrk (lightweight, scriptable)
    wrk uses Lua scripts to handle dynamic logic like login. Create a script (e.g., login.lua):

    local session_cookie = nil
    
    request = function()
        -- First, log in if we don't have a cookie yet
        if not session_cookie then
            local login_resp = wrk.post("/login", nil, "username=your_user&password=your_pass")
            session_cookie = login_resp.headers["Set-Cookie"]
        end
        -- Send the test request with the cookie
        return wrk.format("GET", "/target-endpoint", {Cookie = session_cookie})
    end
    

    Run it with:

    wrk -t4 -c100 -d30s -s login.lua http://your-server:your-port
    

    -t = number of threads, -c = concurrent connections, -d = test duration.

  • k6 (JS-based, user-friendly)
    k6 uses JavaScript for test scripts, making it easy to write and maintain authenticated flows. Create a test.js file:

    import http from 'k6/http';
    import { check, sleep } from 'k6';
    
    export const options = {
        vus: 10, // Number of concurrent virtual users
        duration: '30s', // How long to run the test
    };
    
    export default function () {
        // Step 1: Log in to get session
        const loginRes = http.post(`http://your-server:your-port/login`, {
            username: 'your_user',
            password: 'your_pass',
        });
        check(loginRes, { 'Login succeeded': (r) => r.status === 200 });
    
        // Step 2: Use the session cookie for the test request
        const testRes = http.get(`http://your-server:your-port/target-endpoint`, {
            cookies: loginRes.cookies,
        });
        check(testRes, { 'Test request succeeded': (r) => r.status === 200 });
    
        sleep(1); // Add a small delay between requests per user
    }
    

    Run with:

    k6 run test.js
    

    k6 also generates detailed, easy-to-read reports right in your terminal.

  • Gatling (enterprise-grade, scalable)
    If you need to run large-scale tests with advanced reporting, Gatling is a great choice. It uses Scala (or a DSL) to define test scenarios, supports login flows out of the box, and produces interactive HTML reports. The learning curve is steeper, but it's perfect for complex performance testing needs.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:59:51