From 65fe5616be0c2e8f6e9eb12de584eb82e8d8e4b7 Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Mon, 11 May 2020 23:30:01 +0200
Subject: [PATCH] [feat] sync: print sync duration at end

---
 app/modules/crawler/lib/sync.ts         |  6 ++++--
 app/modules/crawler/lib/sync/Watcher.ts | 16 +++++++++++-----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/app/modules/crawler/lib/sync.ts b/app/modules/crawler/lib/sync.ts
index 1cab9817f..69d073a23 100644
--- a/app/modules/crawler/lib/sync.ts
+++ b/app/modules/crawler/lib/sync.ts
@@ -140,6 +140,7 @@ export class Synchroniser extends stream.Duplex {
       this.logger.info("Sync started.");
 
       const fullSync = !to;
+      const syncStartTime = Date.now();
 
       //============
       // Blockchain headers
@@ -240,9 +241,10 @@ export class Synchroniser extends stream.Duplex {
         await this.syncStrategy.syncPeers(fullSync, to);
       }
 
-      this.watcher.end();
+      const syncDuration = Date.now() - syncStartTime;
+      this.watcher.end(syncDuration);
       this.push({ sync: true });
-      this.logger.info("Sync finished.");
+      this.logger.info("Sync finished (duration %s ms).", syncDuration);
     } catch (err) {
       this.push({ sync: false, msg: err });
       err &&
diff --git a/app/modules/crawler/lib/sync/Watcher.ts b/app/modules/crawler/lib/sync/Watcher.ts
index 796d99b2b..c0cfac51f 100644
--- a/app/modules/crawler/lib/sync/Watcher.ts
+++ b/app/modules/crawler/lib/sync/Watcher.ts
@@ -11,7 +11,7 @@ export interface Watcher {
   appliedPercent(pct?: number): number;
   sbxPercent(pct?: number): number;
   peersPercent(pct?: number): number;
-  end(): void;
+  end(duration?: number): void;
 
   reserveNodes(nodesAvailable: P2pCandidate[]): void;
 
@@ -114,8 +114,8 @@ export class EventWatcher extends events.EventEmitter implements Watcher {
     return method(pct);
   }
 
-  end(): void {
-    this.innerWatcher.end();
+  end(syncDuration?: number): void {
+    this.innerWatcher.end(syncDuration);
   }
 
   onEvent(e: EventName, cb: (pct: number) => void) {
@@ -261,8 +261,14 @@ export class MultimeterWatcher implements Watcher {
     return 0;
   }
 
-  end() {
-    this.multi.write("\nAll done.\n");
+  end(duration?: number) {
+    if (duration) {
+      const durationSecs = Math.floor(duration / 1000);
+      const durationMillisRemain = duration % 1000;
+      this.multi.write("\nAll done in " + durationSecs + "." + durationMillisRemain + " seconds.\n");
+    } else {
+      this.multi.write("\nAll done.\n");
+    }
     this.multi.destroy();
   }
 
-- 
GitLab