Skip to content

Commit 7bad453

Browse files
committed
Add tests and benchmarks for map (de)serialization.
Cast usize to u32 to allow running them on 64-bit systems. Signed-off-by: Piotr Sikora <[email protected]>
1 parent 6d88ed5 commit 7bad453

File tree

5 files changed

+152
-6
lines changed

5 files changed

+152
-6
lines changed

.github/workflows/rust.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ jobs:
148148
- name: Clippy (wasm32-wasi)
149149
run: cargo clippy --release --all-targets --target=wasm32-wasi
150150

151+
- name: Test
152+
run: cargo test
153+
151154
- name: Format (rustfmt)
152155
run: cargo fmt -- --check
153156

@@ -210,6 +213,9 @@ jobs:
210213
- name: Clippy (wasm32-wasip1)
211214
run: cargo clippy --release --all-targets --target=wasm32-wasip1
212215

216+
- name: Test
217+
run: cargo test
218+
213219
- name: Format (rustfmt)
214220
run: cargo fmt -- --check
215221

@@ -273,6 +279,12 @@ jobs:
273279
- name: Clippy (wasm32-wasip1)
274280
run: cargo clippy --release --all-targets --target=wasm32-wasip1
275281

282+
- name: Test
283+
run: cargo test
284+
285+
- name: Bench
286+
run: cargo bench
287+
276288
- name: Format (rustfmt)
277289
run: cargo fmt -- --check
278290

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ opt-level = 3
2020
codegen-units = 1
2121
panic = "abort"
2222
strip = "debuginfo"
23+
24+
[profile.test]
25+
inherits = "release"
26+
debug = true
27+
28+
[profile.bench]
29+
inherits = "release"
30+
debug = true

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
fn main() {
1616
println!("cargo:rerun-if-changed=build.rs");
1717
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
18+
println!("cargo:rustc-check-cfg=cfg(nightly)");
1819
println!("cargo:rustc-check-cfg=cfg(wasi_exec_model_reactor)");
1920

21+
if let Some(toolchain) = std::env::var_os("RUSTUP_TOOLCHAIN") {
22+
if toolchain.to_string_lossy().contains("nightly") {
23+
println!("cargo:rustc-cfg=nightly");
24+
}
25+
}
26+
2027
if let Some(target_os) = std::env::var_os("CARGO_CFG_TARGET_OS") {
2128
if target_os != "wasi" {
2229
return;

src/hostcalls.rs

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,10 @@ mod utils {
11651165
size += name.len() + value.len() + 10;
11661166
}
11671167
let mut bytes: Bytes = Vec::with_capacity(size);
1168-
bytes.extend_from_slice(&map.len().to_le_bytes());
1168+
bytes.extend_from_slice(&(map.len() as u32).to_le_bytes());
11691169
for (name, value) in &map {
1170-
bytes.extend_from_slice(&name.len().to_le_bytes());
1171-
bytes.extend_from_slice(&value.len().to_le_bytes());
1170+
bytes.extend_from_slice(&(name.len() as u32).to_le_bytes());
1171+
bytes.extend_from_slice(&(value.len() as u32).to_le_bytes());
11721172
}
11731173
for (name, value) in &map {
11741174
bytes.extend_from_slice(name.as_bytes());
@@ -1185,10 +1185,10 @@ mod utils {
11851185
size += name.len() + value.len() + 10;
11861186
}
11871187
let mut bytes: Bytes = Vec::with_capacity(size);
1188-
bytes.extend_from_slice(&map.len().to_le_bytes());
1188+
bytes.extend_from_slice(&(map.len() as u32).to_le_bytes());
11891189
for (name, value) in &map {
1190-
bytes.extend_from_slice(&name.len().to_le_bytes());
1191-
bytes.extend_from_slice(&value.len().to_le_bytes());
1190+
bytes.extend_from_slice(&(name.len() as u32).to_le_bytes());
1191+
bytes.extend_from_slice(&(value.len() as u32).to_le_bytes());
11921192
}
11931193
for (name, value) in &map {
11941194
bytes.extend_from_slice(name.as_bytes());
@@ -1243,4 +1243,118 @@ mod utils {
12431243
}
12441244
map
12451245
}
1246+
1247+
#[cfg(test)]
1248+
mod tests {
1249+
use super::*;
1250+
1251+
#[cfg(nightly)]
1252+
use test::Bencher;
1253+
1254+
static MAP: &[(&str, &str)] = &[
1255+
(":method", "GET"),
1256+
(":path", "/bytes/1"),
1257+
(":authority", "httpbin.org"),
1258+
("Powered-By", "proxy-wasm"),
1259+
];
1260+
1261+
#[rustfmt::skip]
1262+
static SERIALIZED_MAP: &[u8] = &[
1263+
// num entries
1264+
4, 0, 0, 0,
1265+
// len (":method", "GET")
1266+
7, 0, 0, 0, 3, 0, 0, 0,
1267+
// len (":path", "/bytes/1")
1268+
5, 0, 0, 0, 8, 0, 0, 0,
1269+
// len (":authority", "httpbin.org")
1270+
10, 0, 0, 0, 11, 0, 0, 0,
1271+
// len ("Powered-By", "proxy-wasm")
1272+
10, 0, 0, 0, 10, 0, 0, 0,
1273+
// ":method"
1274+
58, 109, 101, 116, 104, 111, 100, 0,
1275+
// "GET"
1276+
71, 69, 84, 0,
1277+
// ":path"
1278+
58, 112, 97, 116, 104, 0,
1279+
// "/bytes/1"
1280+
47, 98, 121, 116, 101, 115, 47, 49, 0,
1281+
// ":authority"
1282+
58, 97, 117, 116, 104, 111, 114, 105, 116, 121, 0,
1283+
// "httpbin.org"
1284+
104, 116, 116, 112, 98, 105, 110, 46, 111, 114, 103, 0,
1285+
// "Powered-By"
1286+
80, 111, 119, 101, 114, 101, 100, 45, 66, 121, 0,
1287+
// "proxy-wasm"
1288+
112, 114, 111, 120, 121, 45, 119, 97, 115, 109, 0,
1289+
];
1290+
1291+
#[test]
1292+
fn test_serialize_map() {
1293+
let serialized_map = serialize_map(MAP.to_vec());
1294+
assert_eq!(serialized_map, SERIALIZED_MAP);
1295+
}
1296+
1297+
#[test]
1298+
fn test_serialize_map_bytes() {
1299+
let map: Vec<(&str, &[u8])> = MAP.iter().map(|x| (x.0, x.1.as_bytes())).collect();
1300+
let serialized_map = serialize_map_bytes(map);
1301+
assert_eq!(serialized_map, SERIALIZED_MAP);
1302+
}
1303+
1304+
#[test]
1305+
fn test_deserialize_map() {
1306+
let map = deserialize_map(SERIALIZED_MAP);
1307+
assert_eq!(map.len(), MAP.len());
1308+
for (got, expected) in map.into_iter().zip(MAP) {
1309+
assert_eq!(got.0, expected.0);
1310+
assert_eq!(got.1, expected.1);
1311+
}
1312+
}
1313+
1314+
#[test]
1315+
fn test_deserialize_map_bytes() {
1316+
let map = deserialize_map_bytes(SERIALIZED_MAP);
1317+
assert_eq!(map.len(), MAP.len());
1318+
for (got, expected) in map.into_iter().zip(MAP) {
1319+
assert_eq!(got.0, expected.0);
1320+
assert_eq!(got.1, expected.1.as_bytes());
1321+
}
1322+
}
1323+
1324+
#[cfg(nightly)]
1325+
#[bench]
1326+
fn bench_serialize_map(b: &mut Bencher) {
1327+
let map = MAP.to_vec();
1328+
b.iter(|| {
1329+
serialize_map(test::black_box(map.clone()));
1330+
});
1331+
}
1332+
1333+
#[cfg(nightly)]
1334+
#[bench]
1335+
fn bench_serialize_map_bytes(b: &mut Bencher) {
1336+
let map: Vec<(&str, &[u8])> = MAP.iter().map(|x| (x.0, x.1.as_bytes())).collect();
1337+
b.iter(|| {
1338+
serialize_map_bytes(test::black_box(map.clone()));
1339+
});
1340+
}
1341+
1342+
#[cfg(nightly)]
1343+
#[bench]
1344+
fn bench_deserialize_map(b: &mut Bencher) {
1345+
let serialized_map = SERIALIZED_MAP.to_vec();
1346+
b.iter(|| {
1347+
deserialize_map(test::black_box(&serialized_map));
1348+
});
1349+
}
1350+
1351+
#[cfg(nightly)]
1352+
#[bench]
1353+
fn bench_deserialize_map_bytes(b: &mut Bencher) {
1354+
let serialized_map = SERIALIZED_MAP.to_vec();
1355+
b.iter(|| {
1356+
deserialize_map_bytes(test::black_box(&serialized_map));
1357+
});
1358+
}
1359+
}
12461360
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#![cfg_attr(all(test, nightly), feature(test))]
16+
17+
#[cfg(all(test, nightly))]
18+
extern crate test;
19+
1520
pub mod hostcalls;
1621
pub mod traits;
1722
pub mod types;

0 commit comments

Comments
 (0)