Skip to content

Commit f401b36

Browse files
committed
Added initV8Engine() and test.
Signed-off-by: Rachel Green <[email protected]>
1 parent e2dcb32 commit f401b36

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

include/proxy-wasm/v8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace proxy_wasm {
2323

24+
bool initV8Engine();
2425
std::unique_ptr<WasmVm> createV8Vm();
2526

2627
} // namespace proxy_wasm

src/v8/v8.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ std::string V8::getFailMessage(std::string_view function_name, wasm::own<wasm::T
755755

756756
} // namespace v8
757757

758+
bool initV8Engine() { return v8::engine() != nullptr; }
759+
758760
std::unique_ptr<WasmVm> createV8Vm() { return std::make_unique<v8::V8>(); }
759761

760762
} // namespace proxy_wasm

test/wasm_vm_test.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,46 @@ INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines()
3030
return info.param;
3131
});
3232

33+
TEST_P(TestVm, Init) {
34+
std::chrono::time_point<std::chrono::steady_clock> time2;
35+
36+
auto time1 = std::chrono::steady_clock::now();
37+
if (engine_ == "v8") {
38+
#if defined(PROXY_WASM_HOST_ENGINE_V8)
39+
EXPECT_TRUE(proxy_wasm::initV8Engine());
40+
time2 = std::chrono::steady_clock::now();
41+
EXPECT_TRUE(proxy_wasm::initV8Engine());
42+
#endif
43+
} else if (engine_ == "wamr") {
44+
#if defined(PROXY_WASM_HOST_ENGINE_WAMR)
45+
EXPECT_TRUE(proxy_wasm::initWamrEngine());
46+
time2 = std::chrono::steady_clock::now();
47+
EXPECT_TRUE(proxy_wasm::initWamrEngine());
48+
#endif
49+
} else if (engine_ == "wasmtime") {
50+
#if defined(PROXY_WASM_HOST_ENGINE_WASMTIME)
51+
EXPECT_TRUE(proxy_wasm::initWasmtimeEngine());
52+
time2 = std::chrono::steady_clock::now();
53+
EXPECT_TRUE(proxy_wasm::initWasmtimeEngine());
54+
#endif
55+
} else {
56+
return;
57+
}
58+
auto time3 = std::chrono::steady_clock::now();
59+
60+
auto cold = std::chrono::duration_cast<std::chrono::nanoseconds>(time2 - time1).count();
61+
auto warm = std::chrono::duration_cast<std::chrono::nanoseconds>(time3 - time2).count();
62+
63+
std::cout << "\"cold\" engine time: " << cold << "ns" << std::endl;
64+
std::cout << "\"warm\" engine time: " << warm << "ns" << std::endl;
65+
66+
// Verify that getting a "warm" engine takes less than 10us.
67+
EXPECT_LE(warm, 10000);
68+
69+
// Verify that getting a "warm" engine takes at least 50x less time than getting a "cold" one.
70+
EXPECT_LE(warm * 50, cold);
71+
}
72+
3373
TEST_P(TestVm, Basic) {
3474
if (engine_ == "wasmedge") {
3575
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::NotCloneable);

0 commit comments

Comments
 (0)