根据wrk测试结果性能优化和性能对比

1,108 阅读2分钟

这是我参与更文挑战的第6天,活动详情查看:更文挑战
本文已参与周末学习计划,点击链接查看详情:juejin.cn/post/696572…

如果❤️我的文章有帮助,欢迎点赞、关注。这是对我继续技术创作最大的鼓励。

本文为:iris 基于 ab 或 wrk 的性能测试 以及 iris 实践项目 后续性能优化记录

对比基准: Requests/sec: 779.06 - qps -每秒响应请求数

对比操作性能前后差异

  • 对比 1:模板是否支持每次更新检查
  • 对比 2:开启 xorm 数据缓存
  • 对比 3:不用模板,不查数据
  • 对比 4:关闭 DEBUG 日志

对比 1:模板是否支持每次更新检查

main3.go

tmpl := iris.HTML("./web/views", ".html").
		Layout("shared/layout.html").
		Reload(true)  // 每次都会重新加载模板

Reload 改成 false,不再每次请求都加载新模板 里面涉及磁盘的操作

$ wrk -c10 -t10 -d5 http://localhost:8080/index
Running 5s test @ http://localhost:8080/index
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.83ms    2.46ms  38.28ms   91.24%
    Req/Sec   398.03     66.54   570.00     68.20%
  19836 requests in 5.01s, 94.91MB read
Requests/sec:   3960.96       +3200     - qps -每秒响应请求数
Transfer/sec:     18.95MB

对比 2:开启 xorm 数据缓存

datasource/dbhelper.go

"xorm.io/xorm/caches"

// 性能优化时才考虑进去,加上本机sql缓存
cacher := caches.NewLRUCacher(caches.NewMemoryStore(), 1000)
masterEngine.SetDefaultCacher(cacher)

cachersql缓存数据打开

$ wrk -c10 -t10 -d5 http://localhost:8080/index
Running 5s test @ http://localhost:8080/index
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    11.20ms  826.21us  18.13ms   79.28%
    Req/Sec    89.30      4.82   101.00     77.80%
  4470 requests in 5.02s, 21.38MB read
Requests/sec:    890.51       +120     - qps -每秒响应请求数
Transfer/sec:      4.26MB

但如果 模板缓存 和 数据缓存 都打开,qps 就会飙升到惊人的 9172.56

$ wrk -c10 -t10 -d5 http://localhost:8080/index
Running 5s test @ http://localhost:8080/index
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.15ms  816.68us   9.77ms   78.78%
    Req/Sec     0.94k   171.16     4.39k    97.60%
  46783 requests in 5.10s, 223.83MB read
Requests/sec:   9172.56
Transfer/sec:     43.89MB

对比 3:不用模板,不查数据

web/controllers/index.go

func (c *IndexController) Get() mvc.Result  {
	return mvc.Response{
		Text: "ok \n",
	}

	//datalist := c.Service.GetAll()
	//return mvc.View{
	//	Name: "index.html",
	//	Data: iris.Map{
	//		"Title": "球星库",
	//		"Datalist": datalist,
	//	},
	//}
}

直接返回,不查数据

$ wrk -c10 -t10 -d5 http://localhost:8080/index
Running 5s test @ http://localhost:8080/index
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   333.19us  630.26us  13.84ms   94.94%
    Req/Sec     4.41k   411.91     6.18k    72.44%
  222678 requests in 5.10s, 25.48MB read
Requests/sec:  43660.91
Transfer/sec:      5.00MB

对比 4:关闭 DEBUG 日志

注释 web/main3.go

app.Logger().SetLevel("debug")

注释掉,不在 命令行打日志,提升130多

$ wrk -c10 -t10 -d5 http://localhost:8080/index
Running 5s test @ http://localhost:8080/index
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   333.11us  648.99us  13.47ms   94.98%
    Req/Sec     4.52k   375.94     5.55k    69.80%
  229472 requests in 5.10s, 26.26MB read
Requests/sec:  44967.99
Transfer/sec:      5.15MB

总结

在普通开发电脑,上 Golang 开启数据数据缓存、模板缓存 后能能逐步增加,甚至翻倍。效果喜人