accounting.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "fmt"
  9. "log"
  10. "os"
  11. "text/tabwriter"
  12. "github.com/syncthing/syncthing/lib/db/backend"
  13. )
  14. // account prints key and data size statistics per class
  15. func account(ldb backend.Backend) {
  16. it, err := ldb.NewPrefixIterator(nil)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. var ksizes [256]int
  21. var dsizes [256]int
  22. var counts [256]int
  23. var max [256]int
  24. for it.Next() {
  25. key := it.Key()
  26. t := key[0]
  27. ds := len(it.Value())
  28. ks := len(key)
  29. s := ks + ds
  30. counts[t]++
  31. ksizes[t] += ks
  32. dsizes[t] += ds
  33. if s > max[t] {
  34. max[t] = s
  35. }
  36. }
  37. tw := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', tabwriter.AlignRight)
  38. toti, totds, totks := 0, 0, 0
  39. for t := range ksizes {
  40. if ksizes[t] > 0 {
  41. // yes metric kilobytes 🤘
  42. fmt.Fprintf(tw, "0x%02x:\t%d items,\t%d KB keys +\t%d KB data,\t%d B +\t%d B avg,\t%d B max\t\n", t, counts[t], ksizes[t]/1000, dsizes[t]/1000, ksizes[t]/counts[t], dsizes[t]/counts[t], max[t])
  43. toti += counts[t]
  44. totds += dsizes[t]
  45. totks += ksizes[t]
  46. }
  47. }
  48. fmt.Fprintf(tw, "Total\t%d items,\t%d KB keys +\t%d KB data.\t\n", toti, totks/1000, totds/1000)
  49. tw.Flush()
  50. }