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

chore: tests for Lazy and moving out of unstable #1268

Merged
merged 19 commits into from
Dec 5, 2024
38 changes: 28 additions & 10 deletions near-sdk/tests/store_performance_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// As wasm VM performance is tested, there is no need to test this on other types of OS.
// This test runs only on Linux, as it's much slower on OS X due to an interpreted VM.
#![cfg(target_os = "linux")]
// #![cfg(target_os = "linux")]

use near_account_id::AccountId;
use near_gas::NearGas;
Expand All @@ -9,6 +9,7 @@ use near_workspaces::types::{KeyType, SecretKey};
use near_workspaces::{Account, Worker};
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::sync::Arc;
use strum_macros::Display;

Expand All @@ -25,7 +26,6 @@ pub enum Collection {
LookupSet,
TreeMap,
Vector,
LazyOption,
}

pub enum Contract {
Expand Down Expand Up @@ -72,7 +72,7 @@ async fn setup_worker(contract: Contract) -> anyhow::Result<(Arc<Worker<Sandbox>
Ok((worker, contract.id().clone()))
}

fn perform_asserts(total_gas: u64, col: &Collection) {
fn perform_asserts(total_gas: u64, col: impl Display) {
// Constraints a bit relaxed to account for binary differences due to on-demand compilation.
assert!(
total_gas < NearGas::from_tgas(110).as_gas(),
Expand Down Expand Up @@ -136,7 +136,6 @@ async fn insert_and_remove() -> anyhow::Result<()> {
Collection::LookupMap => (col, 650),
Collection::LookupSet => (col, 1020),
Collection::Vector => (col, 1080),
_ => (col, 0),
}) {
let total_gas = account
.call(&contract_id, "insert")
Expand All @@ -162,7 +161,6 @@ async fn insert_and_remove() -> anyhow::Result<()> {
Collection::LookupMap => (col, 520),
Collection::LookupSet => (col, 1050),
Collection::Vector => (col, 530),
_ => (col, 0),
}) {
let total_gas = account
.call(&contract_id, "remove")
Expand Down Expand Up @@ -447,7 +445,7 @@ async fn test_lazy() -> anyhow::Result<()> {
.await?
.unwrap();

perform_asserts(res.total_gas_burnt.as_gas(), &Collection::LazyOption);
perform_asserts(res.total_gas_burnt.as_gas(), "lazy:insert_delete");

let res = account
.call(&contract_id, "insert_delete_flush_once")
Expand All @@ -457,17 +455,27 @@ async fn test_lazy() -> anyhow::Result<()> {
.await?
.unwrap();

perform_asserts(res.total_gas_burnt.as_gas(), &Collection::LazyOption);
perform_asserts(res.total_gas_burnt.as_gas(), "lazy:insert_delete_flush_once");

let res = account
.call(&contract_id, "flush")
.args_json((2400000,))
.args_json((2450000,))
.max_gas()
.transact()
.await?
.unwrap();

perform_asserts(res.total_gas_burnt.as_gas(), "lazy:flush");

let res = account
.call(&contract_id, "get")
.args_json((3500000,))
.max_gas()
.transact()
.await?
.unwrap();

perform_asserts(res.total_gas_burnt.as_gas(), &Collection::LazyOption);
perform_asserts(res.total_gas_burnt.as_gas(), "lazy:get");

let res = account
.call(&contract_id, "insert_flush")
Expand All @@ -477,6 +485,16 @@ async fn test_lazy() -> anyhow::Result<()> {
.await?
.unwrap();

perform_asserts(res.total_gas_burnt.as_gas(), &Collection::LazyOption);
perform_asserts(res.total_gas_burnt.as_gas(), "lazy:insert_flush");

let res = account
.call(&contract_id, "insert_take_flush")
.args_json((700,))
.max_gas()
.transact()
.await?
.unwrap();

perform_asserts(res.total_gas_burnt.as_gas(), "lazy:insert_take_flush");
Ok(())
akorchyn marked this conversation as resolved.
Show resolved Hide resolved
}
22 changes: 22 additions & 0 deletions near-sdk/tests/test-contracts/lazy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ impl LazyContract {
}
}

#[payable]
pub fn get(&mut self, iterations: u32) {
let insertable = self.insertable();
self.lazy_opt.set(Some(insertable));
for _ in 0..=iterations {
self.lazy_opt.get();
}
}

/// This should write on each iteration.
#[payable]
pub fn insert_flush(&mut self, iterations: u32) {
Expand All @@ -49,6 +58,19 @@ impl LazyContract {
}
}

/// This should write twice on each iteration.
#[payable]
pub fn insert_take_flush(&mut self, iterations: u32) {
Copy link
Collaborator

@akorchyn akorchyn Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name it insert_take to be consistent with insert_delete.

As I got confused initially that it is same as insert_delete_flush_once

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

let mut insertable = self.insertable();
for idx in 0..=iterations {
insertable.index = idx as u32;
self.lazy_opt.set(Some(insertable.clone()));
self.lazy_opt.flush();
self.lazy_opt.take();
self.lazy_opt.flush();
}
}

/// This should write and delete on each iteration.
#[payable]
akorchyn marked this conversation as resolved.
Show resolved Hide resolved
pub fn insert_delete(&mut self, iterations: u32) {
Expand Down
Loading