From f6f26231a1a2c886f43d9ec965eb7903180067fb Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 24 Nov 2009 16:50:20 +0100 Subject: corrected test, added some stuff --- test/dirsize_benchmark.py | 26 ++++++++++++++++++++++++++ test/tc_directory.py | 29 ++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test/dirsize_benchmark.py (limited to 'test') diff --git a/test/dirsize_benchmark.py b/test/dirsize_benchmark.py new file mode 100644 index 00000000..38f0bfd7 --- /dev/null +++ b/test/dirsize_benchmark.py @@ -0,0 +1,26 @@ +import os, time +class Dirsize(): + def a(path): + return len(os.listdir(path)) + + def b(path): + for _, dirs, files in os.walk(path): + return len(files) + len(dirs) + + def c(path): + first = next(os.walk(path)) + return len(first[1]) + len(first[2]) + +paths = { + '/usr/lib': None, + '/usr/bin': None, + '/home/hut': None +} + +for key in paths.keys(): + paths[key] = Dirsize.a(key) # assume Dirsize.a() returns a correct result + for algo in ['a', 'b', 'c']: + t = time.time() + for i in range(4): + assert Dirsize.__dict__[algo](key) == paths[key] + print("algorithm %s: %20s: %f" % (algo, key, time.time() - t)) diff --git a/test/tc_directory.py b/test/tc_directory.py index f97f9913..ebbd6b5e 100644 --- a/test/tc_directory.py +++ b/test/tc_directory.py @@ -1,6 +1,7 @@ import unittest -import sys, os +import sys, os, time sys.path.append('../code') +os.stat_float_times(True) import directory, fsobject, file, debug TESTDIR = os.path.realpath(os.path.join(os.path.dirname(sys.argv[0]), 'testdir')) @@ -78,15 +79,33 @@ class Test1(unittest.TestCase): self.assertRaises(fsobject.FrozenException, clone.load_content) def test_load_if_outdated(self): - if os.path.exists(TESTFILE): os.unlink(TESTFILE) + # modify the directory. If the time between the last modification + # was within the filesystems resolution of mtime, we should have a re-load. + + def modify_dir(): + open(TESTFILE, 'w').close() + os.unlink(TESTFILE) + + def mtime(): + return os.stat(TESTDIR).st_mtime + dir = directory.Directory(TESTDIR) dir.load() - open(TESTFILE, 'w').close() + # If the modification happens to be in the same second as the + # last modification, it will result in mtime having the same + # integer value. So we wait until the resolution is exceeded + # and mtime differs. + old_mtime = mtime() + for i in range(50): + modify_dir() + if old_mtime != mtime(): break + time.sleep(0.1) + else: + # fail after 5 seconds of trying + self.fail("Cannot perform test: mtime of TESTDIR is not being updated.") self.assertTrue(dir.load_if_outdated()) - os.unlink(TESTFILE) - unittest.main() -- cgit 1.4.1-2-gfad0