Skip to content

Commit 744f468

Browse files
perf(nns): Add a benchmark for listing proposals (dfinity#3489)
Add a benchmark for listing proposals before doing any optimizations
1 parent 9e2784a commit 744f468

File tree

2 files changed

+133
-29
lines changed

2 files changed

+133
-29
lines changed

rs/nns/governance/canbench/canbench_results.yml

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,91 @@
11
benches:
22
add_neuron_active_maximum:
33
total:
4-
instructions: 42470580
4+
instructions: 42752805
55
heap_increase: 1
66
stable_memory_increase: 0
77
scopes: {}
88
add_neuron_active_typical:
99
total:
10-
instructions: 2156095
10+
instructions: 2170667
1111
heap_increase: 0
1212
stable_memory_increase: 0
1313
scopes: {}
1414
add_neuron_inactive_maximum:
1515
total:
16-
instructions: 111969854
16+
instructions: 112624643
1717
heap_increase: 1
1818
stable_memory_increase: 0
1919
scopes: {}
2020
add_neuron_inactive_typical:
2121
total:
22-
instructions: 8450397
22+
instructions: 8497304
2323
heap_increase: 0
2424
stable_memory_increase: 0
2525
scopes: {}
2626
cascading_vote_all_heap:
2727
total:
28-
instructions: 34764213
28+
instructions: 35002536
2929
heap_increase: 0
3030
stable_memory_increase: 128
3131
scopes: {}
3232
cascading_vote_heap_neurons_stable_index:
3333
total:
34-
instructions: 60897882
34+
instructions: 61137575
3535
heap_increase: 0
3636
stable_memory_increase: 128
3737
scopes: {}
3838
cascading_vote_stable_everything:
3939
total:
40-
instructions: 188426120
40+
instructions: 188621982
4141
heap_increase: 0
4242
stable_memory_increase: 128
4343
scopes: {}
4444
cascading_vote_stable_neurons_with_heap_index:
4545
total:
46-
instructions: 162159055
46+
instructions: 162353547
4747
heap_increase: 0
4848
stable_memory_increase: 128
4949
scopes: {}
5050
centralized_following_all_stable:
5151
total:
52-
instructions: 78085489
52+
instructions: 78268135
5353
heap_increase: 0
5454
stable_memory_increase: 128
5555
scopes: {}
5656
compute_ballots_for_new_proposal_with_stable_neurons:
5757
total:
58-
instructions: 2152483
58+
instructions: 2169168
5959
heap_increase: 0
6060
stable_memory_increase: 0
6161
scopes: {}
6262
draw_maturity_from_neurons_fund_heap:
6363
total:
64-
instructions: 7455504
64+
instructions: 7598030
6565
heap_increase: 0
6666
stable_memory_increase: 0
6767
scopes: {}
6868
draw_maturity_from_neurons_fund_stable:
6969
total:
70-
instructions: 12275909
70+
instructions: 12339498
7171
heap_increase: 0
7272
stable_memory_increase: 0
7373
scopes: {}
7474
list_active_neurons_fund_neurons_heap:
7575
total:
76-
instructions: 424238
76+
instructions: 427763
7777
heap_increase: 0
7878
stable_memory_increase: 0
7979
scopes: {}
8080
list_active_neurons_fund_neurons_stable:
8181
total:
82-
instructions: 2742494
82+
instructions: 2750339
8383
heap_increase: 0
8484
stable_memory_increase: 0
8585
scopes: {}
8686
list_neurons_heap:
8787
total:
88-
instructions: 4717287
88+
instructions: 4763239
8989
heap_increase: 9
9090
stable_memory_increase: 0
9191
scopes: {}
@@ -97,16 +97,22 @@ benches:
9797
scopes: {}
9898
list_neurons_ready_to_unstake_maturity_stable:
9999
total:
100-
instructions: 41328019
100+
instructions: 41457102
101101
heap_increase: 0
102102
stable_memory_increase: 0
103103
scopes: {}
104104
list_neurons_stable:
105105
total:
106-
instructions: 113419930
106+
instructions: 113374022
107107
heap_increase: 5
108108
stable_memory_increase: 0
109109
scopes: {}
110+
list_proposals:
111+
total:
112+
instructions: 168095717
113+
heap_increase: 0
114+
stable_memory_increase: 0
115+
scopes: {}
110116
list_ready_to_spawn_neuron_ids_heap:
111117
total:
112118
instructions: 132847
@@ -115,49 +121,49 @@ benches:
115121
scopes: {}
116122
list_ready_to_spawn_neuron_ids_stable:
117123
total:
118-
instructions: 41299922
124+
instructions: 41429005
119125
heap_increase: 0
120126
stable_memory_increase: 0
121127
scopes: {}
122128
neuron_data_validation_heap:
123129
total:
124-
instructions: 406681837
130+
instructions: 406864991
125131
heap_increase: 0
126132
stable_memory_increase: 0
127133
scopes: {}
128134
neuron_data_validation_stable:
129135
total:
130-
instructions: 362505461
136+
instructions: 362661286
131137
heap_increase: 0
132138
stable_memory_increase: 0
133139
scopes: {}
134140
neuron_metrics_calculation_heap:
135141
total:
136-
instructions: 1471252
142+
instructions: 1498269
137143
heap_increase: 0
138144
stable_memory_increase: 0
139145
scopes: {}
140146
neuron_metrics_calculation_stable:
141147
total:
142-
instructions: 2994858
148+
instructions: 3027095
143149
heap_increase: 0
144150
stable_memory_increase: 0
145151
scopes: {}
146152
range_neurons_performance:
147153
total:
148-
instructions: 56426715
154+
instructions: 56448740
149155
heap_increase: 0
150156
stable_memory_increase: 0
151157
scopes: {}
152158
single_vote_all_stable:
153159
total:
154-
instructions: 2801395
160+
instructions: 2805838
155161
heap_increase: 0
156162
stable_memory_increase: 128
157163
scopes: {}
158164
update_recent_ballots_stable_memory:
159165
total:
160-
instructions: 273152
166+
instructions: 274000
161167
heap_increase: 0
162168
stable_memory_increase: 0
163169
scopes: {}

rs/nns/governance/src/governance/benches.rs

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use crate::{
2-
governance::{Governance, MIN_DISSOLVE_DELAY_FOR_VOTE_ELIGIBILITY_SECONDS},
2+
governance::{
3+
test_data::CREATE_SERVICE_NERVOUS_SYSTEM_WITH_MATCHED_FUNDING, Governance,
4+
MIN_DISSOLVE_DELAY_FOR_VOTE_ELIGIBILITY_SECONDS,
5+
},
36
neuron::{DissolveStateAndAge, Neuron, NeuronBuilder},
47
neuron_store::NeuronStore,
58
pb::v1::{
6-
neuron::Followees, proposal::Action, Ballot, BallotInfo, Governance as GovernanceProto,
7-
KnownNeuron, ListNeurons, Neuron as NeuronProto, ProposalData, Topic, Vote,
8-
VotingPowerEconomics,
9+
install_code::CanisterInstallMode, neuron::Followees, proposal::Action, Ballot, BallotInfo,
10+
CreateServiceNervousSystem, ExecuteNnsFunction, Governance as GovernanceProto, InstallCode,
11+
KnownNeuron, ListNeurons, ListProposalInfo, NetworkEconomics, Neuron as NeuronProto,
12+
NnsFunction, Proposal, ProposalData, Topic, Vote, VotingPowerEconomics,
913
},
1014
temporarily_disable_allow_active_neurons_in_stable_memory,
1115
temporarily_disable_migrate_active_neurons_to_stable_memory,
@@ -18,10 +22,12 @@ use crate::{
1822
use canbench_rs::{bench, bench_fn, BenchResult};
1923
use futures::FutureExt;
2024
use ic_base_types::PrincipalId;
25+
use ic_nervous_system_proto::pb::v1::Image;
2126
use ic_nns_common::{
2227
pb::v1::{NeuronId as NeuronIdProto, ProposalId},
2328
types::NeuronId,
2429
};
30+
use ic_nns_constants::GOVERNANCE_CANISTER_ID;
2531
use icp_ledger::Subaccount;
2632
use maplit::hashmap;
2733
use rand::{Rng, SeedableRng};
@@ -579,3 +585,95 @@ fn list_neurons_heap() -> BenchResult {
579585
let _b = temporarily_disable_migrate_active_neurons_to_stable_memory();
580586
list_neurons_benchmark()
581587
}
588+
589+
fn create_service_nervous_system_action_with_large_payload() -> CreateServiceNervousSystem {
590+
let mut action = CREATE_SERVICE_NERVOUS_SYSTEM_WITH_MATCHED_FUNDING.clone();
591+
592+
let large_image = Some(Image {
593+
base64_encoding: Some(format!("data:image/png;base64,{}", "A".repeat(1 << 18))), // 256 KiB
594+
});
595+
596+
action.logo = large_image.clone();
597+
action.ledger_parameters.as_mut().unwrap().token_logo = large_image;
598+
599+
action
600+
}
601+
602+
fn list_proposals_benchmark() -> BenchResult {
603+
let neurons = (1..=100)
604+
.map(|id| {
605+
(id, {
606+
make_neuron(
607+
id,
608+
PrincipalId::new_user_test_id(id),
609+
1_000_000_000,
610+
hashmap! {}, // get the default followees
611+
)
612+
.into_proto(&VotingPowerEconomics::DEFAULT, 123_456_789)
613+
})
614+
})
615+
.collect::<BTreeMap<u64, NeuronProto>>();
616+
617+
let governance_proto = GovernanceProto {
618+
neurons,
619+
economics: Some(NetworkEconomics::with_default_values()),
620+
..Default::default()
621+
};
622+
623+
let mut governance = Governance::new(
624+
governance_proto,
625+
Box::new(MockEnvironment::new(Default::default(), 0)),
626+
Box::new(StubIcpLedger {}),
627+
Box::new(StubCMC {}),
628+
);
629+
630+
let request = ListProposalInfo {
631+
limit: 100,
632+
omit_large_fields: Some(true),
633+
..Default::default()
634+
};
635+
636+
let proposal_actions = vec![
637+
Action::ExecuteNnsFunction(ExecuteNnsFunction {
638+
nns_function: NnsFunction::NnsCanisterUpgrade as i32,
639+
payload: vec![0u8; 1 << 20], // 1 MiB
640+
}),
641+
Action::InstallCode(InstallCode {
642+
canister_id: Some(GOVERNANCE_CANISTER_ID.get()),
643+
wasm_module: Some(vec![0u8; 1 << 20]), // 1 MiB
644+
arg: Some(vec![0u8; 1 << 20]), // 1 MiB
645+
install_mode: Some(CanisterInstallMode::Install as i32),
646+
skip_stopping_before_installing: None,
647+
}),
648+
Action::CreateServiceNervousSystem(
649+
create_service_nervous_system_action_with_large_payload(),
650+
),
651+
];
652+
653+
for proposal_action in proposal_actions {
654+
governance
655+
.make_proposal(
656+
&NeuronIdProto { id: 1 },
657+
&PrincipalId::new_user_test_id(1),
658+
&Proposal {
659+
summary: "Summary".to_string(),
660+
url: "".to_string(),
661+
title: Some("Title".to_string()),
662+
action: Some(proposal_action),
663+
},
664+
)
665+
.now_or_never()
666+
.expect("Failed to await for making proposal")
667+
.expect("Failed to make proposal");
668+
}
669+
670+
bench_fn(|| {
671+
let response = governance.list_proposals(&PrincipalId::new_anonymous(), &request);
672+
let _ = ic_nns_governance_api::pb::v1::ListProposalInfoResponse::from(response);
673+
})
674+
}
675+
676+
#[bench(raw)]
677+
fn list_proposals() -> BenchResult {
678+
list_proposals_benchmark()
679+
}

0 commit comments

Comments
 (0)