diff --git a/packages/node-core/src/indexer/benchmark.service.ts b/packages/node-core/src/indexer/benchmark.service.ts index b196087106..698a9a8839 100644 --- a/packages/node-core/src/indexer/benchmark.service.ts +++ b/packages/node-core/src/indexer/benchmark.service.ts @@ -1,6 +1,7 @@ // Copyright 2020-2022 OnFinality Limited authors & contributors // SPDX-License-Identifier: Apache-2.0 +import {Injectable} from '@nestjs/common'; import {OnEvent} from '@nestjs/event-emitter'; import {Interval} from '@nestjs/schedule'; import dayjs from 'dayjs'; @@ -14,6 +15,7 @@ const SAMPLING_TIME_VARIANCE = 15; const logger = getLogger('benchmark'); dayjs.extend(duration); +@Injectable() export class BenchmarkService { private currentProcessingHeight: number; private currentProcessingTimestamp: number; @@ -29,41 +31,45 @@ export class BenchmarkService { @Interval(SAMPLING_TIME_VARIANCE * 1000) async benchmark(): Promise { - if (!this.currentProcessingHeight || !this.currentProcessingTimestamp || !this.currentProcessedBlockAmount) { - await delay(10); - } else { - if (this.lastRegisteredHeight && this.lastRegisteredTimestamp) { - const heightDiff = this.currentProcessingHeight - this.lastRegisteredHeight; - const timeDiff = this.currentProcessingTimestamp - this.lastRegisteredTimestamp; - this.blockPerSecond = heightDiff === 0 || timeDiff === 0 ? 0 : heightDiff / (timeDiff / 1000); + try { + if (!this.currentProcessingHeight || !this.currentProcessingTimestamp || !this.currentProcessedBlockAmount) { + await delay(10); + } else { + if (this.lastRegisteredHeight && this.lastRegisteredTimestamp) { + const heightDiff = this.currentProcessingHeight - this.lastRegisteredHeight; + const timeDiff = this.currentProcessingTimestamp - this.lastRegisteredTimestamp; + this.blockPerSecond = heightDiff === 0 || timeDiff === 0 ? 0 : heightDiff / (timeDiff / 1000); - const blockDuration = dayjs.duration( - (this.targetHeight - this.currentProcessingHeight) / this.blockPerSecond, - 'seconds' - ); - const hoursMinsStr = blockDuration.format('HH [hours] mm [mins]'); - const days = Math.floor(blockDuration.asDays()); - const durationStr = `${days} days ${hoursMinsStr}`; + const blockDuration = dayjs.duration( + (this.targetHeight - this.currentProcessingHeight) / this.blockPerSecond, + 'seconds' + ); + const hoursMinsStr = blockDuration.format('HH [hours] mm [mins]'); + const days = Math.floor(blockDuration.asDays()); + const durationStr = `${days} days ${hoursMinsStr}`; + + if (this.nodeConfig.profiler) { + logger.info( + `Processed ${ + this.currentProcessedBlockAmount - this.lastProcessedBlockAmount + } blocks in the last ${SAMPLING_TIME_VARIANCE}secs ` + ); + } - if (this.nodeConfig.profiler) { logger.info( - `Processed ${ - this.currentProcessedBlockAmount - this.lastProcessedBlockAmount - } blocks in the last ${SAMPLING_TIME_VARIANCE}secs ` + this.targetHeight === this.lastRegisteredHeight && this.blockPerSecond === 0 + ? 'Fully synced, waiting for new blocks' + : `${this.blockPerSecond.toFixed(2)} bps, target: #${this.targetHeight}, current: #${ + this.currentProcessingHeight + }, estimate time: ${this.blockPerSecond === 0 ? 'unknown' : durationStr}` ); } - - logger.info( - this.targetHeight === this.lastRegisteredHeight && this.blockPerSecond === 0 - ? 'Fully synced, waiting for new blocks' - : `${this.blockPerSecond.toFixed(2)} bps, target: #${this.targetHeight}, current: #${ - this.currentProcessingHeight - }, estimate time: ${this.blockPerSecond === 0 ? 'unknown' : durationStr}` - ); + this.lastRegisteredHeight = this.currentProcessingHeight; + this.lastRegisteredTimestamp = this.currentProcessingTimestamp; + this.lastProcessedBlockAmount = this.currentProcessedBlockAmount; } - this.lastRegisteredHeight = this.currentProcessingHeight; - this.lastRegisteredTimestamp = this.currentProcessingTimestamp; - this.lastProcessedBlockAmount = this.currentProcessedBlockAmount; + } catch (e) { + logger.warn(e, 'Failed to measure benchmark'); } }