首页 > 解决方案 > Freecache 性能与 sendFile

问题描述

我正在尝试制作一个小型 http 服务器,用于根据那里的 ID 提供图像。我将 fasthttp 用于 tcp 库,将 bigcache 用于在内存中缓存图像。但是从我的测试来看,从 bigcache 获取图像似乎比使用 fasthttp 的 sendFile() 慢,我不知道为什么会这样。

当前用于发送缓存图像的代码是

entry, err := cache.Get(v.(string))
if err != nil {
    log.Print("Image not cached")
    errMsg := (err).Error()
    if strings.Contains(errMsg, "not found") {
        log.Print(err)
        f, _ := os.Open("./test.jpg")
        inBuf, _ := ioutil.ReadAll(f)
        cache.Set(v.(string), inBuf)
        ctx.SendFile("./test.jpg")
        return
    }
    log.Print(err)
    return
}
mime := mimetype.Detect(entry)
ctx.SetContentType(mime.String())
ctx.Write(entry)

当前用于发送非缓存图像的代码是

ctx.SendFile("./test.jpg")

大缓存配置

config := bigcache.Config{
    Shards:             1024,
    LifeWindow:         100000 * 100000 * 60,
    CleanWindow:        5 * time.Minute,
    MaxEntriesInWindow: 1000 * 10 * 60,
    MaxEntrySize:       500,
    Verbose:            true,
    HardMaxCacheSize:   8192,
    OnRemove:           nil,
    OnRemoveWithReason: nil,
}

这是端点上的一些负载测试

NOCACHE - FASTHTTP

Requests      [total, rate, throughput]         3000, 100.03, 100.03
Duration      [total, attack, wait]             29.992s, 29.99s, 1.644ms
Latencies     [min, mean, 50, 90, 95, 99, max]  979.7µs, 3.15ms, 1.552ms, 1.946ms, 2.216ms,    48.766ms, 218.567ms
Bytes In      [total, mean]                     3714138000, 1238046.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:3000
Error Set:

缓存-FASTHTTP

Requests      [total, rate, throughput]         3000, 100.03, 100.02
Duration      [total, attack, wait]             29.993s, 29.99s, 2.471ms
Latencies     [min, mean, 50, 90, 95, 99, max]  1.308ms, 2.768ms, 2.145ms, 2.707ms, 3.003ms, 3.943ms, 176.941ms
Bytes In      [total, mean]                     3714138000, 1238046.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:3000
Error Set:

以下是一些获取请求的时间

INFO[0174] GET /nocache/test.jpg - 200 - 794.846µs
INFO[0175] GET /nocache/test.jpg - 200 - 54.408µs
INFO[0183] GET /nocache/test.jpg - 200 - 60.724µs
INFO[0191] GET /cache/1afQJqFd.jpeg - 200 - 677.857µs
INFO[0195] GET /cache/1afQJqFd.jpeg - 200 - 620.761µs
INFO[0195] GET /cache/1afQJqFd.jpeg - 200 - 642.827µs

我不确定为什么从 bigcache 获取图像需要比从文件系统读取更长的时间,除非我误解了如何使用 bigcache。任何帮助将不胜感激

标签: gocaching

解决方案


推荐阅读