【发布时间】:2020-04-03 03:34:04
【问题描述】:
我在 Mysql 中使用 golang。我正在使用apache benchmark tool 测试服务器上的负载。我收到太多连接错误。我读了this post 。所以,我在我的代码中添加了SetMaxOpenConns(100)。我仍然收到太多连接错误。
我正在做以下查询
ab -n 1000 -k -c 20 -p post.txt -T application/x-www-form-urlencoded http://localhost:8084/abcd
注意: post.txt 文件包含 35 个 id 的数组(整数类型)。这是我的主要功能:
db, err := models.NewDB("root:@/rules")
if err != nil {
panic(err)
}
db.SetMaxOpenConns(100)
http.Handle("/abcd", getReq(db))
log.Fatal(http.ListenAndServe(":8084", nil))
我在 goroutine 中查询这个函数的所有 id。
func getRuleforProduct(db *sql.DB, id int) map[int]string {
m := make(map[int]string)
var res string
err := db.QueryRow("select rules from table where product_id = ?", id).Scan(&res)
checkError(err)
m[id] = res
return m
}
如何解决这个问题,即使每秒请求数较少。我希望代码至少适用于 20 个并发请求。
【问题讨论】:
-
netstat -antp 长什么样子?