Skip to content

Commit

Permalink
test(nns): Add a benchmark for list neurons (#3076)
Browse files Browse the repository at this point in the history
Adds a benchmark for list_neurons
  • Loading branch information
max-dfinity authored Dec 11, 2024
1 parent 00d999c commit cf7d6c2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 19 deletions.
46 changes: 29 additions & 17 deletions rs/nns/governance/canbench/canbench_results.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
benches:
add_neuron_active_maximum:
total:
instructions: 36177920
instructions: 36176826
heap_increase: 1
stable_memory_increase: 0
scopes: {}
add_neuron_active_typical:
total:
instructions: 1835389
instructions: 1835322
heap_increase: 0
stable_memory_increase: 0
scopes: {}
add_neuron_inactive_maximum:
total:
instructions: 96156666
instructions: 96155572
heap_increase: 1
stable_memory_increase: 0
scopes: {}
add_neuron_inactive_typical:
total:
instructions: 7370994
instructions: 7370927
heap_increase: 0
stable_memory_increase: 0
scopes: {}
cascading_vote_all_heap:
total:
instructions: 34432365
instructions: 34422494
heap_increase: 0
stable_memory_increase: 128
scopes: {}
cascading_vote_heap_neurons_stable_index:
total:
instructions: 56567262
instructions: 56557391
heap_increase: 0
stable_memory_increase: 128
scopes: {}
cascading_vote_stable_everything:
total:
instructions: 372102159
instructions: 371631388
heap_increase: 0
stable_memory_increase: 128
scopes: {}
cascading_vote_stable_neurons_with_heap_index:
total:
instructions: 349917023
instructions: 349446252
heap_increase: 0
stable_memory_increase: 128
scopes: {}
centralized_following_all_stable:
total:
instructions: 173910217
instructions: 173749742
heap_increase: 0
stable_memory_increase: 128
scopes: {}
Expand All @@ -61,7 +61,7 @@ benches:
scopes: {}
draw_maturity_from_neurons_fund_heap:
total:
instructions: 7252619
instructions: 7245419
heap_increase: 0
stable_memory_increase: 0
scopes: {}
Expand All @@ -73,16 +73,22 @@ benches:
scopes: {}
list_active_neurons_fund_neurons_heap:
total:
instructions: 438096
instructions: 440980
heap_increase: 0
stable_memory_increase: 0
scopes: {}
list_active_neurons_fund_neurons_stable:
total:
instructions: 10293308
instructions: 2450275
heap_increase: 0
stable_memory_increase: 0
scopes: {}
list_neurons_heap:
total:
instructions: 3900688
heap_increase: 9
stable_memory_increase: 0
scopes: {}
list_neurons_ready_to_unstake_maturity_heap:
total:
instructions: 471240
Expand All @@ -91,10 +97,16 @@ benches:
scopes: {}
list_neurons_ready_to_unstake_maturity_stable:
total:
instructions: 36937433
instructions: 36937653
heap_increase: 0
stable_memory_increase: 0
scopes: {}
list_neurons_stable:
total:
instructions: 97867451
heap_increase: 5
stable_memory_increase: 0
scopes: {}
list_ready_to_spawn_neuron_ids_heap:
total:
instructions: 459353
Expand All @@ -103,19 +115,19 @@ benches:
scopes: {}
list_ready_to_spawn_neuron_ids_stable:
total:
instructions: 36926134
instructions: 36926354
heap_increase: 0
stable_memory_increase: 0
scopes: {}
neuron_metrics_calculation_heap:
total:
instructions: 536345
instructions: 1077651
heap_increase: 0
stable_memory_increase: 0
scopes: {}
neuron_metrics_calculation_stable:
total:
instructions: 1841727
instructions: 2476865
heap_increase: 0
stable_memory_increase: 0
scopes: {}
Expand All @@ -127,7 +139,7 @@ benches:
scopes: {}
single_vote_all_stable:
total:
instructions: 2474981
instructions: 2474974
heap_increase: 0
stable_memory_increase: 128
scopes: {}
Expand Down
58 changes: 57 additions & 1 deletion rs/nns/governance/src/governance/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
neuron_store::NeuronStore,
pb::v1::{
neuron::Followees, proposal::Action, Ballot, BallotInfo, Governance as GovernanceProto,
KnownNeuron, Neuron as NeuronProto, ProposalData, Topic, Vote, VotingPowerEconomics,
KnownNeuron, ListNeurons, Neuron as NeuronProto, ProposalData, Topic, Vote,
VotingPowerEconomics,
},
temporarily_disable_active_neurons_in_stable_memory,
temporarily_disable_stable_memory_following_index,
Expand Down Expand Up @@ -512,3 +513,58 @@ fn compute_ballots_for_new_proposal_with_stable_neurons() -> BenchResult {
.expect("Failed!");
})
}

fn list_neurons_benchmark() -> BenchResult {
let neurons = (0..100)
.map(|id| {
(id, {
let mut neuron: NeuronProto = make_neuron(
id,
PrincipalId::new_user_test_id(id),
1_000_000_000,
hashmap! {}, // get the default followees
)
.into_proto(&VotingPowerEconomics::DEFAULT, 123_456_789);
neuron.hot_keys = vec![PrincipalId::new_user_test_id(1)];
neuron
})
})
.collect::<BTreeMap<u64, NeuronProto>>();

let governance_proto = GovernanceProto {
neurons,
..GovernanceProto::default()
};

let governance = Governance::new(
governance_proto,
Box::new(MockEnvironment::new(Default::default(), 0)),
Box::new(StubIcpLedger {}),
Box::new(StubCMC {}),
);

let request = ListNeurons {
neuron_ids: vec![],
include_neurons_readable_by_caller: true,
include_empty_neurons_readable_by_caller: Some(false),
include_public_neurons_in_full_neurons: None,
};

bench_fn(|| {
governance.list_neurons(&request, PrincipalId::new_user_test_id(1));
})
}

/// Benchmark list_neurons
#[bench(raw)]
fn list_neurons_stable() -> BenchResult {
let _f = temporarily_enable_active_neurons_in_stable_memory();
list_neurons_benchmark()
}

/// Benchmark list_neurons
#[bench(raw)]
fn list_neurons_heap() -> BenchResult {
let _f = temporarily_disable_active_neurons_in_stable_memory();
list_neurons_benchmark()
}
2 changes: 1 addition & 1 deletion rs/nns/governance/src/neuron_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ impl NeuronStore {
caller: PrincipalId,
) -> Vec<NeuronId> {
let is_non_empty = |neuron_id: &NeuronId| {
self.with_neuron(neuron_id, |neuron| neuron.is_funded())
self.with_neuron_sections(neuron_id, NeuronSections::NONE, |neuron| neuron.is_funded())
.unwrap_or(false)
};

Expand Down

0 comments on commit cf7d6c2

Please sign in to comment.