From a6227f8acca922e25cfef94f831732aa1de66b26 Mon Sep 17 00:00:00 2001 From: Steven Berler Date: Thu, 22 Dec 2011 23:21:58 +0000 Subject: [PATCH] improve random file generation performance Makes the random file generator not generate more data than the size of the file. Previously it would generate 1 MB of data even if the file was much smaller. --- s3tests/realistic.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/s3tests/realistic.py b/s3tests/realistic.py index b88a727..38fd9ba 100644 --- a/s3tests/realistic.py +++ b/s3tests/realistic.py @@ -3,6 +3,7 @@ import random import string import struct import time +import math NANOSECOND = int(1e9) @@ -45,10 +46,11 @@ class RandomContentFile(object): def _generate(self): # generate and return a chunk of pseudorandom data - # 256 bits = 32 bytes at a time - size = 1*1024*1024 - l = [self.random.getrandbits(64) for _ in xrange(size/8)] - s = struct.pack((size/8)*'Q', *l) + size = min(self.size, 1*1024*1024) # generate at most 1 MB at a time + chunks = int(math.ceil(size/8.0)) # number of 8-byte chunks to create + + l = [self.random.getrandbits(64) for _ in xrange(chunks)] + s = struct.pack(chunks*'Q', *l) return s def read(self, size=-1):