有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

MongoDB Java驱动程序的ChangeStream性能问题

我被要求查看mongodb的最新文件。 我已经使用ChangeStream watch API从集合中获取流文档

我的设置是一个副本集,其中3个节点在同一个系统中运行,端口为2701727018和27019。该设置没有任何身份验证设置

mongodb。配置文件:

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);
            }
            
        }

有没有一种方法可以让change stream API获得更好的性能。 或者有没有其他API可以让我在观看文档时表现更好。或任何其他可以提供更好性能的复制属性


共 (1) 个答案

  1. # 1 楼答案

    我写并运行了一个benchmark

    在一台售价100美元、使用i5-4460S的消费者级SFF台式机上,在内存中存储数据库的情况下,我可以获得每秒写入zram的17k个文档。数据库的CPU有限

    此时,变更流的性能受到插入性能的约束,并且变更流以每秒17k的速度交付变更

    然而,变化流正在爆发,爆发显示出比数据库在这个硬件上持续写入所能做的更高的吞吐量

    基于此,我建议变更流性能超过数据库处理写入的能力