Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Whitespace between functions
  • Loading branch information
hugovk committed Dec 12, 2022
commit 902f9a2c39652f136499c96385f19292c2ed6a70
29 changes: 29 additions & 0 deletions Tools/iobench/iobench.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ def get_file_sizes():
size = int(size) * {'KiB': 1024, 'MiB': 1024 ** 2}[unit]
yield s.replace(' ', ''), size


def get_binary_files():
return ((name + ".bin", size) for name, size in get_file_sizes())


def get_text_files():
return (("%s-%s-%s.txt" % (name, TEXT_ENCODING, NEWLINES), size)
for name, size in get_file_sizes())


def with_open_mode(mode):
def decorate(f):
f.file_open_mode = mode
return f
return decorate


def with_sizes(*sizes):
def decorate(f):
f.file_sizes = sizes
Expand All @@ -55,6 +59,7 @@ def read_bytewise(f):
while f.read(1):
pass


@with_open_mode("r")
@with_sizes("medium")
def read_small_chunks(f):
Expand All @@ -63,6 +68,7 @@ def read_small_chunks(f):
while f.read(20):
pass


@with_open_mode("r")
@with_sizes("medium")
def read_big_chunks(f):
Expand All @@ -71,6 +77,7 @@ def read_big_chunks(f):
while f.read(4096):
pass


@with_open_mode("r")
@with_sizes("small", "medium", "large")
def read_whole_file(f):
Expand All @@ -79,6 +86,7 @@ def read_whole_file(f):
while f.read():
pass


@with_open_mode("rt")
@with_sizes("medium")
def read_lines(f):
Expand All @@ -87,6 +95,7 @@ def read_lines(f):
for line in f:
pass


@with_open_mode("r")
@with_sizes("medium")
def seek_forward_bytewise(f):
Expand All @@ -97,6 +106,7 @@ def seek_forward_bytewise(f):
for i in range(0, size - 1):
f.seek(i, 0)


@with_open_mode("r")
@with_sizes("medium")
def seek_forward_blockwise(f):
Expand All @@ -107,6 +117,7 @@ def seek_forward_blockwise(f):
for i in range(0, size - 1, 1000):
f.seek(i, 0)


@with_open_mode("rb")
@with_sizes("medium")
def read_seek_bytewise(f):
Expand All @@ -115,6 +126,7 @@ def read_seek_bytewise(f):
while f.read(1):
f.seek(1, 1)


@with_open_mode("rb")
@with_sizes("medium")
def read_seek_blockwise(f):
Expand All @@ -131,20 +143,23 @@ def write_bytewise(f, source):
for i in range(0, len(source)):
f.write(source[i:i+1])


@with_open_mode("w")
@with_sizes("medium")
def write_small_chunks(f, source):
""" write 20 units at a time """
for i in range(0, len(source), 20):
f.write(source[i:i+20])


@with_open_mode("w")
@with_sizes("medium")
def write_medium_chunks(f, source):
""" write 4096 units at a time """
for i in range(0, len(source), 4096):
f.write(source[i:i+4096])


@with_open_mode("w")
@with_sizes("large")
def write_large_chunks(f, source):
Expand All @@ -161,6 +176,7 @@ def modify_bytewise(f, source):
for i in range(0, len(source)):
f.write(source[i:i+1])


@with_open_mode("w+")
@with_sizes("medium")
def modify_small_chunks(f, source):
Expand All @@ -169,6 +185,7 @@ def modify_small_chunks(f, source):
for i in range(0, len(source), 20):
f.write(source[i:i+20])


@with_open_mode("w+")
@with_sizes("medium")
def modify_medium_chunks(f, source):
Expand All @@ -177,6 +194,7 @@ def modify_medium_chunks(f, source):
for i in range(0, len(source), 4096):
f.write(source[i:i+4096])


@with_open_mode("wb+")
@with_sizes("medium")
def modify_seek_forward_bytewise(f, source):
Expand All @@ -186,6 +204,7 @@ def modify_seek_forward_bytewise(f, source):
f.write(source[i:i+1])
f.seek(i+2)


@with_open_mode("wb+")
@with_sizes("medium")
def modify_seek_forward_blockwise(f, source):
Expand All @@ -195,6 +214,7 @@ def modify_seek_forward_blockwise(f, source):
f.write(source[i:i+1000])
f.seek(i+2000)


# XXX the 2 following tests don't work with py3k's text IO
@with_open_mode("wb+")
@with_sizes("medium")
Expand All @@ -205,6 +225,7 @@ def read_modify_bytewise(f, source):
f.read(1)
f.write(source[i+1:i+2])


@with_open_mode("wb+")
@with_sizes("medium")
def read_modify_blockwise(f, source):
Expand Down Expand Up @@ -233,6 +254,7 @@ def read_modify_blockwise(f, source):
read_modify_bytewise, read_modify_blockwise,
]


def run_during(duration, func):
_t = time.time
n = 0
Expand All @@ -248,6 +270,7 @@ def run_during(duration, func):
real = (end[4] if start[4] else time.time()) - real_start
return n, real, sum(end[0:2]) - sum(start[0:2])


def warm_cache(filename):
with open(filename, "rb") as f:
f.read()
Expand Down Expand Up @@ -322,6 +345,7 @@ def run_test_family(tests, mode_filter, files, open_func, *make_args):
# Binary writes
if "b" in options and "w" in options:
print("\n** Binary append **\n")

def make_test_source(name, size):
with open(name, "rb") as f:
return f.read()
Expand All @@ -331,6 +355,7 @@ def make_test_source(name, size):
# Text writes
if "t" in options and "w" in options:
print("\n** Text append **\n")

def make_test_source(name, size):
with text_open(name, "r") as f:
return f.read()
Expand All @@ -340,6 +365,7 @@ def make_test_source(name, size):
# Binary overwrites
if "b" in options and "w" in options:
print("\n** Binary overwrite **\n")

def make_test_source(name, size):
with open(name, "rb") as f:
return f.read()
Expand All @@ -349,6 +375,7 @@ def make_test_source(name, size):
# Text overwrites
if "t" in options and "w" in options:
print("\n** Text overwrite **\n")

def make_test_source(name, size):
with text_open(name, "r") as f:
return f.read()
Expand Down Expand Up @@ -398,6 +425,7 @@ def prepare_files():
f.write(head)
f.write(tail)


def main():
global TEXT_ENCODING, NEWLINES

Expand Down Expand Up @@ -455,6 +483,7 @@ def main():
prepare_files()
run_all_tests(test_options)


if __name__ == "__main__":
main()

Expand Down