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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!