spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Custom BUFSIZ support

Open randomizedthinking opened this issue 4 years ago • 4 comments

The default BUFSIZ parameter on most Linux systems is simply 8K. On my box, a dd command shows a buffer size of 512K would have much better performance. I wonder whether there is any way I can use a custom buffer size when creating a logger.

In my case, spdlog is used to stream data to multiple (about 100) files. I suspect the default flush policy is hitting the disk too frequently, and would like to see whether there is any way to improve the performance.

randomizedthinking avatar Jan 13 '21 10:01 randomizedthinking

Maybe calling https://en.cppreference.com/w/cpp/io/c/setvbuf somewhere in the file sink impl would do the trick?

gabime avatar Jan 13 '21 16:01 gabime

It should work, and spdlog can add a parameter to expose it to end users.

randomizedthinking avatar Jan 13 '21 17:01 randomizedthinking

Would be nice if you could try and report the actual performance gain.

gabime avatar Jan 13 '21 18:01 gabime

Here is a simple test of using different buffer size, and the results are significant.

For simplicity, I only tested single-thread since the buffer is managed by myself. The bench_single_threaded function is used to evaluation the performance.

[info] **************************************************************                                                                                                                                                                         
[info] Single threaded: 2500000 messages; BufSize: 8192                                                                                                                                                                                       
[info] **************************************************************                                                                                                                                                                         
[info] basic_st                       Elapsed: 1.20 secs        2,089,389/sec                                                                                                                                                                 
[info] basic_st/backtrace-on          Elapsed: 1.29 secs        1,931,904/sec                                                                                                                                                                 
[info] **************************************************************                                                                                                                                                                         
[info] Single threaded: 2500000 messages; BufSize: 262144                                                                                                                                                                                      
[info] **************************************************************                                                                                                                                                                         
[info] basic_st                       Elapsed: 0.62 secs        4,021,943/sec                                                                                                                                                                 
[info] basic_st/backtrace-on          Elapsed: 0.64 secs        3,896,717/sec                                                                                                                                                                 

randomizedthinking avatar Jan 14 '21 19:01 randomizedthinking