[英]MongoDB Java Driver's ChangeStream performance issue
我需要觀看 mongodb 的最新文檔。 我使用 ChangeStream 手表 API 從集合中獲取 stream 文檔。
我擁有的設置是一個副本集,其中 3 個節點在同一系統中運行,端口為 27017、27018 和 27019。該設置沒有任何身份驗證設置。
mongodb.conf 文件:
systemLog:
destination: file
logAppend: true
path: /mongodb/logs/mongodb.log
storage:
dbPath: /mongodb/data/d1
journal:
enabled: true
engine: "wiredTiger"
wiredTiger:
engineConfig:
cacheSizeGB: 4
net:
port: 27017
bindIp: localhost
我已經對其中包含 72663 個文檔的文件執行了批量插入。 我從下面的程序中得到的每秒處理的記錄只有 8073 條。
我必須觀看的 Java 代碼是。
List<ServerAddress> serverAddress = Arrays.asList(new ServerAddress("localhost", 27019),new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27017));
MongoClientSettings settings = MongoClientSettings.builder()
.applyToClusterSettings(builder -> builder.hosts(serverAddress)).build();
MongoClient client = MongoClients.create(settings);
int count = 0;
Instant start = null;
MongoChangeStreamCursor<ChangeStreamDocument<Document>> dep = client.getDatabase("MyDB").getCollection("TestCollection").watch().cursor();
while (true) {
while (dep.hasNext()) {
if (count == 1) {
start = Instant.now();
}
count++;
ChangeStreamDocument<Document> next = dep.next();
if (count == 72663) {
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
long seconds = timeElapsed.getSeconds();
long rec = count / seconds;
System.out.println("records processed per second " + rec);
}
}
有沒有辦法從更改 stream API 中獲得更好的性能。 或者有沒有其他的 API 可以讓我在觀看文檔時有更好的表現。 或任何其他可以提供更好性能的復制屬性。
我編寫並運行了一個基准測試。
在使用 i5-4460S 的 100 美元消費級 SFF 桌面上,數據庫位於 memory 中,我可以獲得每秒寫入 zram 的 17k 文檔。 數據庫受 CPU 限制。
此時,更改 stream 性能受插入性能的約束,更改 stream 提供了 17k 更改/秒。
然而,stream 的變化是突發的,並且突發顯示的吞吐量比數據庫在此硬件上通過持續寫入所能完成的吞吐量更高。
基於此,我建議更改 stream 性能超過數據庫處理寫入的能力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.