From b2d343d7bb95db80a8c60ed69fb2ae8fbc07a5cb Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 23 Mar 2022 22:31:46 -0700 Subject: [PATCH 1/2] Add the ability to "pre-warm" Wasm engines. Signed-off-by: Piotr Sikora --- include/proxy-wasm/v8.h | 1 + include/proxy-wasm/wamr.h | 1 + include/proxy-wasm/wasmtime.h | 1 + src/v8/v8.cc | 2 ++ src/wamr/wamr.cc | 2 ++ src/wasmtime/wasmtime.cc | 2 ++ test/wasm_vm_test.cc | 42 +++++++++++++++++++++++++++++++++++ 7 files changed, 51 insertions(+) diff --git a/include/proxy-wasm/v8.h b/include/proxy-wasm/v8.h index 73c91b95..5531f52c 100644 --- a/include/proxy-wasm/v8.h +++ b/include/proxy-wasm/v8.h @@ -21,6 +21,7 @@ namespace proxy_wasm { +bool initV8Engine(); std::unique_ptr createV8Vm(); } // namespace proxy_wasm diff --git a/include/proxy-wasm/wamr.h b/include/proxy-wasm/wamr.h index 98ff72e3..32a05e94 100644 --- a/include/proxy-wasm/wamr.h +++ b/include/proxy-wasm/wamr.h @@ -21,6 +21,7 @@ namespace proxy_wasm { +bool initWamrEngine(); std::unique_ptr createWamrVm(); } // namespace proxy_wasm diff --git a/include/proxy-wasm/wasmtime.h b/include/proxy-wasm/wasmtime.h index e3fe4b48..11c4ae7a 100644 --- a/include/proxy-wasm/wasmtime.h +++ b/include/proxy-wasm/wasmtime.h @@ -18,6 +18,7 @@ namespace proxy_wasm { +bool initWasmtimeEngine(); std::unique_ptr createWasmtimeVm(); } // namespace proxy_wasm diff --git a/src/v8/v8.cc b/src/v8/v8.cc index dea88325..9e2257dc 100644 --- a/src/v8/v8.cc +++ b/src/v8/v8.cc @@ -709,6 +709,8 @@ std::string V8::getFailMessage(std::string_view function_name, wasm::own createV8Vm() { return std::make_unique(); } } // namespace proxy_wasm diff --git a/src/wamr/wamr.cc b/src/wamr/wamr.cc index 93cae4d5..8c32b041 100644 --- a/src/wamr/wamr.cc +++ b/src/wamr/wamr.cc @@ -644,6 +644,8 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name, } // namespace wamr +bool initWamrEngine() { return wamr::engine() != nullptr; } + std::unique_ptr createWamrVm() { return std::make_unique(); } } // namespace proxy_wasm diff --git a/src/wasmtime/wasmtime.cc b/src/wasmtime/wasmtime.cc index 06f4f0e3..4159b44f 100644 --- a/src/wasmtime/wasmtime.cc +++ b/src/wasmtime/wasmtime.cc @@ -694,6 +694,8 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name, } // namespace wasmtime +bool initWasmtimeEngine() { return wasmtime::engine() != nullptr; } + std::unique_ptr createWasmtimeVm() { return std::make_unique(); } } // namespace proxy_wasm diff --git a/test/wasm_vm_test.cc b/test/wasm_vm_test.cc index d29437c9..977a345f 100644 --- a/test/wasm_vm_test.cc +++ b/test/wasm_vm_test.cc @@ -30,6 +30,48 @@ INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines() return info.param; }); +TEST_P(TestVm, Init) { + std::chrono::time_point time2; + + auto time1 = std::chrono::steady_clock::now(); + if (engine_ == "v8") { +#if defined(PROXY_WASM_HOST_ENGINE_V8) + EXPECT_TRUE(proxy_wasm::initV8Engine()); + time2 = std::chrono::steady_clock::now(); + EXPECT_TRUE(proxy_wasm::initV8Engine()); +#endif + } else if (engine_ == "wamr") { +#if defined(PROXY_WASM_HOST_ENGINE_WAMR) + EXPECT_TRUE(proxy_wasm::initWamrEngine()); + time2 = std::chrono::steady_clock::now(); + EXPECT_TRUE(proxy_wasm::initWamrEngine()); +#endif + } else if (engine_ == "wasmtime") { +#if defined(PROXY_WASM_HOST_ENGINE_WASMTIME) + EXPECT_TRUE(proxy_wasm::initWasmtimeEngine()); + time2 = std::chrono::steady_clock::now(); + EXPECT_TRUE(proxy_wasm::initWasmtimeEngine()); +#endif + } else { + return; + } + auto time3 = std::chrono::steady_clock::now(); + + std::cout << "\"cold\" engine time: " + << std::chrono::duration_cast(time2 - time1).count() + << "ns" << std::endl; + std::cout << "\"warm\" engine time: " + << std::chrono::duration_cast(time3 - time2).count() + << "ns" << std::endl; + + // Verify that getting a "warm" engine takes less than 10us. + EXPECT_LE(std::chrono::duration_cast(time3 - time2).count(), 10); + + // Verify that getting a "warm" engine takes at least 50x less time than getting a "cold" one. + EXPECT_LE(std::chrono::duration_cast(time3 - time2).count() * 50, + std::chrono::duration_cast(time2 - time1).count()); +} + TEST_P(TestVm, Basic) { if (engine_ == "wamr") { EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::NotCloneable); From 10704853c62d1471b88ae4dbc8813f4d777b92fc Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 23 Mar 2022 23:51:23 -0700 Subject: [PATCH 2/2] review: style. Signed-off-by: Piotr Sikora --- test/wasm_vm_test.cc | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/wasm_vm_test.cc b/test/wasm_vm_test.cc index 977a345f..a1f2ec00 100644 --- a/test/wasm_vm_test.cc +++ b/test/wasm_vm_test.cc @@ -57,19 +57,17 @@ TEST_P(TestVm, Init) { } auto time3 = std::chrono::steady_clock::now(); - std::cout << "\"cold\" engine time: " - << std::chrono::duration_cast(time2 - time1).count() - << "ns" << std::endl; - std::cout << "\"warm\" engine time: " - << std::chrono::duration_cast(time3 - time2).count() - << "ns" << std::endl; + auto cold = std::chrono::duration_cast(time2 - time1).count(); + auto warm = std::chrono::duration_cast(time3 - time2).count(); + + std::cout << "\"cold\" engine time: " << cold << "ns" << std::endl; + std::cout << "\"warm\" engine time: " << warm << "ns" << std::endl; // Verify that getting a "warm" engine takes less than 10us. - EXPECT_LE(std::chrono::duration_cast(time3 - time2).count(), 10); + EXPECT_LE(warm, 10000); // Verify that getting a "warm" engine takes at least 50x less time than getting a "cold" one. - EXPECT_LE(std::chrono::duration_cast(time3 - time2).count() * 50, - std::chrono::duration_cast(time2 - time1).count()); + EXPECT_LE(warm * 50, cold); } TEST_P(TestVm, Basic) {