'tahoe stats': tolerate empty directories. Closes #693.

This commit is contained in:
Brian Warner 2009-07-15 00:51:09 -07:00
parent 7950fc0f2e
commit efa608a135
2 changed files with 62 additions and 18 deletions

View File

@ -117,24 +117,25 @@ class StatsGrabber(SlowOperationRunner):
print >>stdout, fmt % (k, data[k]), " ", absize print >>stdout, fmt % (k, data[k]), " ", absize
else: else:
print >>stdout, fmt % (k, data[k]) print >>stdout, fmt % (k, data[k])
print >>stdout, "Size Histogram:" if data["size-files-histogram"]:
prevmax = None print >>stdout, "Size Histogram:"
maxlen = max([len(str(maxsize)) prevmax = None
for (minsize, maxsize, count) maxlen = max([len(str(maxsize))
in data["size-files-histogram"]]) for (minsize, maxsize, count)
maxcountlen = max([len(str(count)) in data["size-files-histogram"]])
for (minsize, maxsize, count) maxcountlen = max([len(str(count))
in data["size-files-histogram"]]) for (minsize, maxsize, count)
minfmt = "%" + str(maxlen) + "d" in data["size-files-histogram"]])
maxfmt = "%-" + str(maxlen) + "d" minfmt = "%" + str(maxlen) + "d"
countfmt = "%-" + str(maxcountlen) + "d" maxfmt = "%-" + str(maxlen) + "d"
linefmt = minfmt + "-" + maxfmt + " : " + countfmt + " %s" countfmt = "%-" + str(maxcountlen) + "d"
for (minsize, maxsize, count) in data["size-files-histogram"]: linefmt = minfmt + "-" + maxfmt + " : " + countfmt + " %s"
if prevmax is not None and minsize != prevmax+1: for (minsize, maxsize, count) in data["size-files-histogram"]:
print >>stdout, " "*(maxlen-1) + "..." if prevmax is not None and minsize != prevmax+1:
prevmax = maxsize print >>stdout, " "*(maxlen-1) + "..."
print >>stdout, linefmt % (minsize, maxsize, count, prevmax = maxsize
abbreviate_space_both(maxsize)) print >>stdout, linefmt % (minsize, maxsize, count,
abbreviate_space_both(maxsize))
def stats(options): def stats(options):
return StatsGrabber().run(options) return StatsGrabber().run(options)

View File

@ -1268,6 +1268,21 @@ class Check(GridTestMixin, CLITestMixin, unittest.TestCase):
in lines, out) in lines, out)
d.addCallback(_check2) d.addCallback(_check2)
d.addCallback(lambda ign: self.do_cli("stats", self.rooturi))
def _check_stats((rc, out, err)):
self.failUnlessEqual(err, "")
self.failUnlessEqual(rc, 0)
lines = out.splitlines()
self.failUnlessIn(" count-immutable-files: 1", lines)
self.failUnlessIn(" count-mutable-files: 1", lines)
self.failUnlessIn(" count-literal-files: 1", lines)
self.failUnlessIn(" count-directories: 1", lines)
self.failUnlessIn(" size-immutable-files: 400", lines)
self.failUnlessIn("Size Histogram:", lines)
self.failUnlessIn(" 4-10 : 1 (10 B, 10 B)", lines)
self.failUnlessIn(" 317-1000 : 1 (1000 B, 1000 B)", lines)
d.addCallback(_check_stats)
def _clobber_shares(ignored): def _clobber_shares(ignored):
shares = self.find_shares(self.uris["good"]) shares = self.find_shares(self.uris["good"])
self.failUnlessEqual(len(shares), 10) self.failUnlessEqual(len(shares), 10)
@ -1431,3 +1446,31 @@ class Errors(GridTestMixin, CLITestMixin, unittest.TestCase):
d.addCallback(_check1) d.addCallback(_check1)
return d return d
class Stats(GridTestMixin, CLITestMixin, unittest.TestCase):
def test_empty_directory(self):
self.basedir = "cli/Stats/empty_directory"
self.set_up_grid()
c0 = self.g.clients[0]
self.fileurls = {}
d = c0.create_empty_dirnode()
def _stash_root(n):
self.rootnode = n
self.rooturi = n.get_uri()
d.addCallback(_stash_root)
# make sure we can get stats on an empty directory too
d.addCallback(lambda ign: self.do_cli("stats", self.rooturi))
def _check_stats((rc, out, err)):
self.failUnlessEqual(err, "")
self.failUnlessEqual(rc, 0)
lines = out.splitlines()
self.failUnlessIn(" count-immutable-files: 0", lines)
self.failUnlessIn(" count-mutable-files: 0", lines)
self.failUnlessIn(" count-literal-files: 0", lines)
self.failUnlessIn(" count-directories: 1", lines)
self.failUnlessIn(" size-immutable-files: 0", lines)
self.failIfIn("Size Histogram:", lines)
d.addCallback(_check_stats)
return d