duancai7568 2019-08-20 18:23
浏览 133
已采纳

开始-在用于负载测试的高性能http客户端中,如何阻止/忽略所有cookie?

I'm creating tools for my company to load test our system. I currently have tools written in Python but I'm exploring options using Go, hoping for efficiency and performance gains as we need to generate millions of users simultaneously (many thousands per box with many boxes) and every little bit counts. My users primarily make http calls against our system and I need a very lightweight and efficient http client.

For our Python tools, a previous employee had some functions manually dealing with and reusing Sockets and totally ignoring cookies. While I'm totally new to Go, I've compared multiple Go networking implementations so far and I've been happy with CPU and network usage so I don't think I need to go so far as to manually manage sockets yet, but RAM usage has been multiple times higher than in our Python solution. I have a suspicion that's due to these Go implementations automatically maintaining and managing cookies as other higher level Python libraries did the same thing. I'm looking for ways to have my http client disable/block/ignore all cookies to either solve my problem or rule out cookies as my memory bloat culprit.

I've used a simple net/http Get() call, fasthttp to create a client that reuses varying numbers of connections per hosts (1000+), and creating a net/http.Client with and without a custom Transport. I've used pprof to memory profile each. Without overriding cookies, my http.Client flow seems to have the least memory bloat and fasthttp has the most. I've tried to go through all their docs and searched SO and the web and can't find anything that explicitly ignores or blocks cookies. Closest I found was fasthttp's DelAllCookies() for both requests and responses, but calling those had no discernible effect on memory usage. I've looked into net/http/cookiejar to set a custom jar and policy for net/http.Client but that looks more like a way to store and use cookie data, which I don't want to do, and I don't see a way to set a policy to ignore cookies altogether.

// fasthttp implementation
var httpClient *fasthttp.Client

func init() {
    httpClient = &fasthttp.Client{
        MaxConnsPerHost: 1000,
    }
}

func fastHTTPClient(method string, uri string) (string, time.Duration) {

    req := fasthttp.AcquireRequest()
    req.Header.SetMethod(method)
    req.SetRequestURI(uri)

    resp := fasthttp.AcquireResponse()

    startTime := time.Now()
    err := httpClient.DoTimeout(req, resp, targetDuration)
    elapsed := time.Since(startTime)

    if err != nil {
        log.Panicln(err)

        body := string(resp.Body())
        req.Header.DelAllCookies()
        resp.Header.DelAllCookies()
        fasthttp.ReleaseRequest(req)
        fasthttp.ReleaseResponse(resp)
        return body, elapsed
    }

    body := string(resp.Body())
    req.Header.DelAllCookies()
    resp.Header.DelAllCookies()
    fasthttp.ReleaseRequest(req)
    fasthttp.ReleaseResponse(resp)
    return body, elapsed
}

// net/http implementation
var tr = &http.Transport{}
var client = &http.Client{Transport: tr}

func netGet(method string, uri string) (string, time.Duration) {

    startTime := time.Now()
    resp, err := client.Get(uri)
    elapsed := time.Since(startTime)

    if err != nil {
        log.Panic(err)

        body, err2 := ioutil.ReadAll(resp.Body)
        resp.Body.Close()

        if err2 != nil {
            log.Panic(err)
        }

        return string(body), elapsed
    }

    body, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()

    if err != nil {
        log.Panicln(err)
    }

    return string(body), elapsed
}

Again, these work functionally but when I run thousands of these concurrently using boomer I end up using multiple GB of RAM vs 1-1.5 GB with our Python Sockets implementation. How can I block/ignore cookies or otherwise be sure cookies aren't what's eating up all my memory?

  • 写回答

2条回答 默认 最新

  • duanlan2003 2019-08-20 20:06
    关注

    Fasthttp will always load the full response into memory. So if you're having large response bodies and want to limit memory usage it's probably not the best solution for you.

    Fasthttp also trades memory usage for CPU usage by keeping buffers in memory and reusing it instead of spending CPU cycles having these buffers collected by the garbage collector.

    Using multiple GBs of memory seems a lot. Are you looking at the RSS or maybe the virtual memory in this case?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算