Skip to content

Commit

Permalink
Better measure with ranges and more info (#43)
Browse files Browse the repository at this point in the history
* test

* test push

* sequential

* measure with multiplier

* more structured data

* prettier output
  • Loading branch information
VladasZ authored Oct 17, 2023
1 parent e453cbd commit ba88d0a
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 339 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/measure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ on:
number_of_jars:
description: 'Number of maximum user jars'
required: false
default: '20'
chunk_size:
description: 'Number of concurrent tests'
default: '5'
multiplier:
description: 'Jars multiplier'
required: false
default: '5'

jobs:
build:
measure:
runs-on: ubuntu-latest

env:
MEASURE_JARS_COUNT: ${{ github.event.inputs.number_of_jars }}
MEASURE_CHUNK_SIZE: ${{ github.event.inputs.chunk_size }}
MEASURE_JARS_MULTIPLIER: ${{ github.event.inputs.multiplier }}

steps:
- name: Checkout
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,47 @@ jobs:

- name: Integration tests
run: make integration

measure:
needs: [ build, lint ]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Measure gas
run: make measure

- name: Upload measure
uses: actions/upload-artifact@v3
with:
name: measured
path: measured.txt

push:
needs: [ unit-tests, integration-tests, measure ]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo

- name: Download binary
uses: actions/download-artifact@v3
with:
name: sweat-jar
path: res/

- name: Download measure
uses: actions/download-artifact@v3
with:
name: measured

- name: Commit & Push changes
uses: actions-js/push@master
with:
message: Updated binary and measure
branch: measure/test
github_token: ${{ secrets.ACTIONS_TOKEN }}
11 changes: 5 additions & 6 deletions integration-tests/src/measure/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use std::collections::HashMap;

use anyhow::Result;
use itertools::Itertools;
use workspaces::types::Gas;

use crate::measure::utils::{number_of_jars_to_measure, MeasureData};
use crate::measure::utils::{measure_jars_range, MeasureData};
use crate::{
common::{prepare_contract, Prepared},
measure::{
Expand All @@ -28,24 +27,24 @@ async fn measure_claim_total_test() -> Result<()> {
RegisterProductCommand::Locked10Minutes6PercentsWithFixedWithdrawFee,
RegisterProductCommand::Locked10Minutes6PercentsWithPercentWithdrawFee,
],
&(1..number_of_jars_to_measure()).collect_vec(),
&measure_jars_range(),
),
measure_claim,
)
.await?;

let mut map: HashMap<RegisterProductCommand, Vec<Gas>> = HashMap::new();
let mut map: HashMap<RegisterProductCommand, Vec<(Gas, usize)>> = HashMap::new();

for measure in measured {
map.entry(measure.0 .0).or_default().push(measure.1);
map.entry(measure.0 .0).or_default().push((measure.1, measure.0 .1));
}

let map: HashMap<RegisterProductCommand, _> = map
.into_iter()
.map(|(key, gas_cost)| {
let mut differences: Vec<i128> = Vec::new();
for i in 1..gas_cost.len() {
let diff = gas_cost[i] as i128 - gas_cost[i - 1] as i128;
let diff = gas_cost[i].0 as i128 - gas_cost[i - 1].0 as i128;
differences.push(diff);
}

Expand Down
23 changes: 11 additions & 12 deletions integration-tests/src/measure/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use itertools::Itertools;
use tokio::spawn;
use workspaces::types::Gas;

use crate::measure::utils::measure_chunk_size;
use crate::{measure::register_product::measure_register_product, product::RegisterProductCommand};

#[ignore]
Expand All @@ -33,9 +32,9 @@ where
let inputs = inputs.into_iter().collect_vec();

// async concurrent execution
// let all = inputs.iter().map(|inp| command(*inp)).collect_vec();
//
// let res: Vec<_> = join_all(all).await.into_iter().collect::<anyhow::Result<_>>()?;
let all = inputs.iter().map(|inp| command(*inp)).collect_vec();

let res: Vec<_> = join_all(all).await.into_iter().collect::<anyhow::Result<_>>()?;

// sequential execution
// let mut res = vec![];
Expand All @@ -45,14 +44,14 @@ where
// }

// Too many concurrent jobs may overwhelm workspaces test framework
let chunks = inputs.iter().map(|inp| command(*inp)).chunks(measure_chunk_size());

let mut res = vec![];

for chunk in &chunks {
let chunk_result: Vec<_> = join_all(chunk).await.into_iter().collect::<anyhow::Result<_>>()?;
res.extend(chunk_result);
}
// let chunks = inputs.iter().map(|inp| command(*inp)).chunks(measure_chunk_size());
//
// let mut res = vec![];
//
// for chunk in &chunks {
// let chunk_result: Vec<_> = join_all(chunk).await.into_iter().collect::<anyhow::Result<_>>()?;
// res.extend(chunk_result);
// }

let res = inputs.into_iter().zip(res.into_iter()).collect_vec();

Expand Down
11 changes: 5 additions & 6 deletions integration-tests/src/measure/restake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use std::collections::HashMap;

use anyhow::Result;
use itertools::Itertools;
use workspaces::types::Gas;

use crate::measure::utils::{number_of_jars_to_measure, MeasureData};
use crate::measure::utils::{measure_jars_range, MeasureData};
use crate::{
common::{prepare_contract, Prepared},
measure::{
Expand All @@ -25,24 +24,24 @@ async fn measure_restake_total_test() -> Result<()> {
let measured = scoped_command_measure(
generate_permutations(
&[RegisterProductCommand::Locked10Minutes6Percents],
&(1..number_of_jars_to_measure()).collect_vec(),
&measure_jars_range(),
),
measure_restake,
)
.await?;

let mut map: HashMap<RegisterProductCommand, Vec<Gas>> = HashMap::new();
let mut map: HashMap<RegisterProductCommand, Vec<(Gas, usize)>> = HashMap::new();

for measure in measured {
map.entry(measure.0 .0).or_default().push(measure.1);
map.entry(measure.0 .0).or_default().push((measure.1, measure.0 .1));
}

let map: HashMap<RegisterProductCommand, _> = map
.into_iter()
.map(|(key, gas_cost)| {
let mut differences: Vec<i128> = Vec::new();
for i in 1..gas_cost.len() {
let diff = gas_cost[i] as i128 - gas_cost[i - 1] as i128;
let diff = gas_cost[i].0 as i128 - gas_cost[i - 1].0 as i128;
differences.push(diff);
}

Expand Down
11 changes: 5 additions & 6 deletions integration-tests/src/measure/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use std::collections::HashMap;

use anyhow::Result;
use itertools::Itertools;
use workspaces::types::Gas;

use crate::measure::utils::{number_of_jars_to_measure, MeasureData};
use crate::measure::utils::{measure_jars_range, MeasureData};
use crate::{
common::{prepare_contract, Prepared},
measure::{
Expand All @@ -32,24 +31,24 @@ async fn measure_stake_total_test() -> Result<()> {
Flexible6Months6Percents,
Locked6Months6PercentsWithWithdrawFee,
],
&(1..number_of_jars_to_measure()).collect_vec(),
&measure_jars_range(),
),
measure_stake,
)
.await?;

let mut map: HashMap<RegisterProductCommand, Vec<Gas>> = HashMap::new();
let mut map: HashMap<RegisterProductCommand, Vec<(Gas, usize)>> = HashMap::new();

for measure in measured {
map.entry(measure.0 .0).or_default().push(measure.1);
map.entry(measure.0 .0).or_default().push((measure.1, measure.0 .1));
}

let map: HashMap<RegisterProductCommand, _> = map
.into_iter()
.map(|(key, gas_cost)| {
let mut differences: Vec<i128> = Vec::new();
for i in 1..gas_cost.len() {
let diff = gas_cost[i] as i128 - gas_cost[i - 1] as i128;
let diff = gas_cost[i].0 as i128 - gas_cost[i - 1].0 as i128;
differences.push(diff);
}

Expand Down
54 changes: 40 additions & 14 deletions integration-tests/src/measure/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env::var;
use std::iter::once;
use std::{fs::OpenOptions, future::Future, io::Write};

use anyhow::{bail, Result};
Expand All @@ -9,28 +10,53 @@ use workspaces::{types::Gas, Account};

use crate::{context::Context, product::RegisterProductCommand};

pub fn number_of_jars_to_measure() -> usize {
var("MEASURE_JARS_COUNT").map(|val| val.parse().unwrap()).unwrap_or(20)
const MEASURE_JARS_COUNT: usize = 5;
const MEASURE_JARS_MULTIPLIER: usize = 10;

fn number_of_jars_to_measure() -> usize {
var("MEASURE_JARS_COUNT")
.map(|val| val.parse().unwrap_or(MEASURE_JARS_COUNT))
.unwrap_or(MEASURE_JARS_COUNT)
}

fn measure_jars_multiplier() -> usize {
var("MEASURE_JARS_MULTIPLIER")
.map(|val| val.parse().unwrap_or(MEASURE_JARS_MULTIPLIER))
.unwrap_or(MEASURE_JARS_MULTIPLIER)
}

pub fn measure_chunk_size() -> usize {
var("MEASURE_CHUNK_SIZE").map(|val| val.parse().unwrap()).unwrap_or(5)
pub fn measure_jars_range() -> Vec<usize> {
let vals = (1..=number_of_jars_to_measure()).map(|i| i * measure_jars_multiplier() + 1);

once(1).chain(vals).collect()
}

#[derive(Serialize)]
#[serde(crate = "near_sdk::serde")]
pub struct MeasureData {
total: u64,
pub cost: Vec<Gas>,
pub diff: Vec<i128>,
cost: Vec<String>,
diff: Vec<String>,
}

impl MeasureData {
pub fn new(cost: Vec<Gas>, diff: Vec<i128>) -> Self {
pub fn new(cost: Vec<(Gas, usize)>, diff: Vec<i128>) -> Self {
MeasureData {
total: cost.iter().sum(),
cost,
diff,
total: cost.iter().map(|a| a.0).sum(),
cost: cost
.into_iter()
.map(|a| format!(" {} - number of jars: {} ", format_number(a.0), a.1))
.collect(),
diff: diff
.into_iter()
.map(|a| {
format!(
" {} - jar cost: {} ",
format_number(a),
format_number(a / measure_jars_multiplier() as i128)
)
})
.collect(),
}
}
}
Expand Down Expand Up @@ -67,9 +93,9 @@ fn format_numbers(json_obj: &Value) -> Value {
match json_obj {
Value::Number(n) => {
if let Some(n) = n.as_i64() {
Value::String(format_number(&n))
Value::String(format_number(n))
} else if let Some(n) = n.as_u64() {
Value::String(format_number(&n))
Value::String(format_number(n))
} else {
json_obj.clone()
}
Expand All @@ -89,11 +115,11 @@ fn format_numbers(json_obj: &Value) -> Value {
}
}

fn format_number<T: num_format::ToFormattedStr>(number: &T) -> String {
fn format_number<T: num_format::ToFormattedStr>(number: T) -> String {
let format = CustomFormat::builder().separator(" ").build().unwrap();

let mut buf = Buffer::new();
buf.write_formatted(number, &format);
buf.write_formatted(&number, &format);

buf.to_string()
}
Expand Down
11 changes: 5 additions & 6 deletions integration-tests/src/measure/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
use std::collections::HashMap;

use anyhow::Result;
use itertools::Itertools;
use workspaces::types::Gas;

use crate::measure::utils::{number_of_jars_to_measure, MeasureData};
use crate::measure::utils::{measure_jars_range, MeasureData};
use crate::{
common::{prepare_contract, Prepared},
measure::{
Expand All @@ -29,24 +28,24 @@ async fn measure_withdraw_total_test() -> Result<()> {
RegisterProductCommand::Locked10Minutes6PercentsWithFixedWithdrawFee,
RegisterProductCommand::Locked10Minutes6PercentsWithPercentWithdrawFee,
],
&(1..number_of_jars_to_measure()).collect_vec(),
&measure_jars_range(),
),
measure_withdraw,
)
.await?;

let mut map: HashMap<RegisterProductCommand, Vec<Gas>> = HashMap::new();
let mut map: HashMap<RegisterProductCommand, Vec<(Gas, usize)>> = HashMap::new();

for measure in measured {
map.entry(measure.0 .0).or_default().push(measure.1);
map.entry(measure.0 .0).or_default().push((measure.1, measure.0 .1));
}

let map: HashMap<RegisterProductCommand, _> = map
.into_iter()
.map(|(key, gas_cost)| {
let mut differences: Vec<i128> = Vec::new();
for i in 1..gas_cost.len() {
let diff = gas_cost[i] as i128 - gas_cost[i - 1] as i128;
let diff = gas_cost[i].0 as i128 - gas_cost[i - 1].0 as i128;
differences.push(diff);
}

Expand Down
Loading

0 comments on commit ba88d0a

Please sign in to comment.