Skip to content

Commit fb145cb

Browse files
committed
Add all init*Engine() static functions, TestVm.Init test, and all warm() implementations.
Signed-off-by: Rachel Green <[email protected]>
1 parent 3a7c1bc commit fb145cb

File tree

8 files changed

+84
-5
lines changed

8 files changed

+84
-5
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

include/proxy-wasm/wamr.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 initWamrEngine();
2425
std::unique_ptr<WasmVm> createWamrVm();
2526

2627
} // namespace proxy_wasm

include/proxy-wasm/wasmtime.h

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

1919
namespace proxy_wasm {
2020

21+
bool initWasmtimeEngine();
2122
std::unique_ptr<WasmVm> createWasmtimeVm();
2223

2324
} // namespace proxy_wasm

src/v8/v8.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ std::string V8::getPluginFailMessage(std::string_view function_name, wasm::own<w
757757

758758
} // namespace v8
759759

760+
bool initV8Engine() { return v8::engine() != nullptr; }
761+
760762
std::unique_ptr<WasmVm> createV8Vm() { return std::make_unique<v8::V8>(); }
761763

762764
} // namespace proxy_wasm

src/wamr/wamr.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,16 @@ class Wamr : public WasmVm {
121121
std::unordered_map<std::string, WasmFuncPtr> module_functions_;
122122
};
123123

124+
void Wamr::initStore() {
125+
if (store_ != nullptr) {
126+
return;
127+
}
128+
store_ = wasm_store_new(engine());
129+
}
130+
124131
bool Wamr::load(std::string_view bytecode, std::string_view precompiled,
125132
const std::unordered_map<uint32_t, std::string> & /*function_names*/) {
126-
store_ = wasm_store_new(engine());
133+
initStore();
127134
if (store_ == nullptr) {
128135
return false;
129136
}
@@ -699,8 +706,12 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
699706
};
700707
};
701708

709+
void Wamr::warm() { initStore(); }
710+
702711
} // namespace wamr
703712

713+
bool initWamrEngine() { return wamr::engine() != nullptr; }
714+
704715
std::unique_ptr<WasmVm> createWamrVm() { return std::make_unique<wamr::Wamr>(); }
705716

706717
} // namespace proxy_wasm

src/wasmedge/wasmedge.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,19 @@ bool WasmEdge::load(std::string_view bytecode, std::string_view /*precompiled*/,
317317
return true;
318318
}
319319

320+
void WasmEdge::initStore() {
321+
if (store_ != nullptr) {
322+
return;
323+
}
324+
store_ = WasmEdge_StoreCreate();
325+
}
326+
320327
bool WasmEdge::link(std::string_view /*debug_name*/) {
321328
assert(ast_module_ != nullptr);
322329

323330
// Create store and register imports.
324-
if (store_ == nullptr) {
325-
store_ = WasmEdge_StoreCreate();
326-
}
331+
initStore();
332+
327333
if (store_ == nullptr) {
328334
return false;
329335
}
@@ -612,8 +618,12 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
612618
};
613619
}
614620

621+
void WasmEdge::warm() { initStore(); }
622+
615623
} // namespace WasmEdge
616624

625+
bool initWasmEdgeEngine() { return wasmedge::engine() != nullptr; }
626+
617627
std::unique_ptr<WasmVm> createWasmEdgeVm() { return std::make_unique<WasmEdge::WasmEdge>(); }
618628

619629
} // namespace proxy_wasm

src/wasmtime/wasmtime.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class Wasmtime : public WasmVm {
103103

104104
void warm() override {}
105105

106+
// Initialize the V8 engine and store if necessary.
107+
void initStore();
108+
106109
WasmStorePtr store_;
107110
WasmModulePtr module_;
108111
WasmSharedModulePtr shared_module_;
@@ -114,9 +117,15 @@ class Wasmtime : public WasmVm {
114117
std::unordered_map<std::string, WasmFuncPtr> module_functions_;
115118
};
116119

120+
void Wasmtime::initStore() {
121+
if (store_ != nullptr) {
122+
return;
123+
}
124+
store_ = wasm_store_new(engine());
125+
117126
bool Wasmtime::load(std::string_view bytecode, std::string_view /*precompiled*/,
118127
const std::unordered_map<uint32_t, std::string> & /*function_names*/) {
119-
store_ = wasm_store_new(engine());
128+
initStore();
120129
if (store_ == nullptr) {
121130
return false;
122131
}
@@ -696,8 +705,12 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
696705
};
697706
};
698707

708+
void Wasmtime::warm() { initStore(); }
709+
699710
} // namespace wasmtime
700711

712+
bool initWasmtimeEngine() { return wasmtime::engine() != nullptr; }
713+
701714
std::unique_ptr<WasmVm> createWasmtimeVm() { return std::make_unique<wasmtime::Wasmtime>(); }
702715

703716
} // 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)