Skip to content

Commit

Permalink
test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Jul 27, 2024
1 parent 87ff548 commit c94c8a6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 39 deletions.
7 changes: 5 additions & 2 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,18 @@ fn large_file_read() {
let filename = "test.txt";

// create large file
fns::append_text(&pic, "t1.txt", "abcdef7890", 10_000_000);
fns::append_text(&pic, "t2.txt", "abcdef7890", 10_000_000);
fns::append_text(&pic, "t3.txt", "abcdef7890", 10_000_000);
fns::append_text(&pic, "t4.txt", "abcdef7890", 10_000_000);
fns::append_text(&pic, filename, "abcdef7890", 10_000_000);

let (instructions, size) = fns::read_bytes(&pic, filename, 13, 100_000_000);

println!("instructions {instructions}, size {size}");

assert!(instructions < 4_000_000_000, "The call should take less than 8 billion instructions");
assert!(instructions < 3_000_000_000, "The call should take less than 3 billion instructions");

assert_eq!(size, 99_999_987);


}
92 changes: 60 additions & 32 deletions src/runtime/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,25 @@ impl File {
storage: &mut dyn Storage,
) -> Result<FileSize, Error> {

self.read_with_offset_range(offset, buf, storage)
/*
if buf.is_empty() {
return Ok(0 as FileSize);
}

let file_size = storage.get_metadata(self.node)?.size;
let end = (offset + buf.len() as FileSize).min(file_size);
let chunk_infos = get_chunk_infos(offset, end);
let mut read_size = 0;

let read_size = storage.read_range(
self.node,
offset,
file_size,
buf,
)?;

for chunk in chunk_infos.into_iter() {
storage.read_filechunk(
self.node,
chunk.index,
chunk.offset,
&mut buf[read_size..read_size + chunk.len as usize],
)?;
read_size += chunk.len as usize;
}
Ok(read_size as FileSize)
*/

}

