How to accurately benchmark a function that writes a file via mmap? Pauses later while printing, after the timed region?

Viewed 67

I am trying to benchmark a public interface provided by someone else, which use mmap to write some in-memory object to disk. So i write such code snippet.

void test(){
    auto start = std::chrono::high_resolution_clock::now();
    func_that_use_mmap_inside();
    auto duration = std::chrono::duration<double, std::milli>(std::chrono::high_resolution_clock::now() - start).count();
    printf("duration: %.2f\n", duration);
}

This test function has some problems. When running it in my laptop, it gives a quite low output, like 20ms. But in fact the program stall many seconds before printing the duration, which is definitely much more than 20ms, and i'm pretty sure there are no operation other than func_that_use_mmap_inside.

I am guessing that the program behaviour is similar to call msync with SYNC symbol during the main clean up phase, thus explain the difference between low printf output and high actual time. If i can modify the func_that_use_mmap_inside, i can add a msync call to get the right result, but actually i can't. So how should i perform a "right" benchmark for func_that_use_mmap_inside?

0 Answers
Related