From 1c6fb91fbad180dc9741177b5ccd6f6f5e267716 Mon Sep 17 00:00:00 2001
From: Jason Zhu <jason.zhu@dfinity.org>
Date: Thu, 16 Jan 2025 04:52:04 +0000
Subject: [PATCH 1/2] Add benchmark

---
 .../governance/canbench/canbench_results.yml  |  56 ++++-----
 rs/nns/governance/src/governance/benches.rs   | 106 +++++++++++++++++-
 2 files changed, 133 insertions(+), 29 deletions(-)

diff --git a/rs/nns/governance/canbench/canbench_results.yml b/rs/nns/governance/canbench/canbench_results.yml
index 34b92de91ce..7fc2d61ada9 100644
--- a/rs/nns/governance/canbench/canbench_results.yml
+++ b/rs/nns/governance/canbench/canbench_results.yml
@@ -1,91 +1,91 @@
 benches:
   add_neuron_active_maximum:
     total:
-      instructions: 42470580
+      instructions: 42752805
       heap_increase: 1
       stable_memory_increase: 0
     scopes: {}
   add_neuron_active_typical:
     total:
-      instructions: 2156095
+      instructions: 2170667
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   add_neuron_inactive_maximum:
     total:
-      instructions: 111969854
+      instructions: 112624643
       heap_increase: 1
       stable_memory_increase: 0
     scopes: {}
   add_neuron_inactive_typical:
     total:
-      instructions: 8450397
+      instructions: 8497304
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   cascading_vote_all_heap:
     total:
-      instructions: 34764213
+      instructions: 35002536
       heap_increase: 0
       stable_memory_increase: 128
     scopes: {}
   cascading_vote_heap_neurons_stable_index:
     total:
-      instructions: 60897882
+      instructions: 61137575
       heap_increase: 0
       stable_memory_increase: 128
     scopes: {}
   cascading_vote_stable_everything:
     total:
-      instructions: 188426120
+      instructions: 188621982
       heap_increase: 0
       stable_memory_increase: 128
     scopes: {}
   cascading_vote_stable_neurons_with_heap_index:
     total:
-      instructions: 162159055
+      instructions: 162353547
       heap_increase: 0
       stable_memory_increase: 128
     scopes: {}
   centralized_following_all_stable:
     total:
-      instructions: 78085489
+      instructions: 78268135
       heap_increase: 0
       stable_memory_increase: 128
     scopes: {}
   compute_ballots_for_new_proposal_with_stable_neurons:
     total:
-      instructions: 2152483
+      instructions: 2169168
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   draw_maturity_from_neurons_fund_heap:
     total:
-      instructions: 7455504
+      instructions: 7598030
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   draw_maturity_from_neurons_fund_stable:
     total:
-      instructions: 12275909
+      instructions: 12339498
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   list_active_neurons_fund_neurons_heap:
     total:
-      instructions: 424238
+      instructions: 427763
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   list_active_neurons_fund_neurons_stable:
     total:
-      instructions: 2742494
+      instructions: 2750339
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   list_neurons_heap:
     total:
-      instructions: 4717287
+      instructions: 4763239
       heap_increase: 9
       stable_memory_increase: 0
     scopes: {}
@@ -97,16 +97,22 @@ benches:
     scopes: {}
   list_neurons_ready_to_unstake_maturity_stable:
     total:
-      instructions: 41328019
+      instructions: 41457102
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   list_neurons_stable:
     total:
-      instructions: 113419930
+      instructions: 113374022
       heap_increase: 5
       stable_memory_increase: 0
     scopes: {}
+  list_proposals:
+    total:
+      instructions: 168095717
+      heap_increase: 0
+      stable_memory_increase: 0
+    scopes: {}
   list_ready_to_spawn_neuron_ids_heap:
     total:
       instructions: 132847
@@ -115,49 +121,49 @@ benches:
     scopes: {}
   list_ready_to_spawn_neuron_ids_stable:
     total:
-      instructions: 41299922
+      instructions: 41429005
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   neuron_data_validation_heap:
     total:
-      instructions: 406681837
+      instructions: 406864991
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   neuron_data_validation_stable:
     total:
-      instructions: 362505461
+      instructions: 362661286
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   neuron_metrics_calculation_heap:
     total:
-      instructions: 1471252
+      instructions: 1498269
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   neuron_metrics_calculation_stable:
     total:
-      instructions: 2994858
+      instructions: 3027095
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   range_neurons_performance:
     total:
-      instructions: 56426715
+      instructions: 56448740
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
   single_vote_all_stable:
     total:
-      instructions: 2801395
+      instructions: 2805838
       heap_increase: 0
       stable_memory_increase: 128
     scopes: {}
   update_recent_ballots_stable_memory:
     total:
-      instructions: 273152
+      instructions: 274000
       heap_increase: 0
       stable_memory_increase: 0
     scopes: {}
