Golang mongo-go-driver挂起/超时收集。在负载测试期间插入InsertOne

时间:2019-04-24 01:48:24

标签: go load-testing mongo-go

我正在尝试使用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端点是否按预期工作,唯一的问题是创建/发布端点

对创建随机的空文档进行简单的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会产生以下日志:

httpserver:

(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的连接 有关如何进一步调试它的任何提示?

0 个答案:

没有答案