From f0acf6ac9bce796d67adb04b2d6bfb06dadaa72d Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Wed, 9 Aug 2023 14:24:51 +0700 Subject: [PATCH] chore: update computeDeltas perf test --- .../perf/protoArray/computeDeltas.test.ts | 122 ++++++------------ 1 file changed, 43 insertions(+), 79 deletions(-) diff --git a/packages/fork-choice/test/perf/protoArray/computeDeltas.test.ts b/packages/fork-choice/test/perf/protoArray/computeDeltas.test.ts index f8f1fef38e15..10421f8b9a76 100644 --- a/packages/fork-choice/test/perf/protoArray/computeDeltas.test.ts +++ b/packages/fork-choice/test/perf/protoArray/computeDeltas.test.ts @@ -1,98 +1,62 @@ import {itBench, setBenchOpts} from "@dapplion/benchmark"; -import {expect} from "chai"; -import { - CachedBeaconStateAltair, - computeStartSlotAtEpoch, - EffectiveBalanceIncrements, - getEffectiveBalanceIncrementsZeroed, -} from "@lodestar/state-transition"; -import {TIMELY_SOURCE_FLAG_INDEX} from "@lodestar/params"; -// eslint-disable-next-line import/no-relative-packages -import {generatePerfTestCachedStateAltair} from "../../../../state-transition/test/perf/util.js"; +import {EffectiveBalanceIncrements, getEffectiveBalanceIncrementsZeroed} from "@lodestar/state-transition"; import {VoteTracker} from "../../../src/protoArray/interface.js"; import {computeDeltas} from "../../../src/protoArray/computeDeltas.js"; import {computeProposerBoostScoreFromBalances} from "../../../src/forkChoice/forkChoice.js"; -/** Same to https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.5/specs/altair/beacon-chain.md#has_flag */ -const TIMELY_SOURCE = 1 << TIMELY_SOURCE_FLAG_INDEX; -function flagIsTimelySource(flag: number): boolean { - return (flag & TIMELY_SOURCE) === TIMELY_SOURCE; -} - describe("computeDeltas", () => { - let originalState: CachedBeaconStateAltair; const indices: Map = new Map(); let oldBalances: EffectiveBalanceIncrements; let newBalances: EffectiveBalanceIncrements; const oldRoot = "0x32dec344944029ba183ac387a7aa1f2068591c00e9bfadcfb238e50fbe9ea38e"; const newRoot = "0xb59f3a209f639dd6b5645ea9fad8d441df44c3be93bd1bbf50ef90bf124d1238"; - - before(function () { - this.timeout(2 * 60 * 1000); // Generating the states for the first time is very slow - - originalState = generatePerfTestCachedStateAltair({goBackOneSlot: true}); - - const previousEpochParticipationArr = originalState.previousEpochParticipation.getAll(); - const currentEpochParticipationArr = originalState.currentEpochParticipation.getAll(); - - const numPreviousEpochParticipation = previousEpochParticipationArr.filter(flagIsTimelySource).length; - const numCurrentEpochParticipation = currentEpochParticipationArr.filter(flagIsTimelySource).length; - - expect(numPreviousEpochParticipation).to.equal(250000, "Wrong numPreviousEpochParticipation"); - expect(numCurrentEpochParticipation).to.equal(250000, "Wrong numCurrentEpochParticipation"); - - oldBalances = getEffectiveBalanceIncrementsZeroed(numPreviousEpochParticipation); - newBalances = getEffectiveBalanceIncrementsZeroed(numPreviousEpochParticipation); - - for (let i = 0; i < numPreviousEpochParticipation; i++) { - oldBalances[i] = 32; - newBalances[i] = 32; - } - for (let i = 0; i < 10000; i++) { - indices.set("" + i, i); - } - indices.set(oldRoot, 1001); - indices.set(newRoot, 1001); - }); - - setBenchOpts({ - minMs: 30 * 1000, - maxMs: 40 * 1000, - }); - - itBench({ - id: "computeDeltas", - beforeEach: () => { - const votes: VoteTracker[] = []; - const epoch = originalState.epochCtx.currentShuffling.epoch; - const committee = originalState.epochCtx.getBeaconCommittee(computeStartSlotAtEpoch(epoch), 0); - for (let i = 0; i < 250000; i++) { - if (committee.includes(i)) { + for (const numValidator of [800_000, 2_100_100]) { + before(function () { + this.timeout(2 * 60 * 1000); + oldBalances = getEffectiveBalanceIncrementsZeroed(numValidator); + newBalances = getEffectiveBalanceIncrementsZeroed(numValidator); + + for (let i = 0; i < numValidator; i++) { + oldBalances[i] = 32; + newBalances[i] = 32; + } + for (let i = 0; i < 10000; i++) { + indices.set("" + i, i); + } + indices.set(oldRoot, 1001); + indices.set(newRoot, 1002); + }); + + setBenchOpts({ + minMs: 30 * 1000, + maxMs: 40 * 1000, + }); + + itBench({ + id: `computeDeltas ${numValidator} validators`, + beforeEach: () => { + const votes: VoteTracker[] = []; + const epoch = 100_000; + for (let i = 0; i < numValidator; i++) { votes.push({ currentRoot: oldRoot, nextRoot: newRoot, nextEpoch: epoch, }); - } else { - votes.push({ - currentRoot: oldRoot, - nextRoot: oldRoot, - nextEpoch: epoch - 1, - }); } - } - return votes; - }, - fn: (votes) => { - computeDeltas(indices, votes, oldBalances, newBalances, new Set()); - }, - }); - - itBench({ - id: "computeProposerBoostScoreFromBalances", - fn: () => { - computeProposerBoostScoreFromBalances(newBalances, {slotsPerEpoch: 32, proposerScoreBoost: 70}); - }, - }); + return votes; + }, + fn: (votes) => { + computeDeltas(indices, votes, oldBalances, newBalances, new Set()); + }, + }); + + itBench({ + id: `computeProposerBoostScoreFromBalances ${numValidator} validators`, + fn: () => { + computeProposerBoostScoreFromBalances(newBalances, {slotsPerEpoch: 32, proposerScoreBoost: 70}); + }, + }); + } });