diff --git a/program/c/src/oracle/model/test_price_model.c b/program/c/src/oracle/model/test_price_model.c index 5a715f22..5c98843b 100644 --- a/program/c/src/oracle/model/test_price_model.c +++ b/program/c/src/oracle/model/test_price_model.c @@ -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<>i) & 1; + + 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 */ @@ -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; } diff --git a/program/rust/src/tests/test_benchmark.rs b/program/rust/src/tests/test_benchmark.rs index 9bcf90ac..fbd1c7f6 100644 --- a/program/rust/src/tests/test_benchmark.rs +++ b/program/rust/src/tests/test_benchmark.rs @@ -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> { +async fn run_benchmark( + num_publishers: usize, + strategy: TestingStrategy, +) -> Result<(), Box> { let mut sim = PythSimulator::new().await; let mapping_keypair = sim.init_mapping().await?; @@ -40,14 +49,20 @@ async fn run_benchmark(num_publishers: usize) -> Result<(), Box 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?; } @@ -67,21 +82,41 @@ async fn run_benchmark(num_publishers: usize) -> Result<(), Box Result<(), Box> { - run_benchmark(64).await +async fn test_benchmark_64_pubs_random() -> Result<(), Box> { + run_benchmark(64, TestingStrategy::Random).await +} + +#[tokio::test] +async fn test_benchmark_64_pubs_similar_prices() -> Result<(), Box> { + run_benchmark(64, TestingStrategy::SimilarPrices).await +} + +#[tokio::test] +async fn test_benchmark_32_pubs_random() -> Result<(), Box> { + run_benchmark(32, TestingStrategy::Random).await +} + +#[tokio::test] +async fn test_benchmark_32_pubs_similar_prices() -> Result<(), Box> { + run_benchmark(32, TestingStrategy::SimilarPrices).await +} + +#[tokio::test] +async fn test_benchmark_16_pubs_random() -> Result<(), Box> { + run_benchmark(16, TestingStrategy::Random).await } #[tokio::test] -async fn test_benchmark_32_pubs() -> Result<(), Box> { - run_benchmark(32).await +async fn test_benchmark_16_pubs_similar_prices() -> Result<(), Box> { + run_benchmark(16, TestingStrategy::SimilarPrices).await } #[tokio::test] -async fn test_benchmark_16_pubs() -> Result<(), Box> { - run_benchmark(16).await +async fn test_benchmark_8_pubs_random() -> Result<(), Box> { + run_benchmark(8, TestingStrategy::Random).await } #[tokio::test] -async fn test_benchmark_8_pubs() -> Result<(), Box> { - run_benchmark(8).await +async fn test_benchmark_8_pubs_similar_prices() -> Result<(), Box> { + run_benchmark(8, TestingStrategy::SimilarPrices).await }