在有关mongodb驱动程序的最新基准测试中,我们注意到python和.Net(核心或框架)之间的性能存在巨大差异。
我认为这可以解释其中的部分差异。
我们获得了以下结果:
????????????????????????????????????????????????????????????
? Metric ? Csharp ? Python ? ratio p/c ?
????????????????????????????????????????????????????????????
? Ratio Duration/Document ? 24.82 ? 0.03 ? 0.001 ?
? Duration (ms) ? 49 638 ? 20 016 ? 0.40 ?
? Count ? 2000 ? 671 972 ? 336 ?
????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
我们看了一下C#中的内存分配,并且注意到在BsonChunck的下载阶段和反序列化之间存在乒乓球。(正常情况下是按批次进行的。)但是下载阶段很长。因此,当mongo使用TCP / IP时,我们查看了不同查询的网络跟踪:
??????????????????????????????????????????????????????????????????????
? Metric ? Csharp ? Python ? ratio p/c ?
??????????????????????????????????????????????????????????????????????
? Packets/sec to db ? 30 ? 160 ? 5.3 ?
? Packets/sec from DB ? 120 - 150 ? 750 - 1050 ? ~6.5 ?
? Packet count to db ? 1560 ? 2870 ? 1.84 ?
? Packet count from db ? 7935 ? 13663 ? 1.7 ?
? Packet average length to db ? 73.6 ? 57.6 ? 0.74 ?
? Packet average length from db ? 1494 ? 1513 ? 1.01 ?
? Max TCP Errors/sec ? 20 ? 170 ? 8.5 ?
??????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
对于配置文件,仅使用以下内容,结果令人震惊:
{
"connectionString": "mongodb://ip.of.the.mongo:27018",
"dbname" : "mydb",
"colname" : "mycollection",
"query" : {},
"projection" :{},
"limit" : 2000,
"batchsize": 10
}
Run Code Online (Sandbox Code Playgroud)
一个对象的延迟令人印象深刻:python为0.03毫秒,而csharp为24.82。
您对此差异有一些见解吗?您知道在C#中达到与Python相同性能的方法吗?先感谢您 :-)
为了进行基准测试,我们使用以下两个代码:
Python(pymongo驱动程序):
????????????????????????????????????????????????????????????
? Metric ? Csharp ? Python ? ratio p/c ?
????????????????????????????????????????????????????????????
? Ratio Duration/Document ? 24.82 ? 0.03 ? 0.001 ?
? Duration (ms) ? 49 638 ? 20 016 ? 0.40 ?
? Count ? 2000 ? 671 972 ? 336 ?
????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
对于.Net(MongoDB.Driver):
??????????????????????????????????????????????????????????????????????
? Metric ? Csharp ? Python ? ratio p/c ?
??????????????????????????????????????????????????????????????????????
? Packets/sec to db ? 30 ? 160 ? 5.3 ?
? Packets/sec from DB ? 120 - 150 ? 750 - 1050 ? ~6.5 ?
? Packet count to db ? 1560 ? 2870 ? 1.84 ?
? Packet count from db ? 7935 ? 13663 ? 1.7 ?
? Packet average length to db ? 73.6 ? 57.6 ? 0.74 ?
? Packet average length from db ? 1494 ? 1513 ? 1.01 ?
? Max TCP Errors/sec ? 20 ? 170 ? 8.5 ?
??????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
老度量:从基准循环中删除连接之前。
????????????????????????????????????????????????????????????
? Metric ? Csharp ? Python ? ratio p/c ?
????????????????????????????????????????????????????????????
? Ratio Duration/Document ? 26.07 ? 0.06 ? 0.002 ?
? Duration (ms) ? 52 145.0 ? 41 981 ? 0.80 ?
? Count ? 2000 ? 671 972 ? 336 ?
????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????
? Metric ? Csharp ? Python ? ratio p/c ?
??????????????????????????????????????????????????????????????????????
? Packets/sec to db ? 30 ? 150 ? 5 ?
? Packets/sec from DB ? 120 - 180 ? 750 - 1050 ? ~6 ?
? Packet count to db ? 1540 ? 2815 ? 1.8 ?
? Packet count from db ? 7946 ? 13700 ? 1.7 ?
? Packet average length to db ? 74 ? 59 ? 0.80 ?
? Packet average length from db ? 1493 ? 1512 ? 1 ?
? Max TCP Errors/sec ? 10 ? 320 ? 32 ?
??????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
pymongo您正在试验的 C# 和 python 驱动程序之间的性能差异是由于 python lib 如何理解空投影的一个非常小的细节造成的{ }。
在 mongodb-shell 和 C# 驱动程序中,{ }投影确实返回整个文档。实际上根本没有投影。它可能被视为默认行为,因为 mongodb-shell 就是这样的。
然而,{ }Python 中的投影 withpymongo只返回_id字段!
文档越多,C# 代码下载文档所需的时间就越长,而 Python 代码只需要非常小的 _ids。
如果你改变从
res = col.find(conf["query"], conf["projection"]) #projection being { }.
for i in res:
print(i) # Only print Id fields.
returned += 1
Run Code Online (Sandbox Code Playgroud)
到
res = col.find(conf["query"])
for i in res:
print(i) # Whole document 'downloaded' and printed.
returned += 1
Run Code Online (Sandbox Code Playgroud)
您会注意到两者都表现良好。