From e1258956157c43d2d3a8afd1d53b939ab7546e97 Mon Sep 17 00:00:00 2001 From: shortcutme Date: Thu, 13 Apr 2017 22:54:16 +0200 Subject: [PATCH] Rev2047, Add archive benchmark --- plugins/Stats/StatsPlugin.py | 84 ++++++++++++++++++++++++++++++++++++ src/Config.py | 2 +- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/plugins/Stats/StatsPlugin.py b/plugins/Stats/StatsPlugin.py index 0f91b17e..ace7ac05 100644 --- a/plugins/Stats/StatsPlugin.py +++ b/plugins/Stats/StatsPlugin.py @@ -652,6 +652,90 @@ class UiRequestPlugin(object): gc.collect() # Implicit grabage collection + # Zip + yield "
Compression:
" + import zipfile + test_data = "Test" * 1024 + file_name = "\xc3\x81rv\xc3\xadzt\xc5\xb0r\xc5\x91t\xc3\xbck\xc3\xb6r\xc3\xb3g\xc3\xa9p\xe4\xb8\xad\xe5\x8d\x8e%s.txt" + + with benchmark("Zip pack x 10", 0.043): + for i in range(10): + with zipfile.ZipFile('%s/test.zip' % config.data_dir, 'w') as archive: + for y in range(100): + archive.writestr(file_name % y, test_data) + yield "." + assert CryptHash.sha512sum(open("%s/test.zip" % config.data_dir)) != "2b0a1c0f0e70cf777146956479a5002a0ea181c94dbfec7878451a5aa8c45d55", "Invalid hash: %s" + + with benchmark("Zip unpack x 10", 0.078): + for i in range(10): + with zipfile.ZipFile('%s/test.zip' % config.data_dir) as archive: + for y in range(100): + assert archive.read(file_name % y) == test_data + yield "." + + if os.path.isfile("%s/test.zip" % config.data_dir): + os.unlink("%s/test.zip" % config.data_dir) + + # Tar.gz + import tarfile + import struct + + # Monkey patch _init_write_gz to use fixed date in order to keep the hash independent from datetime + def nodate_write_gzip_header(self): + self.mtime = 0 + original_write_gzip_header(self) + + import gzip + original_write_gzip_header = gzip.GzipFile._write_gzip_header + gzip.GzipFile._write_gzip_header = nodate_write_gzip_header + + test_data_io = StringIO("Test" * 1024) + with benchmark("Tar.gz pack x 10", 0.131): + for i in range(10): + with tarfile.open('%s/test.tar.gz' % config.data_dir, 'w:gz') as archive: + for y in range(100): + test_data_io.seek(0) + tar_info = tarfile.TarInfo(file_name % y) + tar_info.size = 4 * 1024 + archive.addfile(tar_info, test_data_io) + yield "." + assert CryptHash.sha512sum(open("%s/test.tar.gz" % config.data_dir)) == "0c1ac0cced53533a7df5eca92b9c8c38e3d33ad4b96ec09fc5ccb2bc02cb9ffd", "Invalid hash" + + with benchmark("Tar.gz unpack x 10", 0.106): + for i in range(10): + with tarfile.open('%s/test.tar.gz' % config.data_dir, 'r:gz') as archive: + for y in range(100): + assert archive.extractfile(file_name % y).read() == test_data + yield "." + + if os.path.isfile("%s/test.tar.gz" % config.data_dir): + os.unlink("%s/test.tar.gz" % config.data_dir) + + # Tar.bz2 + import tarfile + test_data_io = StringIO("Test" * 1024) + with benchmark("Tar.bz2 pack x 10", 0.94): + for i in range(10): + with tarfile.open('%s/test.tar.bz2' % config.data_dir, 'w:bz2') as archive: + for y in range(100): + test_data_io.seek(0) + tar_info = tarfile.TarInfo(file_name % y) + tar_info.size = 4 * 1024 + archive.addfile(tar_info, test_data_io) + yield "." + + with benchmark("Tar.bz2 unpack x 10", 0.255): + for i in range(10): + with tarfile.open('%s/test.tar.bz2' % config.data_dir, 'r:bz2') as archive: + for y in range(100): + assert archive.extractfile(file_name % y).read() == test_data + yield "." + assert CryptHash.sha512sum(open("%s/test.tar.bz2" % config.data_dir)) == "182c0a3a8da7e6e2eb3bf9c661caeec8a91b12bcf389bd3facdbc5a8f6645199", "Invalid hash" + + if os.path.isfile("%s/test.tar.bz2a" % config.data_dir): + os.unlink("%s/test.tar.bz2a" % config.data_dir) + + yield "
Done. Total: %.2fs" % (time.time() - t) def actionGcCollect(self): diff --git a/src/Config.py b/src/Config.py index c6da0a93..e0d79c0a 100644 --- a/src/Config.py +++ b/src/Config.py @@ -10,7 +10,7 @@ class Config(object): def __init__(self, argv): self.version = "0.5.4" - self.rev = 2046 + self.rev = 2047 self.argv = argv self.action = None self.config_file = "zeronet.conf"