aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2015-12-05 14:03:15 -0700
committerBen Johnson <benbjohnson@yahoo.com>2015-12-05 14:03:15 -0700
commit0dfebeb660e15d2962cc4d67cbfb74f0feb8e82b (patch)
treeff2b9edaaff81cc4b68ef19ae294c94005831e6e
parentMerge pull request #462 from rhcarvalho/patch-readme (diff)
parentFix `bolt stats` output (diff)
downloaddedo-0dfebeb660e15d2962cc4d67cbfb74f0feb8e82b.tar.gz
dedo-0dfebeb660e15d2962cc4d67cbfb74f0feb8e82b.tar.xz
Merge pull request #463 from rhcarvalho/fix-stats
Fix `bolt stats` output
-rw-r--r--cmd/bolt/main.go5
-rw-r--r--cmd/bolt/main_test.go40
2 files changed, 44 insertions, 1 deletions
diff --git a/cmd/bolt/main.go b/cmd/bolt/main.go
index c41ebe4..b96e6f7 100644
--- a/cmd/bolt/main.go
+++ b/cmd/bolt/main.go
@@ -825,7 +825,10 @@ func (cmd *StatsCommand) Run(args ...string) error {
fmt.Fprintln(cmd.Stdout, "Bucket statistics")
fmt.Fprintf(cmd.Stdout, "\tTotal number of buckets: %d\n", s.BucketN)
- percentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN))
+ percentage = 0
+ if s.BucketN != 0 {
+ percentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN))
+ }
fmt.Fprintf(cmd.Stdout, "\tTotal number on inlined buckets: %d (%d%%)\n", s.InlineBucketN, percentage)
percentage = 0
if s.LeafInuse != 0 {
diff --git a/cmd/bolt/main_test.go b/cmd/bolt/main_test.go
index b9e8c67..c378b79 100644
--- a/cmd/bolt/main_test.go
+++ b/cmd/bolt/main_test.go
@@ -24,6 +24,46 @@ func TestInfoCommand_Run(t *testing.T) {
}
}
+// Ensure the "stats" command executes correctly with an empty database.
+func TestStatsCommand_Run_EmptyDatabase(t *testing.T) {
+ // Ignore
+ if os.Getpagesize() != 4096 {
+ t.Skip("system does not use 4KB page size")
+ }
+
+ db := MustOpen(0666, nil)
+ defer db.Close()
+ db.DB.Close()
+
+ // Generate expected result.
+ exp := "Aggregate statistics for 0 buckets\n\n" +
+ "Page count statistics\n" +
+ "\tNumber of logical branch pages: 0\n" +
+ "\tNumber of physical branch overflow pages: 0\n" +
+ "\tNumber of logical leaf pages: 0\n" +
+ "\tNumber of physical leaf overflow pages: 0\n" +
+ "Tree statistics\n" +
+ "\tNumber of keys/value pairs: 0\n" +
+ "\tNumber of levels in B+tree: 0\n" +
+ "Page size utilization\n" +
+ "\tBytes allocated for physical branch pages: 0\n" +
+ "\tBytes actually used for branch data: 0 (0%)\n" +
+ "\tBytes allocated for physical leaf pages: 0\n" +
+ "\tBytes actually used for leaf data: 0 (0%)\n" +
+ "Bucket statistics\n" +
+ "\tTotal number of buckets: 0\n" +
+ "\tTotal number on inlined buckets: 0 (0%)\n" +
+ "\tBytes used for inlined buckets: 0 (0%)\n"
+
+ // Run the command.
+ m := NewMain()
+ if err := m.Run("stats", db.Path); err != nil {
+ t.Fatal(err)
+ } else if m.Stdout.String() != exp {
+ t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String())
+ }
+}
+
// Ensure the "stats" command can execute correctly.
func TestStatsCommand_Run(t *testing.T) {
// Ignore