diff --git a/rs/nns/governance/src/governance/benches.rs b/rs/nns/governance/src/governance/benches.rs
index 723fd5262c4..ed0ed6fa0b4 100644
--- a/rs/nns/governance/src/governance/benches.rs
+++ b/rs/nns/governance/src/governance/benches.rs
@@ -1,11 +1,15 @@
 use crate::{
-    governance::{Governance, MIN_DISSOLVE_DELAY_FOR_VOTE_ELIGIBILITY_SECONDS},
+    governance::{
+        test_data::CREATE_SERVICE_NERVOUS_SYSTEM_WITH_MATCHED_FUNDING, Governance,
+        MIN_DISSOLVE_DELAY_FOR_VOTE_ELIGIBILITY_SECONDS,
+    },
     neuron::{DissolveStateAndAge, Neuron, NeuronBuilder},
     neuron_store::NeuronStore,
     pb::v1::{
-        neuron::Followees, proposal::Action, Ballot, BallotInfo, Governance as GovernanceProto,
-        KnownNeuron, ListNeurons, Neuron as NeuronProto, ProposalData, Topic, Vote,
-        VotingPowerEconomics,
+        install_code::CanisterInstallMode, neuron::Followees, proposal::Action, Ballot, BallotInfo,
+        CreateServiceNervousSystem, ExecuteNnsFunction, Governance as GovernanceProto, InstallCode,
+        KnownNeuron, ListNeurons, ListProposalInfo, NetworkEconomics, Neuron as NeuronProto,
+        NnsFunction, Proposal, ProposalData, Topic, Vote, VotingPowerEconomics,
     },
     temporarily_disable_allow_active_neurons_in_stable_memory,
     temporarily_disable_migrate_active_neurons_to_stable_memory,
@@ -18,10 +22,12 @@ use crate::{
 use canbench_rs::{bench, bench_fn, BenchResult};
 use futures::FutureExt;
 use ic_base_types::PrincipalId;
+use ic_nervous_system_proto::pb::v1::Image;
 use ic_nns_common::{
     pb::v1::{NeuronId as NeuronIdProto, ProposalId},
     types::NeuronId,
 };
+use ic_nns_constants::GOVERNANCE_CANISTER_ID;
 use icp_ledger::Subaccount;
 use maplit::hashmap;
 use rand::{Rng, SeedableRng};
@@ -579,3 +585,95 @@ fn list_neurons_heap() -> BenchResult {
     let _b = temporarily_disable_migrate_active_neurons_to_stable_memory();
     list_neurons_benchmark()
 }
+
+fn create_service_nervous_system_action_with_large_payload() -> CreateServiceNervousSystem {
+    let mut action = CREATE_SERVICE_NERVOUS_SYSTEM_WITH_MATCHED_FUNDING.clone();
+
+    let large_image = Some(Image {
+        base64_encoding: Some(format!("data:image/png;base64,{}", "A".repeat(1 << 18))), // 256 KiB
+    });
+
+    action.logo = large_image.clone();
+    action.ledger_parameters.as_mut().unwrap().token_logo = large_image;
+
+    action
+}
+
+fn list_proposals_benchmark() -> BenchResult {
+    let neurons = (1..=100)
+        .map(|id| {
+            (id, {
+                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)
+            })
+        })
+        .collect::<BTreeMap<u64, NeuronProto>>();
+
+    let governance_proto = GovernanceProto {
+        neurons,
+        economics: Some(NetworkEconomics::with_default_values()),
+        ..Default::default()
+    };
+
+    let mut governance = Governance::new(
+        governance_proto,
+        Box::new(MockEnvironment::new(Default::default(), 0)),
+        Box::new(StubIcpLedger {}),
+        Box::new(StubCMC {}),
+    );
+
+    let request = ListProposalInfo {
+        limit: 100,
+        omit_large_fields: Some(true),
+        ..Default::default()
+    };
+
+    let proposal_actions = vec![
+        Action::ExecuteNnsFunction(ExecuteNnsFunction {
+            nns_function: NnsFunction::NnsCanisterUpgrade as i32,
+            payload: vec![0u8; 1 << 20], // 1 MiB
+        }),
+        Action::InstallCode(InstallCode {
+            canister_id: Some(GOVERNANCE_CANISTER_ID.get()),
+            wasm_module: Some(vec![0u8; 1 << 20]), // 1 MiB
+            arg: Some(vec![0u8; 1 << 20]),         // 1 MiB
+            install_mode: Some(CanisterInstallMode::Install as i32),
+            skip_stopping_before_installing: None,
+        }),
+        Action::CreateServiceNervousSystem(
+            create_service_nervous_system_action_with_large_payload(),
+        ),
+    ];
+
+    for proposal_action in proposal_actions {
+        governance
+            .make_proposal(
+                &NeuronIdProto { id: 1 },
+                &PrincipalId::new_user_test_id(1),
+                &Proposal {
+                    summary: "Summary".to_string(),
+                    url: "".to_string(),
+                    title: Some("Title".to_string()),
+                    action: Some(proposal_action),
+                },
+            )
+            .now_or_never()
+            .expect("Failed to await for making proposal")
+            .expect("Failed to make proposal");
+    }
+
+    bench_fn(|| {
+        let response = governance.list_proposals(&PrincipalId::new_anonymous(), &request);
+        ic_nns_governance_api::pb::v1::ListProposalInfoResponse::from(response);
+    })
+}
+
+#[bench(raw)]
+fn list_proposals() -> BenchResult {
+    list_proposals_benchmark()
+}

From 28ca3f5a28eb6a92bf0a012975047defb02efccd Mon Sep 17 00:00:00 2001
From: Jason Zhu <jason.zhu@dfinity.org>
Date: Tue, 21 Jan 2025 21:25:13 +0000
Subject: [PATCH 2/2] Clippy

---
 rs/nns/governance/src/governance/benches.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rs/nns/governance/src/governance/benches.rs b/rs/nns/governance/src/governance/benches.rs
index ed0ed6fa0b4..9952e9fee1a 100644
--- a/rs/nns/governance/src/governance/benches.rs
+++ b/rs/nns/governance/src/governance/benches.rs
@@ -669,7 +669,7 @@ fn list_proposals_benchmark() -> BenchResult {
 
     bench_fn(|| {
         let response = governance.list_proposals(&PrincipalId::new_anonymous(), &request);
-        ic_nns_governance_api::pb::v1::ListProposalInfoResponse::from(response);
+        let _ = ic_nns_governance_api::pb::v1::ListProposalInfoResponse::from(response);
     })
 }