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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