Skip to content

Commit

Permalink
refactor: add more tests and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-bahjati committed May 16, 2024
1 parent c581da7 commit 9afe6e8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
30 changes: 27 additions & 3 deletions program/c/src/oracle/model/test_price_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ int test_price_model() {
int64_t quote [N];
int64_t val [3];

/* Brute force validate small sizes via the 0-1 principle. */
for( int cnt=0; cnt<=24; cnt++ ) {
for( long mask=0L; mask<(1L<<cnt); mask++ ) {
for( int i=0; i<cnt; i++ ) quote[i] = (int64_t) ((mask>>i) & 1L);

memcpy( quote, quote0, sizeof(int64_t)*(size_t)cnt );
if( price_model_core( cnt, quote, val+0, val+1, val+2)!=quote ) { printf( "FAIL (01-compose)\n" ); return 1; }

/* Validate the results */

/* Although being sorted is not necessary it gives us more confidence about the correctness of the model */
qsort( quote0, (size_t)cnt, sizeof(int64_t), qcmp );
if( memcmp( quote, quote0, sizeof(int64_t)*(size_t)cnt ) ) { printf( "FAIL (01-sort)\n" ); return 1; }

uint64_t p25_idx = cnt>>2;
uint64_t p50_idx = cnt>>1;
uint64_t p75_idx = cnt - (uint64_t)1 - p25_idx;
uint64_t is_even = (uint64_t)!(cnt & (uint64_t)1);

if( val[0]!=quote[ p25_idx ] ) { printf( "FAIL (01-p25)\n" ); return 1; }
if( val[1]!=avg_2_int64( quote[ p50_idx-is_even ], quote[ p50_idx ] ) ) { printf( "FAIL (01-p50)\n" ); return 1; }
if( val[2]!=quote[ p75_idx ] ) { printf( "FAIL (01-p75)\n" ); return 1; }
}
}

/* Test using randomized inputs */
for( int iter=0; iter<10000000; iter++ ) {

/* Generate a random test */
Expand All @@ -39,9 +65,7 @@ int test_price_model() {

/* Validate the results */

/*
* Although being sorted is not necessary it gives us more confidence about the correctness of the model.
*/
/* Although being sorted is not necessary it gives us more confidence about the correctness of the model */
qsort( quote0, (size_t)cnt, sizeof(int64_t), qcmp );
if( memcmp( quote, quote0, sizeof(int64_t)*(size_t)cnt ) ) { printf( "FAIL (sort)\n" ); return 1; }

Expand Down
65 changes: 50 additions & 15 deletions program/rust/src/tests/test_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ use {
},
};

#[derive(Clone, Copy, Debug)]
enum TestingStrategy {
Random,
SimilarPrices,
}

/// Benchmark the execution of the oracle program
async fn run_benchmark(num_publishers: usize) -> Result<(), Box<dyn std::error::Error>> {
async fn run_benchmark(
num_publishers: usize,
strategy: TestingStrategy,
) -> Result<(), Box<dyn std::error::Error>> {
let mut sim = PythSimulator::new().await;

let mapping_keypair = sim.init_mapping().await?;
Expand All @@ -40,14 +49,20 @@ async fn run_benchmark(num_publishers: usize) -> Result<(), Box<dyn std::error::
let mut rnd = rand::rngs::SmallRng::seed_from_u64(14);

for kp in publishers_keypairs.iter() {
// The ranges are chosen to create overlap between
// publishers price set (price-conf, price, price + conf)
let quote = Quote {
price: rnd.gen_range(10000..11000),
confidence: rnd.gen_range(1..1000),
status: PC_STATUS_TRADING,
let quote = match strategy {
TestingStrategy::Random => Quote {
price: rnd.gen_range(10000..11000),
confidence: rnd.gen_range(1..1000),
status: PC_STATUS_TRADING,
},
TestingStrategy::SimilarPrices => Quote {
price: rnd.gen_range(10..12),
confidence: rnd.gen_range(1..3),
status: PC_STATUS_TRADING,
},
};


sim.upd_price(kp, price_pubkey, quote).await?;
}

Expand All @@ -67,21 +82,41 @@ async fn run_benchmark(num_publishers: usize) -> Result<(), Box<dyn std::error::
}

#[tokio::test]
async fn test_benchmark_64_pubs() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(64).await
async fn test_benchmark_64_pubs_random() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(64, TestingStrategy::Random).await
}

#[tokio::test]
async fn test_benchmark_64_pubs_similar_prices() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(64, TestingStrategy::SimilarPrices).await
}

#[tokio::test]
async fn test_benchmark_32_pubs_random() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(32, TestingStrategy::Random).await
}

#[tokio::test]
async fn test_benchmark_32_pubs_similar_prices() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(32, TestingStrategy::SimilarPrices).await
}

#[tokio::test]
async fn test_benchmark_16_pubs_random() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(16, TestingStrategy::Random).await
}

#[tokio::test]
async fn test_benchmark_32_pubs() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(32).await
async fn test_benchmark_16_pubs_similar_prices() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(16, TestingStrategy::SimilarPrices).await
}

#[tokio::test]
async fn test_benchmark_16_pubs() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(16).await
async fn test_benchmark_8_pubs_random() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(8, TestingStrategy::Random).await
}

#[tokio::test]
async fn test_benchmark_8_pubs() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(8).await
async fn test_benchmark_8_pubs_similar_prices() -> Result<(), Box<dyn std::error::Error>> {
run_benchmark(8, TestingStrategy::SimilarPrices).await
}

0 comments on commit 9afe6e8

Please sign in to comment.