// Read file at the current file cursor, the cursor position will NOT be updated after reading.
fn read_with_offset_range(
// Read file by chunks at the current file cursor, the cursor position will NOT be updated after reading.
fn read_with_offset_chunk(
&self,
offset: FileSize,
buf: &mut [u8],
Expand All @@ -146,14 +137,20 @@ impl File {
}

let file_size = storage.get_metadata(self.node)?.size;

let read_size = storage.read_range(
self.node,
offset,
file_size,
buf,
)?;
let end = (offset + buf.len() as FileSize).min(file_size);
let chunk_infos = get_chunk_infos(offset, end);

let mut read_size = 0;

for chunk in chunk_infos.into_iter() {
storage.read_filechunk(
self.node,
chunk.index,
chunk.offset,
&mut buf[read_size..read_size + chunk.len as usize],
)?;
read_size += chunk.len as usize;
}
Ok(read_size as FileSize)
}

Expand Down Expand Up @@ -376,7 +373,38 @@ mod tests {
}

#[test]
fn read_and_write_offset_range() {
fn read_and_write_small_and_big_buffer() {
let mut fs = test_fs();
let fd = fs
.create_file(fs.root_fd(), "test", FdStat::default(), 0)
.unwrap();

let file = fs.get_test_file(fd);
let storage = fs.get_test_storage();

for i in 0..1000 {
let buf = [(i % 256) as u8; 10];
file.write_with_offset(i * 16, &buf, storage).unwrap();
}

for i in 0..1000 {
let mut buf1 = [0; 13];
let mut buf2 = [0; 5000];
let mut buf3 = [0; 15000];

let r1 = file.read_with_offset_chunk(i * 17, &mut buf1, storage).unwrap() as usize;
let r2 = file.read_with_offset_chunk(i * 17, &mut buf2, storage).unwrap() as usize;
let _r3 = file.read_with_offset_chunk(i * 17, &mut buf3, storage).unwrap() as usize;

assert_eq!(buf1[..r1], buf2[..r1]);
assert_eq!(buf2[..r2], buf3[..r2]);
}


}

#[test]
fn read_and_write_offset_chunk() {
let mut fs = test_fs();
let fd = fs
.create_file(fs.root_fd(), "test", FdStat::default(), 0)
Expand All @@ -394,7 +422,7 @@ mod tests {

for i in 0..1000 {
let mut buf = [0; 16];
file.read_with_offset_range(i * 16, &mut buf, storage).unwrap();
file.read_with_offset_chunk(i * 16, &mut buf, storage).unwrap();

let expected = [(i % 256) as u8; 16];
assert_eq!(buf, expected);
Expand All @@ -418,7 +446,7 @@ mod tests {

for i in 0..1000 {
let mut buf1 = [0; 13];
let len1 = file.read_with_offset_range(i * 16, &mut buf1, storage).unwrap();
let len1 = file.read_with_offset_chunk(i * 16, &mut buf1, storage).unwrap();

let mut buf2 = [0; 13];
let len2 = file.read_with_offset(i * 16, &mut buf2, storage).unwrap();
Expand All @@ -429,7 +457,7 @@ mod tests {

for i in 0..2050 {
let mut buf1 = [0; 5003];
let len1 = file.read_with_offset_range(i * 13, &mut buf1, storage).unwrap();
let len1 = file.read_with_offset_chunk(i * 13, &mut buf1, storage).unwrap();

let mut buf2 = [0; 5003];
let len2 = file.read_with_offset(i * 13, &mut buf2, storage).unwrap();
Expand Down Expand Up @@ -457,7 +485,7 @@ mod tests {

for i in 0..1000 {
let mut buf1 = [0; 13];
let len1 = file.read_with_offset_range(i * 16, &mut buf1, storage).unwrap();
let len1 = file.read_with_offset_chunk(i * 16, &mut buf1, storage).unwrap();

let mut buf2 = [0; 13];
let len2 = file.read_with_offset(i * 16, &mut buf2, storage).unwrap();
Expand All @@ -468,7 +496,7 @@ mod tests {

for i in 0..2050 {
let mut buf1 = [0; 5003];
let len1 = file.read_with_offset_range(i * 13, &mut buf1, storage).unwrap();
let len1 = file.read_with_offset_chunk(i * 13, &mut buf1, storage).unwrap();

let mut buf2 = [0; 5003];
let len2 = file.read_with_offset(i * 13, &mut buf2, storage).unwrap();
Expand Down
57 changes: 52 additions & 5 deletions tests/perf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,66 +41,115 @@ let rs_config = record { start_page = 1; page_limit = 1128};

let wasm_name = "fs_benchmark_test/target/wasm32-unknown-unknown/release/fs_benchmark_test_backend_small.wasm";

let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

function perf_file_write_10mib() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.write_mib_text( "files/test.txt", (10: nat64) );
flamegraph(cid, "perf_file_write_10mib", "svg/perf_file_write_10mib.svg");
uninstall(cid)
};

function perf_append_text_10kib() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.append_text( "test.txt", "some_text_", (1024 : nat64) );
flamegraph(cid, "perf_append_text_10kib", "svg/perf_append_text_10kib.svg");
uninstall(cid)
};

function perf_append_text_10kib_deep_folder_structure() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.append_text( "d0/d1/d2/d3/d4/d5/d6/d7/d8/d9/d10/d11/d12/d13/d14/d15/d16/d17/d18/d19/d20/d21/d22/d23/d24/d25/d26/d27/d28/d29/d30/d31/d32/d33/d34/d35/d36/d37/d38/d39/d40/d41/d42/d43/d44/d45/d46/d47/d48/d49/d50/d51/d52/d53/d54/d55/d56/d57/d58/d59/d60/d61/d62/d63/d64/d65/d66/d67/d68/d69/d70/d71/d72/d73/d74/d75/d76/d77/d78/d79/d80/d81/d82/d83/d84/d85/d86/d87/d88/d89/d90/d91/d92/d93/d94/d95/d96/d97/d98/d99/test.txt", "some_text_", (1024 : nat64) );
flamegraph(cid, "perf_append_text_10kib_deep_folder_structure", "svg/perf_append_text_10kib_deep_folder_structure.svg");
uninstall(cid)
};

function perf_create_files() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.create_files( "files", (100: nat64) );
flamegraph(cid, "perf_create_files", "svg/perf_create_files.svg");
uninstall(cid)
};

function perf_create_folders() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.create_depth_folders("files", (100: nat64));
flamegraph(cid, "create_depth_folders", "svg/create_depth_folders.svg");
uninstall(cid)
};

function perf_list_files() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.list_files("files");
flamegraph(cid, "perf_list_files", "svg/perf_list_files.svg");

uninstall(cid)
};

function perf_delete_files() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.delete_file("files/0.txt");
flamegraph(cid, "delete_files", "svg/delete_files.svg");

uninstall(cid)
};

function perf_delete_folders() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

call cid.delete_folder("files/d0/d1/d2/d3/d4/d5/d6/d7/d8/d9/d10/d11/d12/d13/d14/d15/d16/d17/d18/d19/d20/d21/d22/d23/d24/d25/d26/d27/d28/d29/d30/d31/d32/d33/d34/d35/d36/d37/d38/d39/d40/d41/d42/d43/d44/d45/d46/d47/d48/d49/d50/d51/d52/d53/d54/d55/d56/d57/d58/d59/d60/d61/d62/d63/d64/d65/d66/d67/d68/d69/d70/d71/d72/d73/d74/d75/d76/d77/d78/d79/d80/d81/d82/d83/d84/d85/d86/d87/d88/d89/d90/d91/d92/d93/d94/d95/d96/d97/d98/d99");

flamegraph(cid, "delete_folders", "svg/delete_folders.svg");

uninstall(cid)
};


function perf_file_write_100mib() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

// stop trace
call cid.__toggle_tracing();

call cid.append_chunk("test.txt", "1234567890abcdefghij", (10: nat64) );
call cid.append_buffer("test.txt", "1234567890abcdefghij", (10: nat64) );

// start trace
call cid.__toggle_tracing();

flamegraph(cid, "perf_file_write_10mib", "svg/perf_file_write_10mib.svg");

uninstall(cid)
};

function perf_file_read_100mb() {
let cid = install(wasm_profiling(wasm_name, rs_config), encode (), null);

// stop trace
call cid.__toggle_tracing();

call cid.append_buffer("1234567890", (10_000_000: nat64) );

call cid.store_buffer("test.txt");

// start trace
call cid.__toggle_tracing();

call cid.read_bytes("test.txt", (0: int64), (100_000_000: nat64) );

flamegraph(cid, "perf_file_read_100mb", "svg/perf_file_read_100mb.svg");

uninstall(cid)
};


/// files
perf_file_read_100mb();

//perf_file_write_10mib();

//perf_append_text_10kib();
Expand All @@ -116,8 +165,6 @@ function perf_file_write_100mib() {
//perf_create_folders();
//perf_delete_folders();

uninstall(cid);

//call cid.__toggle_tracing();
//call cid.list_files("files");
//call cid.__toggle_tracing();
Expand Down

0 comments on commit c94c8a6

Please sign in to comment.