Skip to content

Commit

Permalink
Fix nodeConfig not being provided to benchmark service (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Oct 5, 2022
1 parent e72ea41 commit a8f5485
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions packages/node-core/src/indexer/benchmark.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -29,41 +31,45 @@ export class BenchmarkService {

@Interval(SAMPLING_TIME_VARIANCE * 1000)
async benchmark(): Promise<void> {
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');
}
}

Expand Down

0 comments on commit a8f5485

Please sign in to comment.