diff --git a/Cargo.toml b/Cargo.toml index 5a33072fd..f6423cd7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,8 @@ members = [ "packages/plugin", "packages/native", "packages/resolver-extensions", - "packages/default-config" + "packages/default-config", + "packages/benchmarks" ] # Do not include test utils & default config crates in default members @@ -35,7 +36,7 @@ default-members = [ ] [workspace.dependencies] -polywrap_client = { version = "0.1.6-beta.7", path = "packages/client" } +polywrap_client = { version = "0.1.6-beta.7", path = "packages/client" } polywrap_wasm = { version = "0.1.6-beta.7", path = "packages/wasm" } polywrap_client_builder = { version = "0.1.6-beta.7", path = "packages/builder" } polywrap_core = { version = "0.1.6-beta.7", path = "packages/core" } diff --git a/packages/benchmarks/Cargo.toml b/packages/benchmarks/Cargo.toml new file mode 100644 index 000000000..68346bbfc --- /dev/null +++ b/packages/benchmarks/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "polywrap_benchmarks" +description = "Polywrap Client benchmarks in rust" +repository = "https://github.com/polywrap/rust-client" + +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true + +[lib] +crate-type = ["rlib"] + +[dependencies] +polywrap_client.workspace = true +polywrap_wasm.workspace = true +polywrap_client_builder.workspace = true +polywrap_core.workspace = true +wrap_manifest_schemas.workspace = true +polywrap_resolvers.workspace = true +polywrap_msgpack.workspace = true +polywrap_tests_utils.workspace = true +polywrap_plugin.workspace = true +polywrap_plugin_implementor.workspace = true +polywrap_client_default_config.workspace = true +polywrap-wasm-rs = "*" + +[dev-dependencies] +criterion = { version = "0.5.1", features = ["html_reports"] } + +[[bench]] +name = "bench_invoke" +harness = false + +[[bench]] +name = "bench_runtime" +harness = false + +[[bench]] +name = "bench_load_wrapper" +harness = false \ No newline at end of file diff --git a/packages/benchmarks/benches/bench_invoke.rs b/packages/benchmarks/benches/bench_invoke.rs new file mode 100644 index 000000000..a26e48b81 --- /dev/null +++ b/packages/benchmarks/benches/bench_invoke.rs @@ -0,0 +1,97 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::collections::HashMap; +use std::sync::Arc; + +use polywrap_client::client::PolywrapClient; +use polywrap_client::core::uri::Uri; +use polywrap_core::client::ClientConfig; +use polywrap_core::file_reader::SimpleFileReader; +use polywrap_core::resolution::uri_resolution_context::UriPackageOrWrapper; +use polywrap_msgpack::msgpack; +use polywrap_resolvers::base_resolver::BaseResolver; +use polywrap_resolvers::simple_file_resolver::FilesystemResolver; +use polywrap_resolvers::static_resolver::StaticResolver; +use polywrap_tests_utils::helpers::get_tests_path_string; + +fn prepare_client() -> PolywrapClient { + let path = get_tests_path_string(); + let subinvoke_uri = Uri::try_from(format!( + "fs/{path}/subinvoke/00-subinvoke/implementations/rs" + )).unwrap(); + + let mut resolvers = HashMap::new(); + resolvers.insert( + String::from("wrap://ens/imported-subinvoke.eth"), + UriPackageOrWrapper::Uri(subinvoke_uri), + ); + let file_reader = SimpleFileReader::new(); + let fs_resolver = FilesystemResolver::new(Arc::new(file_reader)); + + let base_resolver = BaseResolver::new( + Box::new(fs_resolver), + Box::new(StaticResolver::new(resolvers)), + ); + + let config = ClientConfig { + resolver: Arc::new(base_resolver), + envs: None, + interfaces: None, + }; + let client = PolywrapClient::new(config); + client +} + +fn bench_invoke(c: &mut Criterion) { + let client = prepare_client(); + + // Note: this is using the correct URI for invoke, which is in the 00-subinvoke wrapper + let path = get_tests_path_string(); + let uri = Uri::try_from(format!( + "fs/{path}/subinvoke/00-subinvoke/implementations/rs" + )).unwrap(); + + c.bench_function("client/invoke", |b| b.iter(|| { + let result = client + .invoke::( + &uri, + "add", + Some(&msgpack!({"a": 1, "b": 1})), + None, + None, + ) + .unwrap(); + + assert_eq!(result, 2); + })); +} + +fn bench_subinvoke(c: &mut Criterion) { + let client = prepare_client(); + + // Note: this is using the correct URI for subinvoke, which is in the 01-invoke wrapper + let path = get_tests_path_string(); + let uri = Uri::try_from(format!( + "fs/{path}/subinvoke/01-invoke/implementations/rs" + )).unwrap(); + + c.bench_function("client/subinvoke", |b| b.iter(|| { + let result = client + .invoke::( + &uri, + "addAndIncrement", + Some(&msgpack!({"a": 1, "b": 1})), + None, + None, + ) + .unwrap(); + + assert_eq!(result, 3); + })); +} + +criterion_group!{ + name = benches; + config = Criterion::default().sample_size(60); + targets = bench_invoke, bench_subinvoke +} +criterion_main!(benches); diff --git a/packages/benchmarks/benches/bench_load_wrapper.rs b/packages/benchmarks/benches/bench_load_wrapper.rs new file mode 100644 index 000000000..4173b1b20 --- /dev/null +++ b/packages/benchmarks/benches/bench_load_wrapper.rs @@ -0,0 +1,79 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use criterion::BatchSize::PerIteration; + +use polywrap_client::client::PolywrapClient; +use polywrap_core::{ + uri::Uri, + wrap_loader::WrapLoader, +}; +use polywrap_client_builder::types::{ClientConfigHandler}; +use polywrap_core::uri_resolver_handler::UriResolverHandler; +use polywrap_benchmarks::{get_fibonacci_dir}; + +fn prepare_client() -> PolywrapClient { + let builder = polywrap_client_default_config::build(); + let config = builder.build(); + PolywrapClient::new(config) +} + +pub struct UriCase { + pub id: String, + pub uri: Uri, +} + +pub fn prepare_uris() -> Vec { + let fs_uri = UriCase { + id: "fs_uri".to_string(), + uri: Uri::try_from(format!("fs/{}", get_fibonacci_dir("rs"))).unwrap(), + }; + let http_uri = UriCase { + id: "http_uri".to_string(), + uri: Uri::try_from(format!( + "http/https://raw.githubusercontent.com/polywrap/rust-client/kris/debug-slow-invocation/packages/benchmarks/fibonacci/rs/build" + )).unwrap(), + }; + let ipfs_uri = UriCase { + id: "ipfs_uri".to_string(), + uri: Uri::try_from("ipfs/QmXTYY4HAhurxZURrnRM8uD2oBCog8ATYDruVekuXQB192").unwrap(), + }; + vec![fs_uri, http_uri, ipfs_uri] +} + +fn bench_try_resolve_uri(c: &mut Criterion) { + let uri_list = prepare_uris(); + + let mut group = c.benchmark_group("client/try_resolve_uri"); + + for uri in uri_list { + group.bench_with_input(&uri.id, &uri.uri, |b, uri| b.iter_batched_ref(|| { + prepare_client() + }, | client: &mut PolywrapClient | { + client.try_resolve_uri(uri, None).unwrap(); + }, + PerIteration + )); + } +} + +fn bench_load_wrapper(c: &mut Criterion) { + let uri_list = prepare_uris(); + + let mut group = c.benchmark_group("client/load_wrapper"); + + for uri in uri_list { + group.bench_with_input(&uri.id, &uri.uri, |b, uri| b.iter_batched_ref(|| { + prepare_client() + }, | client: &mut PolywrapClient | { + client.load_wrapper(uri, None).unwrap(); + }, + PerIteration + )); + } +} + +criterion_group!{ + name = benches; + config = Criterion::default().sample_size(10); + targets = bench_try_resolve_uri, bench_load_wrapper +} +criterion_main!(benches); \ No newline at end of file diff --git a/packages/benchmarks/benches/bench_runtime.rs b/packages/benchmarks/benches/bench_runtime.rs new file mode 100644 index 000000000..723c5afd2 --- /dev/null +++ b/packages/benchmarks/benches/bench_runtime.rs @@ -0,0 +1,117 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use std::{path::Path}; +use polywrap_wasm::{wasm_wrapper::{WasmWrapper}}; +use polywrap_core::{ + file_reader::{SimpleFileReader}, + wrapper::Wrapper, +}; + +use polywrap_msgpack::msgpack; +use std::sync::{Arc}; +use std::fs; +use polywrap_tests_utils::mocks::MockInvoker; +use polywrap_benchmarks::{get_fibonacci_wrap}; +use polywrap_benchmarks::fibonacci::{fibonacci_loop, fibonacci_recursive}; +use polywrap_tests_utils::helpers::get_tests_path_string; + +fn bench_invoke(c: &mut Criterion) { + // Note: this is using the correct URI for invoke, which is in the 00-subinvoke wrapper + let path = get_tests_path_string(); + let module_path = format!("{path}/subinvoke/00-subinvoke/implementations/as/wrap.wasm"); + + let module_bytes = fs::read(Path::new(&module_path)).unwrap(); + let file_reader = SimpleFileReader::new(); + let wrapper = WasmWrapper::new(module_bytes, Arc::new(file_reader)); + + let mock_invoker = Arc::new(MockInvoker {}); + + c.bench_function("wasm/invoke", |b| b.iter(|| { + let result = wrapper.invoke( + "add", + Some(&msgpack!({ "a": 1, "b": 1})), + None, + mock_invoker.clone(), + None + ).unwrap(); + assert_eq!(result, [2]); + })); +} + +fn bench_fibonacci_loop(c: &mut Criterion) { + let mock_invoker = Arc::new(MockInvoker {}); + let n = 10_000; + + let mut group = c.benchmark_group("wasm/fibonacci_loop"); + + group.bench_function("native", |b| b.iter(|| { + fibonacci_loop(n).unwrap() + })); + + let wrapper = get_fibonacci_wrap("as"); + group.bench_function("as", |b| b.iter(|| { + wrapper.invoke( + "fibonacci_loop", + Some(&msgpack!({ "n": n })), + None, + mock_invoker.clone(), + None + ).unwrap(); + })); + + let wrapper = get_fibonacci_wrap("rs"); + group.bench_function("rs", |b| b.iter(|| { + wrapper.invoke( + "fibonacci_loop", + Some(&msgpack!({ "n": n })), + None, + mock_invoker.clone(), + None + ).unwrap(); + })); + + group.finish(); +} + +fn bench_fibonacci_recursive(c: &mut Criterion) { + let mock_invoker = Arc::new(MockInvoker {}); + + // AS wrap crashes at higher N + let n = 30; + + let mut group = c.benchmark_group("wasm/fibonacci_recursive"); + + group.bench_function("native", |b| b.iter(|| { + fibonacci_recursive(n).unwrap() + })); + + let wrapper = get_fibonacci_wrap("as"); + group.bench_function("as", |b| b.iter(|| { + wrapper.invoke( + "fibonacci_recursive", + Some(&msgpack!({ "n": n })), + None, + mock_invoker.clone(), + None + ).unwrap(); + })); + + let wrapper = get_fibonacci_wrap("rs"); + group.bench_function("rs", |b| b.iter(|| { + wrapper.invoke( + "fibonacci_recursive", + Some(&msgpack!({ "n": n })), + None, + mock_invoker.clone(), + None + ).unwrap(); + })); + + group.finish(); +} + +criterion_group!{ + name = benches; + config = Criterion::default().sample_size(10); + targets = bench_invoke, bench_fibonacci_loop, bench_fibonacci_recursive +} +criterion_main!(benches); diff --git a/packages/benchmarks/fibonacci/as/.gitignore b/packages/benchmarks/fibonacci/as/.gitignore new file mode 100644 index 000000000..fe4543bdf --- /dev/null +++ b/packages/benchmarks/fibonacci/as/.gitignore @@ -0,0 +1,5 @@ +wrap +node_modules +.polywrap +yarn.lock +Cargo.lock \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/as/URI.txt b/packages/benchmarks/fibonacci/as/URI.txt new file mode 100644 index 000000000..2d92c45f9 --- /dev/null +++ b/packages/benchmarks/fibonacci/as/URI.txt @@ -0,0 +1 @@ +wrap://ipfs/QmYjsgLJ9z9Z5BytJip1JYC3yQ5mSoy1svd6afzoK3swH1 \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/as/build/wrap.info b/packages/benchmarks/fibonacci/as/build/wrap.info new file mode 100644 index 000000000..fc7ea0269 --- /dev/null +++ b/packages/benchmarks/fibonacci/as/build/wrap.info @@ -0,0 +1 @@ +„§version£0.1¤name¬fibonacci-as¤type¤wasm£abi‚§version£0.1ªmoduleTypeƒ¤type¦Module¤kindÌ€§methods’†¤name®fibonacci_loop¦return…¤type¦BigInt¤name®fibonacci_loop¨requiredäkind"¦scalar„¤name®fibonacci_loop¤type¦BigInt¨requiredäkind¤type¦Method¤kind@¨requiredéarguments‘…¤type£Int¤name¡n¨requiredäkind"¦scalar„¤name¡n¤type£Int¨requiredäkind†¤name³fibonacci_recursive¦return…¤type¦BigInt¤name³fibonacci_recursive¨requiredäkind"¦scalar„¤name³fibonacci_recursive¤type¦BigInt¨requiredäkind¤type¦Method¤kind@¨requiredéarguments‘…¤type£Int¤name¡n¨requiredäkind"¦scalar„¤name¡n¤type£Int¨requiredäkind \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/as/build/wrap.wasm b/packages/benchmarks/fibonacci/as/build/wrap.wasm new file mode 100755 index 000000000..5d08da728 Binary files /dev/null and b/packages/benchmarks/fibonacci/as/build/wrap.wasm differ diff --git a/packages/benchmarks/fibonacci/as/package.json b/packages/benchmarks/fibonacci/as/package.json new file mode 100644 index 000000000..66b18c64c --- /dev/null +++ b/packages/benchmarks/fibonacci/as/package.json @@ -0,0 +1,15 @@ +{ + "name": "test-cases-default", + "private": true, + "scripts": { + "build": "npx polywrap build", + "codegen": "npx polywrap codegen" + }, + "dependencies": { + "@polywrap/wasm-as": "latest", + "assemblyscript": "0.19.23" + }, + "devDependencies": { + "polywrap": "latest" + } +} \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/as/polywrap.deploy.yaml b/packages/benchmarks/fibonacci/as/polywrap.deploy.yaml new file mode 100644 index 000000000..b7ba99b4f --- /dev/null +++ b/packages/benchmarks/fibonacci/as/polywrap.deploy.yaml @@ -0,0 +1,9 @@ +format: 0.2.0 +jobs: + deploy: + steps: + - name: ipfs_deploy + package: ipfs + uri: fs/./build + config: + gatewayUri: https://ipfs.wrappers.io diff --git a/packages/benchmarks/fibonacci/as/polywrap.yaml b/packages/benchmarks/fibonacci/as/polywrap.yaml new file mode 100644 index 000000000..dbe6df805 --- /dev/null +++ b/packages/benchmarks/fibonacci/as/polywrap.yaml @@ -0,0 +1,7 @@ +format: 0.3.0 +project: + name: fibonacci-as + type: wasm/assemblyscript +source: + schema: ../polywrap.graphql + module: ./src/index.ts diff --git a/packages/benchmarks/fibonacci/as/src/index.ts b/packages/benchmarks/fibonacci/as/src/index.ts new file mode 100644 index 000000000..4010531e9 --- /dev/null +++ b/packages/benchmarks/fibonacci/as/src/index.ts @@ -0,0 +1,36 @@ +import { + Args_fibonacci_loop, + Args_fibonacci_recursive, + ModuleBase +} from "./wrap"; +import { BigInt } from "@polywrap/wasm-as" + +export class Module extends ModuleBase { + fibonacci_loop(args: Args_fibonacci_loop): BigInt { + const n = args.n; + let a = BigInt.ZERO; + let b = BigInt.ONE; + + if (n == 0) return BigInt.ZERO; + if (n == 1) return BigInt.ONE; + for (let i = 0; i < n - 1; i++) { + let temp = a + b; + a = b; + b = temp; + } + return b; + } + + fibonacci_recursive(args: Args_fibonacci_recursive): BigInt { + return this._fibonacci(BigInt.from(args.n)); + } + + private _fibonacci(n: BigInt): BigInt { + if (n == BigInt.ZERO) return BigInt.ZERO; + if (n == BigInt.ONE) return BigInt.ONE; + + let n_1 = n - BigInt.ONE; + let n_2 = n - BigInt.from(2); + return this._fibonacci(n_1) + this._fibonacci(n_2); + } +} diff --git a/packages/benchmarks/fibonacci/polywrap.graphql b/packages/benchmarks/fibonacci/polywrap.graphql new file mode 100644 index 000000000..e979147cf --- /dev/null +++ b/packages/benchmarks/fibonacci/polywrap.graphql @@ -0,0 +1,4 @@ +type Module { + fibonacci_loop(n: Int!): BigInt! + fibonacci_recursive(n: Int!): BigInt! +} diff --git a/packages/benchmarks/fibonacci/rs/.gitignore b/packages/benchmarks/fibonacci/rs/.gitignore new file mode 100644 index 000000000..fe4543bdf --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/.gitignore @@ -0,0 +1,5 @@ +wrap +node_modules +.polywrap +yarn.lock +Cargo.lock \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/rs/Cargo.toml b/packages/benchmarks/fibonacci/rs/Cargo.toml new file mode 100644 index 000000000..44436b858 --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "module" +version = "0.1.0" +description = "Default dependencies for test harness" +repository = "https://github.com/polywrap/monorepo" +license = "MIT" +edition = "2021" + +[dependencies] +polywrap-wasm-rs = "*" +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0", features = ["preserve_order"]} + +[lib] +crate-type = ["cdylib", "rlib"] + +[profile.release] +opt-level = 's' +lto = true +panic = 'abort' + +[workspace] \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/rs/URI.txt b/packages/benchmarks/fibonacci/rs/URI.txt new file mode 100644 index 000000000..f25a23c6a --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/URI.txt @@ -0,0 +1 @@ +wrap://ipfs/QmXTYY4HAhurxZURrnRM8uD2oBCog8ATYDruVekuXQB192 \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/rs/build/wrap.info b/packages/benchmarks/fibonacci/rs/build/wrap.info new file mode 100644 index 000000000..559f22f73 --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/build/wrap.info @@ -0,0 +1 @@ +„§version£0.1¤name¬fibonacci-rs¤type¤wasm£abi‚§version£0.1ªmoduleTypeƒ¤type¦Module¤kindÌ€§methods’†¤name®fibonacci_loop¦return…¤type¦BigInt¤name®fibonacci_loop¨requiredäkind"¦scalar„¤name®fibonacci_loop¤type¦BigInt¨requiredäkind¤type¦Method¤kind@¨requiredéarguments‘…¤type£Int¤name¡n¨requiredäkind"¦scalar„¤name¡n¤type£Int¨requiredäkind†¤name³fibonacci_recursive¦return…¤type¦BigInt¤name³fibonacci_recursive¨requiredäkind"¦scalar„¤name³fibonacci_recursive¤type¦BigInt¨requiredäkind¤type¦Method¤kind@¨requiredéarguments‘…¤type£Int¤name¡n¨requiredäkind"¦scalar„¤name¡n¤type£Int¨requiredäkind \ No newline at end of file diff --git a/packages/benchmarks/fibonacci/rs/build/wrap.wasm b/packages/benchmarks/fibonacci/rs/build/wrap.wasm new file mode 100755 index 000000000..4a938b84d Binary files /dev/null and b/packages/benchmarks/fibonacci/rs/build/wrap.wasm differ diff --git a/packages/benchmarks/fibonacci/rs/package.json b/packages/benchmarks/fibonacci/rs/package.json new file mode 100644 index 000000000..00488c461 --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/package.json @@ -0,0 +1,11 @@ +{ + "name": "polywrap_fibonacci_wrap", + "version": "0.1.1", + "scripts": { + "build": "npx polywrap build", + "codegen": "npx polywrap codegen" + }, + "dependencies": { + "polywrap": "latest" + } +} diff --git a/packages/benchmarks/fibonacci/rs/polywrap.deploy.yaml b/packages/benchmarks/fibonacci/rs/polywrap.deploy.yaml new file mode 100644 index 000000000..b7ba99b4f --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/polywrap.deploy.yaml @@ -0,0 +1,9 @@ +format: 0.2.0 +jobs: + deploy: + steps: + - name: ipfs_deploy + package: ipfs + uri: fs/./build + config: + gatewayUri: https://ipfs.wrappers.io diff --git a/packages/benchmarks/fibonacci/rs/polywrap.yaml b/packages/benchmarks/fibonacci/rs/polywrap.yaml new file mode 100644 index 000000000..608909e25 --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/polywrap.yaml @@ -0,0 +1,7 @@ +format: 0.3.0 +project: + name: fibonacci-rs + type: wasm/rust +source: + schema: ../polywrap.graphql + module: ./Cargo.toml diff --git a/packages/benchmarks/fibonacci/rs/src/lib.rs b/packages/benchmarks/fibonacci/rs/src/lib.rs new file mode 100644 index 000000000..5b488488f --- /dev/null +++ b/packages/benchmarks/fibonacci/rs/src/lib.rs @@ -0,0 +1,45 @@ +pub mod wrap; +use wrap::module::{Module, ModuleTrait}; +pub use wrap::*; +use polywrap_wasm_rs::BigInt; + +impl ModuleTrait for Module { + fn fibonacci_loop(args: ArgsFibonacciLoop) -> Result { + let n = args.n; + let mut a = BigInt::from(0); + let mut b = BigInt::from(1); + let result = match n { + 0 => BigInt::from(0), + 1 => BigInt::from(1), + _ => { + for _ in 0..n - 1 { + let temp = &a + &b; + a = b; + b = temp; + } + b + }, + }; + Ok(result) + } + + fn fibonacci_recursive(args: ArgsFibonacciRecursive) -> Result { + let n = BigInt::from(args.n); + Ok(_fibonacci(&n)) + } +} + +fn _fibonacci(n: &BigInt) -> BigInt { + let zero = BigInt::from(0); + let one = BigInt::from(1); + if n == &zero { + zero + } else if n == &one { + one + } else { + let n_1 = n - one; + let n_2 = n - 2; + _fibonacci(&n_1) + _fibonacci(&n_2) + } +} + diff --git a/packages/benchmarks/src/fibonacci.rs b/packages/benchmarks/src/fibonacci.rs new file mode 100644 index 000000000..4cfe112a4 --- /dev/null +++ b/packages/benchmarks/src/fibonacci.rs @@ -0,0 +1,37 @@ +use polywrap_wasm_rs::BigInt; + +pub fn fibonacci_loop(n: i32) -> Result { + let mut a = BigInt::from(0); + let mut b = BigInt::from(1); + let result = match n { + 0 => BigInt::from(0), + 1 => BigInt::from(1), + _ => { + for _ in 0..n - 1 { + let temp = &a + &b; + a = b; + b = temp; + } + b + }, + }; + Ok(result) +} + +pub fn fibonacci_recursive(n: i32) -> Result { + Ok(_fibonacci(&BigInt::from(n))) +} + +fn _fibonacci(n: &BigInt) -> BigInt { + let zero = BigInt::from(0); + let one = BigInt::from(1); + if n == &zero { + zero + } else if n == &one { + one + } else { + let n_1 = n - one; + let n_2 = n - 2; + _fibonacci(&n_1) + _fibonacci(&n_2) + } +} \ No newline at end of file diff --git a/packages/benchmarks/src/lib.rs b/packages/benchmarks/src/lib.rs new file mode 100644 index 000000000..b4657f36b --- /dev/null +++ b/packages/benchmarks/src/lib.rs @@ -0,0 +1,26 @@ +pub mod fibonacci; + +use std::fs; +use std::path::Path; +use std::sync::{Arc}; +use polywrap_core::file_reader::SimpleFileReader; +use polywrap_tests_utils::helpers::get_tests_path; +use polywrap_wasm::wasm_wrapper::WasmWrapper; + +pub fn get_fibonacci_dir(implementation: &str) -> String { + let relative_path = format!("fibonacci/{}/build", implementation); + Path::new(&relative_path) + .canonicalize() + .unwrap() + .into_os_string() + .into_string() + .unwrap() +} + +pub fn get_fibonacci_wrap(implementation: &str) -> WasmWrapper { + let path = Path::new(&get_fibonacci_dir(implementation)).join("wrap.wasm"); + let module_bytes = fs::read(path).unwrap(); + let file_reader = SimpleFileReader::new(); + let wrapper = WasmWrapper::new(module_bytes, Arc::new(file_reader)); + wrapper +} diff --git a/packages/client/tests/it/wrap_features/env_with_invoke.rs b/packages/client/tests/it/wrap_features/env_with_invoke.rs index c35ed1fd6..36ffda2b5 100644 --- a/packages/client/tests/it/wrap_features/env_with_invoke.rs +++ b/packages/client/tests/it/wrap_features/env_with_invoke.rs @@ -11,16 +11,13 @@ use polywrap_resolvers::recursive_resolver::RecursiveResolver; use polywrap_resolvers::resolver_vec; use polywrap_resolvers::simple_file_resolver::FilesystemResolver; use polywrap_resolvers::static_resolver::StaticResolver; -use polywrap_tests_utils::helpers::get_tests_path; +use polywrap_tests_utils::helpers::get_tests_path_string; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::Arc; fn get_env_wrapper_uri() -> Uri { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); - - + let path = get_tests_path_string(); Uri::try_from(format!( "fs/{path}/env-type/00-main/implementations/rs" diff --git a/packages/client/tests/it/wrap_features/env_with_subinvoke.rs b/packages/client/tests/it/wrap_features/env_with_subinvoke.rs index 093af6139..fb35c9430 100644 --- a/packages/client/tests/it/wrap_features/env_with_subinvoke.rs +++ b/packages/client/tests/it/wrap_features/env_with_subinvoke.rs @@ -8,16 +8,13 @@ use polywrap_core::resolution::uri_resolution_context::UriPackageOrWrapper; use polywrap_resolvers::base_resolver::BaseResolver; use polywrap_resolvers::simple_file_resolver::FilesystemResolver; use polywrap_resolvers::static_resolver::StaticResolver; -use polywrap_tests_utils::helpers::get_tests_path; +use polywrap_tests_utils::helpers::get_tests_path_string; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::Arc; fn get_subinvoker_uri() -> Uri { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); - - + let path = get_tests_path_string(); Uri::try_from(format!( "fs/{path}/env-type/01-subinvoker/implementations/rs" @@ -26,10 +23,7 @@ fn get_subinvoker_uri() -> Uri { } fn get_subinvoker_with_env_uri() -> Uri { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); - - + let path = get_tests_path_string(); Uri::try_from(format!( "fs/{path}/env-type/02-subinvoker-with-env/implementations/rs" @@ -38,10 +32,7 @@ fn get_subinvoker_with_env_uri() -> Uri { } fn get_subinvoked_uri() -> Uri { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); - - + let path = get_tests_path_string(); Uri::try_from(format!( "fs/{path}/env-type/00-main/implementations/rs" diff --git a/packages/client/tests/it/wrap_features/interface_implementation.rs b/packages/client/tests/it/wrap_features/interface_implementation.rs index f7a671306..696014b23 100644 --- a/packages/client/tests/it/wrap_features/interface_implementation.rs +++ b/packages/client/tests/it/wrap_features/interface_implementation.rs @@ -7,7 +7,7 @@ use polywrap_core::file_reader::SimpleFileReader; use polywrap_resolvers::base_resolver::BaseResolver; use polywrap_resolvers::simple_file_resolver::FilesystemResolver; use polywrap_resolvers::static_resolver::StaticResolver; -use polywrap_tests_utils::helpers::get_tests_path; +use polywrap_tests_utils::helpers::get_tests_path_string; use serde::Deserialize; use std::collections::HashMap; use std::sync::Arc; @@ -20,8 +20,7 @@ struct ModuleMethodResponse { #[test] fn test_interface_implementation() { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); + let path = get_tests_path_string(); let implementation_uri = Uri::try_from(format!( "fs/{path}/interface-invoke/01-implementation/implementations/as" )) diff --git a/packages/client/tests/it/wrap_features/subinvoke.rs b/packages/client/tests/it/wrap_features/subinvoke.rs index 4fedeb420..2bc894077 100644 --- a/packages/client/tests/it/wrap_features/subinvoke.rs +++ b/packages/client/tests/it/wrap_features/subinvoke.rs @@ -10,12 +10,11 @@ use polywrap_msgpack::msgpack; use polywrap_resolvers::base_resolver::BaseResolver; use polywrap_resolvers::simple_file_resolver::FilesystemResolver; use polywrap_resolvers::static_resolver::StaticResolver; -use polywrap_tests_utils::helpers::get_tests_path; +use polywrap_tests_utils::helpers::get_tests_path_string; #[test] fn subinvoke_test() { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); + let path = get_tests_path_string(); let invoke_uri = Uri::try_from(format!( "fs/{path}/subinvoke/01-invoke/implementations/rs" diff --git a/packages/tests-utils/src/helpers.rs b/packages/tests-utils/src/helpers.rs index ac7b8270c..429394804 100644 --- a/packages/tests-utils/src/helpers.rs +++ b/packages/tests-utils/src/helpers.rs @@ -6,3 +6,9 @@ pub fn get_tests_path() -> Result { .unwrap(); Ok(path) } + +pub fn get_tests_path_string() -> String { + let test_path = get_tests_path().unwrap(); + let path = test_path.into_os_string().into_string().unwrap(); + path +} \ No newline at end of file diff --git a/packages/wasm/tests/test_runtime.rs b/packages/wasm/tests/test_runtime.rs index 6f181c562..1d70ac1d8 100644 --- a/packages/wasm/tests/test_runtime.rs +++ b/packages/wasm/tests/test_runtime.rs @@ -6,14 +6,11 @@ use polywrap_core::{ error::Error, file_reader::{SimpleFileReader}, resolution::uri_resolution_context::UriResolutionContext, wrapper::Wrapper, interface_implementation::InterfaceImplementations }; -use wrap_manifest_schemas::{ - deserialize::deserialize_wrap_manifest -}; use polywrap_msgpack::msgpack; use std::sync::{Arc}; use std::fs; -use polywrap_tests_utils::helpers::get_tests_path; +use polywrap_tests_utils::helpers::get_tests_path_string; #[derive(Clone)] struct MockInvoker { @@ -79,16 +76,12 @@ impl Invoker for MockInvoker { #[test] fn invoke_test() { - let test_path = get_tests_path().unwrap(); - let path = test_path.into_os_string().into_string().unwrap(); + let path = get_tests_path_string(); let module_path = format!("{path}/subinvoke/00-subinvoke/implementations/as/wrap.wasm"); - let manifest_path = format!("{path}/subinvoke/00-subinvoke/implementations/as/wrap.info"); let module_bytes = fs::read(Path::new(&module_path)).unwrap(); - let manifest_bytes = fs::read(Path::new(&manifest_path)).unwrap(); - - let _manifest = deserialize_wrap_manifest(&manifest_bytes, None).unwrap(); + let file_reader = SimpleFileReader::new(); let wrapper = WasmWrapper::new(module_bytes, Arc::new(file_reader));