-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import time | ||
import hashlib | ||
|
||
def generate_random_data(size): | ||
return os.urandom(size) | ||
|
||
def write_file(filename, data): | ||
with open(filename, 'wb') as f: | ||
f.write(data) | ||
|
||
def read_file(filename): | ||
with open(filename, 'rb') as f: | ||
return f.read() | ||
|
||
def compute_checksum(data): | ||
return hashlib.sha256(data).hexdigest() | ||
|
||
def test_read_write(file_sizes, iterations): | ||
filename = 'test_file.bin' | ||
for file_size in file_sizes: | ||
data_to_write = generate_random_data(file_size) | ||
write_checksum = compute_checksum(data_to_write) | ||
start_time = time.time() | ||
for i in range(iterations): | ||
write_file(filename, data_to_write) | ||
read_data = read_file(filename) | ||
read_checksum = compute_checksum(read_data) | ||
if write_checksum != read_checksum: | ||
print(f"Data consistency check FAILED on iteration {i+1}!") | ||
break | ||
end_time = time.time() | ||
total_time = end_time - start_time | ||
print(f"Total time for {iterations} iterations: {total_time:.4f} seconds") | ||
os.remove(filename) | ||
|
||
def test_create_delete(iterations): | ||
filename = 'test_file.bin' | ||
data_to_write = generate_random_data(1) | ||
start_time = time.time() | ||
for i in range(iterations): | ||
write_file(filename, data_to_write) | ||
os.remove(filename) | ||
end_time = time.time() | ||
total_time = end_time - start_time | ||
print(f"Total time for {iterations} iterations: {total_time:.4f} seconds") | ||
|
||
if __name__ == "__main__": | ||
file_size = [4, 64, 1024, 4096] | ||
iterations = 1000 | ||
test_read_write(file_size, iterations) | ||
test_create_delete(iterations) |