golang HTTP请求路由性能测试与比较插件库go-http-routing-benchmark的使用
Golang HTTP请求路由性能测试与比较插件库go-http-routing-benchmark的使用
简介
go-http-routing-benchmark是一个用于比较Go语言中各种HTTP请求路由器性能的基准测试套件。它通过实现一些真实世界API的路由结构来评估不同路由器的性能表现。
测试的路由器和框架
该基准测试套件测试了以下路由器和框架:
- Beego
- go-json-rest
- Denco
- Gocraft Web
- Goji
- Gorilla Mux
- http.ServeMux
- HttpRouter
- HttpTreeMux
- Kocha-urlrouter
- Martini
- Pat
- Possum
- R2router
- TigerTonic
- Traffic
动机
Go是构建Web应用程序的优秀语言。由于Go的net/http包中的默认请求多路复用器非常简单且功能有限,因此存在大量HTTP请求路由器。许多早期路由器使用相当糟糕的路由算法,并且在内存分配方面非常浪费。本基准测试旨在比较这些路由器的性能表现。
结果
内存消耗
下表显示了加载各自API的路由结构所需的内存:
Router | Static | GitHub | Google+ | Parse |
---|---|---|---|---|
HttpServeMux | 18064 B | - | - | - |
Beego | 79472 B | 497248 B | 26480 B | 38768 B |
HttpRouter | 26232 B | 44344 B | 3144 B | 5792 B |
… (其他路由器) | … | … | … | … |
静态路由
静态基准测试不是真实API的克隆,而是受Go目录结构启发的随机静态路径集合。目的是允许与Go的net/http包的默认路由器http.ServeMux进行比较。
示例基准测试结果:
BenchmarkHttpServeMux_StaticAll 5000 706222 ns/op 96 B/op 6 allocs/op
BenchmarkHttpRouter_StaticAll 100000 15010 ns/op 0 B/op 0 allocs/op
微基准测试
以下基准测试测量一些非常基本操作的成本:
单个参数路由
BenchmarkHttpRouter_Param 20000000 139 ns/op 33 B/op 1 allocs/op
多个参数路由(5个和20个参数)
BenchmarkHttpRouter_Param5 5000000 319 ns/op 162 B/op 1 allocs/op
BenchmarkHttpRouter_Param20 2000000 954 ns/op 646 B/op 1 allocs/op
真实API测试
基准测试还使用了Parse.com、GitHub和Google+等真实API的路由结构进行测试。
结论
- 没有理由使用net/http的默认ServeMux,它功能有限且性能不佳
- 一些框架的广泛功能是以性能为代价的
- HttpRouter包目前在几乎所有基准测试中表现最佳
使用示例
要运行这些基准测试,首先需要安装该包:
go get github.com/julienschmidt/go-http-routing-benchmark
安装完成后,可以运行全套基准测试:
cd $GOPATH/src/github.com/julienschmidt/go-http-routing-bbenchmark
go test -bench=.
如果测试超时,可以增加超时时间:
go test -timeout=2h -bench=.
也可以只测试特定的框架:
go test -bench="Martini|Gin|HttpMux"
完整示例Demo
以下是一个使用go-http-routing-benchmark进行自定义测试的示例代码:
package main
import (
"testing"
"github.com/julienschmidt/go-http-routing-benchmark/static"
)
// 自定义测试函数
func BenchmarkMyRouter(b *testing.B) {
// 初始化路由器和测试请求
router := static.MyRouter()
requests := static.GetRequests()
// 重置计时器
b.ResetTimer()
// 运行基准测试
for i := 0; i < b.N; i++ {
for _, request := range requests {
router.ServeHTTP(nil, request)
}
}
}
// 主测试函数
func TestAll(t *testing.T) {
// 可以在这里添加更多测试用例
t.Run("BenchmarkMyRouter", BenchmarkMyRouter)
}
要运行这个自定义测试,可以使用以下命令:
go test -bench=BenchmarkMyRouter -run=^TestAll$
这个示例展示了如何集成自己的路由器到基准测试套件中,与其他路由器进行公平比较。
更多关于golang HTTP请求路由性能测试与比较插件库go-http-routing-benchmark的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang HTTP请求路由性能测试与比较插件库go-http-routing-benchmark的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Golang HTTP路由性能测试与比较 - go-http-routing-benchmark 使用指南
在Go语言Web开发中,选择高性能的路由器框架对应用性能至关重要。go-http-routing-benchmark是一个专门用于测试和比较各种Go HTTP路由框架性能的工具库。
1. 安装go-http-routing-benchmark
首先安装基准测试工具:
go get github.com/julienschmidt/go-http-routing-benchmark
2. 支持的框架
该工具支持测试多种流行的Go路由框架,包括:
- 标准库
net/http
gorilla/mux
julienschmidt/httprouter
gin-gonic/gin
echo
chi
- 等等
3. 基本使用方法
3.1 运行所有基准测试
cd $GOPATH/src/github.com/julienschmidt/go-http-routing-benchmark
go build
./go-http-routing-benchmark
3.2 测试特定框架
./go-http-routing-benchmark -framework=httprouter,gin
4. 解读测试结果
测试结果通常包含以下指标:
- Requests per second: 每秒处理的请求数
- Time per request: 每个请求的平均处理时间
- Memory usage: 内存使用情况
示例输出:
BenchmarkHttpRouter_GithubAll 20000 98765 ns/op 0 B/op 0 allocs/op
BenchmarkGin_GithubAll 18000 112345 ns/op 32 B/op 1 allocs/op
5. 自定义测试用例
你可以添加自己的路由框架进行测试,以下是示例代码:
package main
import (
"net/http"
"testing"
"github.com/julienschmidt/go-http-routing-benchmark/static"
)
func BenchmarkMyRouter_StaticAll(b *testing.B) {
router := setupMyRouter()
static.BenchmarkRoutes(b, router, static.AllRoutes)
}
func setupMyRouter() http.Handler {
// 这里初始化你的自定义路由器
// 示例使用标准库
mux := http.NewServeMux()
for _, route := range static.AllRoutes {
mux.HandleFunc(route.Path, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
}
return mux
}
6. 实际应用中的性能考量
虽然基准测试提供了重要参考,但实际选择路由框架时还应考虑:
- 功能需求:路由参数、中间件支持等
- 开发体验:API设计是否直观
- 社区支持:文档、生态系统和活跃度
- 内存占用:长期运行的内存表现
7. 常见路由框架性能比较
根据最新测试结果(可能随时间变化):
- httprouter:通常性能最高,但功能较基础
- gin:性能接近httprouter,提供更多功能
- echo:性能优秀,API设计优雅
- chi:性能良好,兼容标准库接口
- gorilla/mux:功能丰富但性能较低
8. 高级用法
8.1 测试特定路由模式
func BenchmarkMyRouter_StaticRoot(b *testing.B) {
router := setupMyRouter()
static.BenchmarkRoutes(b, router, []static.Route{
{Path: "/"},
})
}
8.2 添加自定义测试场景
func BenchmarkMyRouter_Param(b *testing.B) {
router := setupMyRouterWithParams()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, _ := http.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
}
}
9. 结论
go-http-routing-benchmark是评估Go路由框架性能的强大工具。通过合理使用它,开发者可以基于实际性能数据做出更明智的技术选型决策。但记住,性能只是选择框架的一个方面,还需综合考虑其他工程因素。
建议定期运行基准测试,因为各框架会不断优化,性能特征可能随时间变化。