Appending to LARGE log files
A colleague mentioned today that the performance of logging to a text file suffers progressively as the file grows. I decided to run some tests to confirm this.
First, I created 3 programs that logged one line at a time to a log file, each time opening the file (for append), writing to it, and then closing the file. The 3 programs compared performance of VB6 file IO, the FileSystemObject from the MS Scripting library, and .NET System.IO. Performance of all 3 was comparable (about 1300 lines per second), and appeared to be bound by CPU rather than the disk.
I then created another .NET app to test the effect of the initial file size on the process of appending to the file. The app created a file of a specified size (on an external USB HD), and then attempted to append a single line to that file. Performance suffered, badly, just as my colleague had predicted. For a new 5GB file, it took 5 minutes to append a single line. That 5 minuites was all disk activity, no CPU.
After that first appended line, performance returned to the normal speed. Closing the app, waiting several minutes, and appending another line performed normally, i.e. well. Obviously, at some level the OS has to read the whole file before it can append to it. It then caches whatever information it needs, and can append to the file normally. I don’t know how long it caches the info for.
As a final test, I created a 5GB file on a NTFS compressed directory. The file just contained NULLs, so I’m sure it compressed well. Appending a first line to this file did NOT suffer the performance penalty that an uncompressed file did. I think this is because reading the compressed file does not require much disk activity, since it is very small on the disk.
So, if your app has large log files, you might be well served to place them on a compressed directory, or else to limit their size. Otherwise, your app may be very slow to start up as it tries to write that first “I’m starting” message to the log.