2

我正在尝试使用 beego 制作一个简单的 api 应用程序。在压力测试期间,出现了一个意想不到的问题。在 ~16400 个请求之前,一切都以惊人的速度执行。在 16400 次查询几乎全部停止后,每秒运行 1-2 个请求。我有一种感觉,beego无法分配到数据库的连接。我尝试更改 maxIdle、maxConn 参数但没有效果。

升级版。其他数据库也有同样的问题

主控制器:

package controllers

import (
    models "github.com/Hepri/taxi/models"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
)

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    o := orm.NewOrm()
    app := models.ApiApp{}
    err := o.Read(&app)

    if err == orm.ErrMissPK {
        // do nothing
    }
    c.ServeJson()
}

模型:

package models

const (
    CompanyAccessTypeAll      = 1
    CompanyAccessTypeSpecific = 2
)

type ApiApp struct {
    Id                int    `orm:"auto"`
    Token             string `orm:"size(100)"`
}

func (a *ApiApp) TableName() string {
    return "api_apps"
}

main.go:

package main

import (
    models "github.com/Hepri/taxi/models"
    _ "github.com/Hepri/taxi/routers"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    _ "github.com/lib/pq"
)

func main() {
    orm.RegisterDriver("postgres", orm.DR_Postgres)
    orm.RegisterDataBase("default", "postgres", "user=test password=123456 dbname=test sslmode=disable")
    orm.RegisterModel(new(models.ApiApp))
    beego.EnableAdmin = true
    orm.RunCommand()
    beego.Run()
}

在达到〜16400之前:

Benchmarking localhost (be patient)
^C

Server Software:        beegoServer:1.4.2
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        4 bytes

Concurrency Level:      10
Time taken for tests:   3.844 seconds
Complete requests:      16396
Failed requests:        0
Write errors:           0
Total transferred:      2492192 bytes
HTML transferred:       65584 bytes
Requests per second:    4264.91 [#/sec] (mean)
Time per request:       2.345 [ms] (mean)
Time per request:       0.234 [ms] (mean, across all concurrent requests)
Transfer rate:          633.07 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   2.2      0     275
Processing:     0    2  10.9      1     370
Waiting:        0    1   8.6      1     370
Total:          0    2  11.1      2     370

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      2
  95%      3
  98%      3
  99%      4
 100%    370 (longest request)

达到〜16400后:

Benchmarking localhost (be patient)
^C

Server Software:        beegoServer:1.4.2
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        4 bytes

Concurrency Level:      10
Time taken for tests:   15.534 seconds
Complete requests:      16392
Failed requests:        0
Write errors:           0
Total transferred:      2491584 bytes
HTML transferred:       65568 bytes
Requests per second:    1055.22 [#/sec] (mean)
Time per request:       9.477 [ms] (mean)
Time per request:       0.948 [ms] (mean, across all concurrent requests)
Transfer rate:          156.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0      11
Processing:     0    2  16.7      1     614
Waiting:        0    1  15.7      1     614
Total:          0    2  16.7      1     614

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      2
  80%      2
  90%      2
  95%      2
  98%      3
  99%      3
 100%    614 (longest request)

即使在 30 秒后仍是相同的图像

Benchmarking localhost (be patient)
^C

Server Software:        beegoServer:1.4.2
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        4 bytes

Concurrency Level:      10
Time taken for tests:   25.585 seconds
Complete requests:      16391
Failed requests:        0
Write errors:           0
Total transferred:      2491432 bytes
HTML transferred:       65564 bytes
Requests per second:    640.65 [#/sec] (mean)
Time per request:       15.609 [ms] (mean)
Time per request:       1.561 [ms] (mean, across all concurrent requests)
Transfer rate:          95.10 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  10.1      0     617
Processing:     0    2  16.2      1     598
Waiting:        0    1  11.1      1     597
Total:          0    2  19.1      1     618

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      2
  75%      2
  80%      2
  90%      2
  95%      2
  98%      3
  99%      3
 100%    618 (longest request)
4

0 回答 0