go - 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。任何帮助将不胜感激
解决方案
推荐阅读
- python - 如何用更 Pythonic 的东西用日期范围替换这个 SQL 查询
- python - Stack two LSTM layers in Keras dimension mismatch
- go - One channel with one receiver and unknown number of goroutines senders causing deadlock
- python - 如何在 tensorflow 中实现负二项式可能的 hood 函数并将其用作损失函数来训练 RNN?
- javascript - 如何从循环中执行总和?
- java - How to connect to dynamodb from lambda
- firebase-cloud-messaging - 如果用户安装了我的 PWA,我是否需要单独请求权限才能发送推送通知?
- ide - 用于 PHP 扩展开发的 CLion?
- sql-server - Merge rows in single SQL table
- ruby-on-rails - Delete a user together with his posts