【发布时间】:2021-03-24 19:17:46
【问题描述】:
我们有一个在 IIS 上运行的应用程序,使用 mongodb c# 驱动程序,(2.11.5.0 版本),当它打开与副本集的新连接时,大约需要 500 毫秒(包括数据库上市操作),
在相同的环境和相同的机器上,我用 NodeJS 应用程序尝试了相同的情况,它建立了连接并在 62 毫秒 内列出了数据库。我也尝试过使用 python 代码,它需要 12 毫秒,包括从集合中获取记录。 (每次我控制这些持续时间时,我都会关闭所有与 mongodb 的连接并首先停止这些应用程序)
在我们的环境中,第一个连接初始化缓慢是个问题,因为负载均衡器后面有服务器,如果没有足够的请求到达所有服务器并保持连接打开,即使我将空闲超时设置为更长的时间(如 20 分钟) ,请求可以被转发到空闲的服务器,500 毫秒的额外持续时间是事务的成本。在我们的例子中,事务响应时间应该小于 250 毫秒。
NodeJS 代码:
const {MongoClient} = require('mongodb');
async function listDatabases(client){
databasesList = await client.db().admin().listDatabases();
};
async function main(){
const uri = "mongodb://dbUserName:dbPassword@SERVER/DBName?retryWrites=true&w=majority&ssl=false&replicaSet=replicaName&authSource=admin";
console.time("listDatabaseNamesDuration");
const client = new MongoClient(uri,{ useNewUrlParser: true, useUnifiedTopology: true});
try {
await client.connect();
await listDatabases(client);
console.timeEnd("listDatabaseNamesDuration");
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main();
C#代码:
var credential = MongoCredential.CreateCredential(databaseName: "admin",
username: "dbUserName",
password: "dbPassword");
IList<MongoServerAddress> serverList = new List<MongoServerAddress>();
serverList.Add(new MongoServerAddress("serverName", portNumber));
MongoClientSettings settings = new MongoClientSettings
{
Servers = serverList,
ConnectionMode = ConnectionMode.ReplicaSet,
ReplicaSetName = "replicasetName",
Credential = credential,
UseTls = false,
};
Stopwatch sw = Stopwatch.StartNew();
MongoClient _client = new MongoClient(settings);
_client.ListDatabaseNames();
sw.Stop();
output.ListDatabaseNamesDuration = sw.ElapsedMilliseconds;
【问题讨论】:
标签: c# mongodb performance connection