-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New test script for #709 consumes memory by reading a file over and o…
…ver.
- Loading branch information
Showing
1 changed file
with
55 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,55 @@ | ||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, FileType | ||
|
||
from datetime import datetime, timedelta | ||
|
||
|
||
def parse_args(): | ||
parser = ArgumentParser(description='Consume memory, and keep it active.', | ||
formatter_class=ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('-t', | ||
'--time', | ||
type=int, | ||
help='time in seconds to run') | ||
parser.add_argument('source', | ||
type=FileType('rb'), | ||
default='/dev/urandom', | ||
nargs='?', | ||
help='source file to read data from') | ||
parser.add_argument('size', | ||
type=int, | ||
default=1000, | ||
nargs='?', | ||
help='size of memory to hold, in MB') | ||
return parser.parse_args() | ||
|
||
|
||
def read_chunk(source): | ||
read_size = 1024*1024 | ||
chunk = '' | ||
while read_size: | ||
piece = source.read(read_size) | ||
if not piece: | ||
source.seek(0) | ||
piece = '!' | ||
chunk += piece | ||
read_size -= len(piece) | ||
return chunk | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
if args.time is None: | ||
end_time = None | ||
else: | ||
end_time = datetime.now() + timedelta(seconds=args.time) | ||
chunks = [] | ||
allocated_count = 0 | ||
while end_time is None or datetime.now() < end_time: | ||
while len(chunks) > args.size: | ||
chunks.pop(0) | ||
chunks.append(read_chunk(args.source)) | ||
allocated_count += 1 | ||
print('Allocated {}; now holding {}.'.format(allocated_count, len(chunks))) | ||
|
||
|
||
main() |