Skip to content

Commit

Permalink
More cost snapshot tests (#4449)
Browse files Browse the repository at this point in the history
* improved cost snapshot tests
  • Loading branch information
oxade authored Sep 2, 2022
1 parent 29dd195 commit ed4258a
Show file tree
Hide file tree
Showing 8 changed files with 3,007 additions and 82 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions crates/sui-cost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ tokio = { version = "1.20.1", features = ["full"] }
serde = { version = "1.0.144", features = ["derive"] }
strum = { version = "0.24", features = ["derive"] }
strum_macros = "0.24"
toml = "0.5.9"
bcs = "0.1.3"
serde_json = "1.0.83"

[dev-dependencies]
insta = { version = "1.17.1", features = ["redactions"] }
insta = { version = "1.17.1", features = ["redactions", "json"] }
test-utils = { path = "../test-utils" }
sui-config = {path = "../sui-config"}
sui-config = {path = "../sui-config"}
move-cli = { git = "https://github.com/move-language/move", rev = "70b34a66473c34ad30d101290b249f2db3c847a2" }
move-disassembler = { git = "https://github.com/move-language/move", rev = "70b34a66473c34ad30d101290b249f2db3c847a2" }
move-package = { git = "https://github.com/move-language/move", rev = "70b34a66473c34ad30d101290b249f2db3c847a2" }
15 changes: 12 additions & 3 deletions crates/sui-cost/src/estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sui_types::gas::GasCostSummary;
use sui_types::messages::SingleTransactionKind;
use sui_types::messages::TransactionKind;

pub const ESTIMATE_FILE: &str = "data/estimate.toml";
pub const ESTIMATE_FILE: &str = "tests/snapshots/empirical_transaction_cost__good_snapshot.snap";
#[derive(
Debug, Eq, PartialEq, Hash, Serialize, Deserialize, Ord, PartialOrd, Clone, Display, EnumString,
)]
Expand Down Expand Up @@ -54,9 +54,18 @@ pub fn estimate_computational_costs_for_transaction(

pub fn read_estimate_file(
) -> Result<BTreeMap<CommonTransactionCosts, GasCostSummary>, anyhow::Error> {
let toml_str = fs::read_to_string(&ESTIMATE_FILE).unwrap();
let json_str = fs::read_to_string(&ESTIMATE_FILE).unwrap();

// Remove the metadata: first 4 lines form snapshot tests
let json_str = json_str
.split('\n')
.skip(4)
.map(|q| q.to_string())
.collect::<Vec<String>>()
.join("\n");

let cost_map: BTreeMap<String, GasCostSummary> = serde_json::from_str(&json_str).unwrap();

let cost_map: BTreeMap<String, GasCostSummary> = toml::from_str(&toml_str).unwrap();
let cost_map: BTreeMap<CommonTransactionCosts, GasCostSummary> = cost_map
.iter()
.map(|(k, v)| (CommonTransactionCosts::from_str(k).unwrap(), v.clone()))
Expand Down
63 changes: 63 additions & 0 deletions crates/sui-cost/tests/calibration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::path::PathBuf;

use insta::assert_snapshot;
use move_cli::base::reroot_path;
use move_disassembler::disassembler::Disassembler;
use move_package::BuildConfig;

const TEST_MODULE_DATA_DIR: &str = "../sui-framework/tests";

// Execute every entry function in Move framework and examples and ensure costs don't change
// To review snapshot changes, and fix snapshot differences,
// 0. Install cargo-insta
// 1. Run `cargo insta test --review` under `./sui-cost`.
// 2. Review, accept or reject changes.

#[tokio::test]
async fn test_natives_disassemble_snapshot() -> Result<(), anyhow::Error> {
let natives_calib = disassemble_test_module("natives_calibration_tests".to_string())?;

// Assert that nothing has changed in the disassemby of the code
assert_snapshot!(natives_calib);
Ok(())
}

#[tokio::test]
async fn test_bytecode_disassemble_snapshot() -> Result<(), anyhow::Error> {
let bytecode_calib = disassemble_test_module("bytecode_calibration_tests".to_string())?;

// Assert that nothing has changed in the disassemby of the code
assert_snapshot!(bytecode_calib);
Ok(())
}

fn disassemble_test_module(name: String) -> anyhow::Result<String> {
let path = PathBuf::from(TEST_MODULE_DATA_DIR);
let config = BuildConfig {
test_mode: true,
..Default::default()
};
let package_name: Option<String> = None;
let module_name = name;

let rerooted_path = reroot_path(Some(path))?;

let package = config.compile_package(&rerooted_path, &mut Vec::new())?;
let needle_package = package_name
.as_deref()
.unwrap_or(package.compiled_package_info.package_name.as_str());
match package
.get_module_by_name(needle_package, &module_name)
.ok()
{
None => anyhow::bail!(
"Unable to find module or script with name '{}' in package '{}'",
module_name,
needle_package,
),
Some(unit) => Disassembler::from_unit(&unit.unit).disassemble(),
}
}
34 changes: 8 additions & 26 deletions crates/sui-cost/tests/empirical_transaction_cost.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use insta::assert_yaml_snapshot;
use std::fs;
use insta::assert_json_snapshot;
use std::{collections::BTreeMap, path::PathBuf};
use sui_config::ValidatorInfo;
use sui_cost::estimator::ESTIMATE_FILE;
use sui_cost::estimator::{
estimate_computational_costs_for_transaction, read_estimate_file, CommonTransactionCosts,
};
Expand Down Expand Up @@ -43,16 +41,17 @@ const TEST_DATA_DIR: &str = "tests/data/";

#[tokio::test]
async fn test_good_snapshot() -> Result<(), anyhow::Error> {
let common_costs = run_common_tx_costs().await?;
assert_yaml_snapshot!(common_costs);
let common_costs: BTreeMap<String, GasCostSummary> = run_common_tx_costs()
.await?
.iter()
.map(|q| (q.0.to_string(), q.1.clone()))
.collect();
assert_json_snapshot!(common_costs);
Ok(())
}

#[tokio::test]
async fn check_estimates() {
// Generate the estimates to file
generate_estimates().await.unwrap();

// Read the estimates
let cost_map = read_estimate_file().unwrap();

Expand Down Expand Up @@ -96,26 +95,9 @@ async fn check_estimates() {
);
}

async fn generate_estimates() -> Result<(), anyhow::Error> {
let common_costs: BTreeMap<_, _> = run_common_tx_costs()
.await?
.iter()
.map(|(k, v)| (format!("{k}"), v.clone()))
.collect();

let out_string = toml::to_string(&common_costs).unwrap();

fs::write(ESTIMATE_FILE, out_string).expect("Could not write estimator to file!");
Ok(())
}

pub async fn run_common_tx_costs(
) -> Result<BTreeMap<CommonTransactionCosts, GasCostSummary>, anyhow::Error> {
Ok(run_counter_costs().await)
}

pub async fn run_counter_costs() -> BTreeMap<CommonTransactionCosts, GasCostSummary> {
run_cost_test().await.into_iter().collect()
Ok(run_cost_test().await)
}

async fn run_cost_test() -> BTreeMap<CommonTransactionCosts, GasCostSummary> {
Expand Down
Loading

0 comments on commit ed4258a

Please sign in to comment.