Skip to content

Commit

Permalink
New test script for #709 consumes memory by reading a file over and o…
Browse files Browse the repository at this point in the history
…ver.
  • Loading branch information
donkirkby committed Feb 19, 2018
1 parent b51fbdd commit 458c8b8
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions utils/memory_consumer2.py
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()

0 comments on commit 458c8b8

Please sign in to comment.