doutu1939 2019-04-24 01:48
浏览 109

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

I'm trying to loadtest a simple go/mongo API with vegeta but I'm getting only timeouts during the test of the POST endpoint.

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

Handler

// 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)

    }

}

Account.Create

// 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 (Called in 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)
    }

}

Testing GET endpoints work as expected the only problem is with create/post endpoint

Doing a simple 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 for the creation of random empty documents will produce the following logs:

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 ("vanilla" container from official 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)

vegeta

$ 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}

If I drop the -rateto 5 it works but it still hangs for 1 or 2 seconds between requests. From the http server error it seems that go is loosing connection with mongo Any tips on how to debug it further ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)