Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nodeConfig not being provided to benchmark service #1338

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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