Compare commits

...

5 Commits

Author SHA1 Message Date
zach
e15b8c9019 fix: remove remaining_fuel 2024-12-16 10:46:15 -08:00
zach
1802ccb2ac cleanup: simplify fuel_consumed implementation 2024-12-16 09:58:10 -08:00
pwnintended
2f70dc42a3 chore: fixed clippy error 2024-12-16 13:35:13 +01:00
pwnintended
8247be78e6 chore: fixed formatter errors 2024-12-16 13:30:27 +01:00
pwnintended
e72480bfa3 feat: added a function to track fuel consumption 2024-12-16 12:59:37 +01:00
2 changed files with 36 additions and 0 deletions

View File

@@ -1155,6 +1155,25 @@ impl Plugin {
anyhow::bail!("Plugin::clear_error failed, extism:host/env::error_set not found")
}
}
/// Returns the amount of fuel consumed by the plugin.
///
/// This function calculates the difference between the initial fuel and the remaining fuel.
/// If either the initial fuel or the remaining fuel is not set, it returns `None`.
///
/// # Returns
///
/// * `Some(u64)` - The amount of fuel consumed.
/// * `None` - If the initial fuel or remaining fuel is not set.
pub fn fuel_consumed(&self) -> Option<u64> {
self.fuel.map(|x| {
x.saturating_sub(
self.store
.get_fuel()
.expect("fuel support should be enabled to use fuel"),
)
})
}
}
// Enumerates the PDK languages that need some additional initialization

View File

@@ -258,6 +258,23 @@ fn test_fuel() {
}
}
#[test]
fn test_fuel_consumption() {
let manifest = Manifest::new([extism_manifest::Wasm::data(WASM_LOOP)]);
let mut plugin = PluginBuilder::new(manifest)
.with_wasi(true)
.with_fuel_limit(10000)
.build()
.unwrap();
let output: Result<&[u8], Error> = plugin.call("loop_forever", "abc123");
assert!(output.is_err());
let fuel_consumed = plugin.fuel_consumed().unwrap();
println!("Fuel consumed: {}", fuel_consumed);
assert!(fuel_consumed > 0);
}
#[test]
#[cfg(feature = "http")]
fn test_http_timeout() {