Closed supitsdu closed 2 months ago
I evaluated the performance impact of using buffered I/O (bufio.Reader
) for reading file content in Clipper, addressing issue #22. Our goal was to enhance performance, particularly with large files. However, benchmarks revealed that the non-buffered implementation outperforms the buffered one.
With Buffered I/O:
Without Buffered I/O:
Huge Files (500 MB):
Large Files (50 MB):
Medium Files (1 MB):
Small Files (24 KB):
The anticipated performance improvements from using buffered I/O were not realized. The non-buffered implementation consistently outperforms the buffered one across all tested file sizes.
We will close PR #33 and continue using the non-buffered I/O implementation. This decision is based on the benchmark results showing superior performance of the non-buffered approach.
If future developments suggest a more efficient use of buffered I/O, we will re-evaluate this decision. All findings and benchmark results will be documented for future reference.
// BenchmarkFileContentReader benchmarks the Read method of FileContentReader for different file sizes.
func BenchmarkFileContentReader(b *testing.B) {
sizes := map[string]int{
"Small": 24 * 1024,
"Medium": 1 * 1024 * 1024,
"Large": 50 * 1024 * 1024,
"Huge": 500 * 1024 * 1024,
}
for sizeName, size := range sizes {
b.Run(sizeName, func(b *testing.B) {
largeInput := randomstring.String(size)
tempFile, err := createBenchmarkTempFile(b, largeInput)
if err != nil {
b.Fatalf("Failed to create temp file: %v", err)
}
reader := clipper.FileContentReader{FilePath: tempFile.Name()}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := reader.Read()
if err != nil {
b.Fatalf("FileContentReader.Read failed: %v", err)
}
}
})
}
}
// createTempFile creates a temporary file for testing purposes and writes the given content to it.
func createBenchmarkTempFile(b *testing.B, content string) (*os.File, error) {
b.Helper()
file, err := os.CreateTemp(b.TempDir(), "testfile")
if err != nil {
return nil, err
}
_, err = file.WriteString(content)
if err != nil {
return nil, err
}
return file, nil
}
Currently, clipper reads and writes file content directly using standard I/O operations. When dealing with large files, this can lead to suboptimal performance due to the overhead of frequent system calls.
We could introduce buffered I/O (e.g.: bufio) for file operations. This involves using a buffer to read and write data in larger chunks, reducing the number of system calls and potentially significantly improving performance, especially when working with large files.
Tasks: