英文:
Performance implication of (mongo.Client).Ping
问题
最近在测试对MongoDB无服务器集群进行写入或读取性能时,我注意到了一些奇怪的行为。
我不认为这与无服务器选项有关,但是作为背景,我正在使用AWS(爱尔兰地区)的一个新的无服务器集群,并从我的本地机器与之交互。
以下是一些演示代码:
package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Item struct {
Value string `bson:"value"`
}
func main() {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("add-url"))
if err != nil {
log.Fatalln(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatalln(err)
}
collection := client.Database("test").Collection("test")
item := Item{
Value: "foo",
}
start := time.Now()
_, err = collection.InsertOne(ctx, item)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Item added in", time.Since(start))
}
这段代码运行良好,一直保持在大约60毫秒左右的结果。
然而,如果我删除err = client.Ping(ctx, nil)
这部分,时间会一直跳到超过500毫秒。为什么会这样?
从文档中我没有得到太多信息。
英文:
I’ve recently noticed some odd behavior while testing the performance of writing or reading to a MongoDB serveless cluster.
I don’t think is related to the serverless option, but as a context I’m using a new serverless cluster in AWS (Ireland region) which I interact with from my local machine.
Some demo code:
package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Item struct {
Value string `bson:"value"`
}
func main() {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("add-url"))
if err != nil {
log.Fatalln(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatalln(err)
}
collection := client.Database("test").Collection("test")
item := Item{
Value: "foo",
}
start := time.Now()
_, err = collection.InsertOne(ctx, item)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Item added in", time.Since(start))
}
This works fine, producing results of about 60ms consistently.
However, if I remove the err = client.Ping(ctx, nil)
part the times jump to over 500ms consistently. Why does this happen?
Not much info I get from the docs.
答案1
得分: 0
从NewClient方法的文档中可以得知:
NewClient函数不执行任何I/O操作,如果给定的选项无效,则返回错误。Client.Connect方法启动后台goroutine来监视部署的状态,并且在主goroutine中不执行任何I/O操作,以防止主goroutine阻塞。因此,如果部署停止运行,它不会报错。
可以使用Client.Ping方法来验证是否成功连接到部署,并且Client已正确配置。
在删除Ping
指令后,由于创建与服务器的连接的开销,InsertOne
操作需要更长的时间。你可以尝试测量Ping
命令的执行时间,大约应该在500毫秒左右。
英文:
From the documentation of NewClient method:
The NewClient function does not do any I/O and returns an error if the given options are invalid. The Client.Connect method starts background goroutines to monitor the state of the deployment and does not do any I/O in the main goroutine to prevent the main goroutine from blocking. Therefore, it will not error if the deployment is down.
The Client.Ping method can be used to verify that the deployment is successfully connected and the Client was correctly configured.
After you remove Ping
instruction, InsertOne
takes longer due to the overhead of creating the connection to the server. You may try to measure how long Ping
command takes and it should be around 500ms.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论