From 7b36a1431cfd5bdd20ba5b99524881aa2f405e7a Mon Sep 17 00:00:00 2001
From: Neil Booth <kyuupichan@gmail.com>
Date: Sun, 9 Oct 2016 13:00:46 +0900
Subject: [PATCH] Add instrumentation

---
 server/db.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/server/db.py b/server/db.py
index 35c7e47..9eacc54 100644
--- a/server/db.py
+++ b/server/db.py
@@ -191,13 +191,16 @@ class DB(object):
         with self.db.write_batch(transaction=True) as batch:
             # Flush the state, then the cache, then the history
             self.put_state()
-            for key, value in self.write_cache.items():
+            for n, (key, value) in enumerate(self.write_cache.items()):
                 if value is None:
                     batch.delete(key)
                     deletes += 1
                 else:
                     batch.put(key, value)
                     writes += 1
+                if n % 1000 == 0:
+                    pct = n // len(self.write_cache) * 100
+                    self.logger.info('{:d} {:d}% done...'.format(n, pct))
 
             self.flush_history()
 
@@ -220,7 +223,7 @@ class DB(object):
         # Drop any None entry
         self.history.pop(None, None)
 
-        for hash160, hist in self.history.items():
+        for m, (hash160, hist) in enumerate(self.history.items()):
             prefix = b'H' + hash160
             for key, v in self.db.iterator(reverse=True, prefix=prefix,
                                            fill_cache=False):
@@ -245,6 +248,10 @@ class DB(object):
                                          .format(addr, idx))
                     self.db.put(key, v[n:n + HIST_ENTRY_LEN])
 
+            if m % 1000 == 0:
+                pct = m // len(self.history) * 100
+                self.logger.info('{:d} {:d}% done...'.format(m, pct))
+
         self.history = defaultdict(list)
 
     def get_hash160(self, tx_hash, idx, delete=True):