I am writing the benchmarking for file IO operation in Go.
I want to avoid any OS level asynchronization or caching while performing simple IO operations like create, open, read, write, delete file.
from this SO question When to flush a file in Go? I got to know, simple writing is not enough as it doesn't guarantee an actual disk operation completed.
Thus I changed my code as
start := time.Now()
_, e = file.Write(b)
if e == nil {
e = file.Sync()
}
elapsed := time.Since(start)
I am assuming this will take care of write operation.
For Create, Read, and Open operation, I am going with my intuition that if relevant operations are done without any errors, an actual disk operation would have performed. Please correct my understanding here if it is wrong.
My concern is for closing and deleting the file.
I checked this Safely close a file descriptor in golang, but could not make much sense in my context
Currently, my file closing code looks like
start := time.Now()
e = file.Close()
elapsed := time.Since(start)
And I am using os.Remove(fileName)
to delete the file.
I can think about possible OS level asynchronization for close/delete operation as well in a way when program calls to close/delete the file, the file instance would be marked to be closed or deleted and actual disk operation would be done when OS decides.
Can sync
method be helpful in closing the file?
What is better way to guarantee that the file is deleting (without putting much overhead - like if file exist loop right after deleting)