zxCoder
V2EX  ›  问与答

关于数据库查询,涉及到"所花费时间"的测试,怎么保证稳定性呢?

  •  
  •   zxCoder · Mar 26, 2021 · 905 views
    This topic created in 1875 days ago, the information mentioned may be changed or developed.

    比如我写了代码

    var sw = new Stopwatch();
    sw.Start();
    foreach (var from in _froms) {
       var filter = Builders<BsonDocument>.Filter.Eq("from", from);
       var t = await _dataClient.GetDatabase("db")
           			.GetCollection<BsonDocument>("col")
                	.FindAsync(filter);
    }
    sw.Stop();
    

    测出来时间基本上是从 200ms 到 300ms 之间,但是有时候也出现过 700+ms 的情况。。。但是电脑并没有说明显的卡顿或者说 cpu 使用率过高的

    这种情况下要怎么保证测试的稳定呢

    大概会跟什么因素有比较大的关系呢?我程序和数据库都是本地运行,数据库是 docker 启动的容器。跟网络会有关系吗?或者是跟电脑的内存,cpu 使用率关系大吗?

    akira
        1
    akira  
       Mar 26, 2021
    跑 10 万次
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1563 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:44 · PVG 00:44 · LAX 09:44 · JFK 12:44
    ♥ Do have faith in what you're doing.