|
| 1 | +"""Performance data streaming performance""" |
| 2 | + |
| 3 | +from lib import TestBigRepoR |
| 4 | +from gitdb.db import * |
| 5 | +from gitdb.stream import * |
| 6 | + |
| 7 | +from cStringIO import StringIO |
| 8 | +from time import time |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import stat |
| 12 | +import subprocess |
| 13 | + |
| 14 | + |
| 15 | +from lib import ( |
| 16 | + TestBigRepoR, |
| 17 | + make_bytes, |
| 18 | + with_rw_directory |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def make_memory_file(size_in_bytes, randomize=False): |
| 23 | + """:return: tuple(size_of_stream, stream) |
| 24 | + :param randomize: try to produce a very random stream""" |
| 25 | + d = make_bytes(size_in_bytes, randomize) |
| 26 | + return len(d), StringIO(d) |
| 27 | + |
| 28 | + |
| 29 | +class TestObjDBPerformance(TestBigRepoR): |
| 30 | + |
| 31 | + large_data_size_bytes = 1000*1000*10 # some MiB should do it |
| 32 | + moderate_data_size_bytes = 1000*1000*1 # just 1 MiB |
| 33 | + |
| 34 | + @with_rw_directory |
| 35 | + def test_large_data_streaming(self, path): |
| 36 | + ldb = LooseObjectDB(path) |
| 37 | + |
| 38 | + for randomize in range(2): |
| 39 | + desc = (randomize and 'random ') or '' |
| 40 | + print >> sys.stderr, "Creating %s data ..." % desc |
| 41 | + st = time() |
| 42 | + size, stream = make_memory_file(self.large_data_size_bytes, randomize) |
| 43 | + elapsed = time() - st |
| 44 | + print >> sys.stderr, "Done (in %f s)" % elapsed |
| 45 | + |
| 46 | + # writing - due to the compression it will seem faster than it is |
| 47 | + st = time() |
| 48 | + sha = ldb.store(IStream('blob', size, stream)).sha |
| 49 | + elapsed_add = time() - st |
| 50 | + assert ldb.has_object(sha) |
| 51 | + db_file = ldb.readable_db_object_path(sha) |
| 52 | + fsize_kib = os.path.getsize(db_file) / 1000 |
| 53 | + |
| 54 | + |
| 55 | + size_kib = size / 1000 |
| 56 | + print >> sys.stderr, "Added %i KiB (filesize = %i KiB) of %s data to loose odb in %f s ( %f Write KiB / s)" % (size_kib, fsize_kib, desc, elapsed_add, size_kib / elapsed_add) |
| 57 | + |
| 58 | + # reading all at once |
| 59 | + st = time() |
| 60 | + ostream = ldb.stream(sha) |
| 61 | + shadata = ostream.read() |
| 62 | + elapsed_readall = time() - st |
| 63 | + |
| 64 | + stream.seek(0) |
| 65 | + assert shadata == stream.getvalue() |
| 66 | + print >> sys.stderr, "Read %i KiB of %s data at once from loose odb in %f s ( %f Read KiB / s)" % (size_kib, desc, elapsed_readall, size_kib / elapsed_readall) |
| 67 | + |
| 68 | + |
| 69 | + # reading in chunks of 1 MiB |
| 70 | + cs = 512*1000 |
| 71 | + chunks = list() |
| 72 | + st = time() |
| 73 | + ostream = ldb.stream(sha) |
| 74 | + while True: |
| 75 | + data = ostream.read(cs) |
| 76 | + chunks.append(data) |
| 77 | + if len(data) < cs: |
| 78 | + break |
| 79 | + # END read in chunks |
| 80 | + elapsed_readchunks = time() - st |
| 81 | + |
| 82 | + stream.seek(0) |
| 83 | + assert ''.join(chunks) == stream.getvalue() |
| 84 | + |
| 85 | + cs_kib = cs / 1000 |
| 86 | + print >> sys.stderr, "Read %i KiB of %s data in %i KiB chunks from loose odb in %f s ( %f Read KiB / s)" % (size_kib, desc, cs_kib, elapsed_readchunks, size_kib / elapsed_readchunks) |
| 87 | + |
| 88 | + # del db file so git has something to do |
| 89 | + os.remove(db_file) |
| 90 | + |
| 91 | + # END for each randomization factor |
0 commit comments