pconvert_rust/wasm/
benchmark.rs

1//! Benchmarking functions of the WASM API exposed
2
3use crate::constants;
4use crate::wasm::utils::{encode_file, load_png, log_benchmark, log_benchmark_header};
5use crate::wasm::{blend_image_buffers, blend_multiple_buffers};
6use image::codecs::png::{CompressionType, FilterType};
7use js_sys::try_iter;
8use wasm_bindgen::prelude::*;
9use web_sys::File;
10
11/// Benchmarks the `blend_images_js` API method for all combinations of
12/// algorithms, compression and filter types.
13#[wasm_bindgen(js_name = blendImagesBenchmarkAll)]
14pub async fn blend_images_benchmark_all_js(
15    bot: File,
16    top: File,
17    is_inline: Option<bool>,
18) -> Result<(), JsValue> {
19    log_benchmark_header();
20    for algorithm in constants::ALGORITHMS.iter() {
21        for compression in constants::COMPRESSION_TYPES.iter() {
22            for filter in constants::FILTER_TYPES.iter() {
23                blend_images_benchmark_js(
24                    bot.clone(),
25                    top.clone(),
26                    "".to_string(),
27                    Some(algorithm.to_string()),
28                    is_inline,
29                    *compression,
30                    *filter,
31                )
32                .await?;
33            }
34        }
35    }
36    Ok(())
37}
38
39/// Benchmarks the `blend_multiple_js` API method for all combinations of
40/// algorithms, compression and filter types.
41#[wasm_bindgen(js_name = blendMultipleBenchmarkAll)]
42pub async fn blend_multiple_benchmark_all_js(
43    image_files: JsValue,
44    is_inline: Option<bool>,
45) -> Result<(), JsValue> {
46    log_benchmark_header();
47    for algorithm in constants::ALGORITHMS.iter() {
48        for compression in constants::COMPRESSION_TYPES.iter() {
49            for filter in constants::FILTER_TYPES.iter() {
50                blend_multiple_benchmark_js(
51                    image_files.clone(),
52                    "".to_string(),
53                    Some(algorithm.to_string()),
54                    None,
55                    is_inline,
56                    *compression,
57                    *filter,
58                )
59                .await?;
60            }
61        }
62    }
63    Ok(())
64}
65
66async fn blend_images_benchmark_js(
67    bot: File,
68    top: File,
69    target_file_name: String,
70    algorithm: Option<String>,
71    is_inline: Option<bool>,
72    compression: CompressionType,
73    filter: FilterType,
74) -> Result<File, JsValue> {
75    let start_read = js_sys::Date::now();
76
77    let mut bot = load_png(bot, false).await?;
78    let mut top = load_png(top, false).await?;
79
80    let start_blend = js_sys::Date::now();
81
82    blend_image_buffers(&mut bot, &mut top, algorithm.clone(), is_inline)?;
83
84    let start_write = js_sys::Date::now();
85
86    let file = encode_file(bot, compression, filter, target_file_name)?;
87
88    let end = js_sys::Date::now();
89
90    let read_time = start_blend - start_read;
91    let blend_time = start_write - start_blend;
92    let write_time = end - start_write;
93
94    log_benchmark(
95        algorithm.unwrap_or_else(|| "multiplicative".to_string()),
96        compression,
97        filter,
98        blend_time,
99        read_time,
100        write_time,
101    );
102
103    Ok(file)
104}
105
106async fn blend_multiple_benchmark_js(
107    image_files: JsValue,
108    target_file_name: String,
109    algorithm: Option<String>,
110    algorithms: Option<Vec<JsValue>>,
111    is_inline: Option<bool>,
112    compression: CompressionType,
113    filter: FilterType,
114) -> Result<File, JsValue> {
115    let start_read = js_sys::Date::now();
116
117    let mut image_buffers = Vec::new();
118    let image_files = try_iter(&image_files).unwrap().unwrap();
119    for file in image_files {
120        let file = file?;
121        let img = load_png(file.into(), false).await?;
122
123        image_buffers.push(img);
124    }
125
126    let start_blend = js_sys::Date::now();
127
128    let composition =
129        blend_multiple_buffers(image_buffers, algorithm.clone(), algorithms, is_inline)?;
130
131    let start_write = js_sys::Date::now();
132
133    let file = encode_file(composition, compression, filter, target_file_name)?;
134
135    let end = js_sys::Date::now();
136
137    let read_time = start_blend - start_read;
138    let blend_time = start_write - start_blend;
139    let write_time = end - start_write;
140
141    log_benchmark(
142        algorithm.unwrap_or_else(|| "multiplicative".to_string()),
143        compression,
144        filter,
145        blend_time,
146        read_time,
147        write_time,
148    );
149
150    Ok(file)
151}