3

我正在尝试使用 vegeta 加载测试一个简单的 go/mongo API,但我只是在POST端点测试期间超时。

Mongo 驱动https://github.com/mongodb/mongo-go-driver

处理程序

// CreateAccount handles creation of a new Account
func CreateAccount(w http.ResponseWriter, r *http.Request) {
    var account schemas.Account
    json.NewDecoder(r.Body).Decode(&account)

    account.Name = RandStringBytes(10)
    if err := account.Create(); err != nil {
        log.Fatal(err)
    } else {

        w.Header().Add("Content-Type", "application/json")
        w.WriteHeader(http.StatusCreated)

        json.NewEncoder(w).Encode(&account)

    }

}

帐户.创建

// Create Creates an Account
func (acc *Account) Create() error {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    acc.ID = primitive.NewObjectID()

    if err := acc.hashPassword(); err != nil {
        log.Fatal(err)
    }

    _, err := accountCollection.InsertOne(ctx, acc)
    if err != nil {
        return err
    }

    return nil
}

InitDb(在 main.go 中调用)

func InitDb(databaseURI string) {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    var err error
    Db, err = mongo.Connect(ctx, options.Client().ApplyURI(databaseURI))
    accountCollection = Db.Database(databaseName).Collection(accountCollectionName)

    if err != nil {
        log.Fatal(err)
    }

    if err = Db.Ping(ctx, readpref.Primary()); err != nil {
        log.Fatal(err)
    }

}

测试GET端点按预期工作。唯一的问题是POST端点

做一个简单jq -ncM '{method: "POST", url: "http://localhost:8080/accounts", body: "Punch!" | @base64, header: {"Content-Type": ["applicati/json"]}}' | vegeta attack -format=json -rate=100 | vegeta encode的创建随机空文档将产生以下日志:

http服务器:

(master) $ go run main.go 
Server running :8080
2019/04/23 22:36:46 Error
2019/04/23 22:36:46 context deadline exceeded
exit status 1

mongo(来自官方 docker-hub 的“香草”容器)

sudo docker run --rm -p 27017:27017 mongo
2019-04-24T01:35:43.128+0000 I NETWORK  [listener] connection accepted from 172.17.0.1:34002 #1 (1 connection now open)
2019-04-24T01:36:46.193+0000 I NETWORK  [conn1] end connection 172.17.0.1:34002 (0 connections now open)

素食

$ jq -ncM '{method: "POST", url: "http://localhost:8080/accounts", body: "Punch!" | @base64, header: {"Content-Type": ["applicati/json"]}}' |   vegeta attack -format=json -rate=50 | vegeta encode
{"attack":"","seq":1020,"code":0,"timestamp":"2019-04-24T01:39:27.355666931Z","latency":0,"bytes_out":0,"bytes_in":0,"error":"Post http://localhost:8080/accounts: dial tcp: lookup localhost: device or resource busy","body":null}
{"attack":"","seq":1021,"code":0,"timestamp":"2019-04-24T01:39:27.375672741Z","latency":0,"bytes_out":0,"bytes_in":0,"error":"Post http://localhost:8080/accounts: dial tcp: lookup localhost: device or resource busy","body":null}
{"attack":"","seq":1022,"code":0,"timestamp":"2019-04-24T01:39:27.395670955Z","latency":0,"bytes_out":0,"bytes_in":0,"error":"Post http://localhost:8080/accounts: dial tcp: lookup localhost: device or resource busy","body":null}

如果我将-rate它降到 5 它可以工作,但它仍然在请求之间挂起 1 或 2 秒。从http服务器错误看来,go正在失去与mongo的连接关于如何进一步调试它的任何提示?

4

0 回答 0