Skip to content

Commit

Permalink
profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonBurke committed May 30, 2024
1 parent 69f86f3 commit 6a2d7a5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
55 changes: 47 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ console_error_panic_hook = "0.1.7"
qcell = "0.5.4"
rand = "0.8.5"
getrandom = { version = "0.2.15", features = ["js"] }
screeps-timing = { git = "https://github.com/Azaril/screeps-timing", optional = true }
screeps-timing-annotate = { git = "https://github.com/Azaril/screeps-timing-annotate", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3"
Expand All @@ -46,6 +48,7 @@ wasm-opt = ["-O4", "--disable-sign-ext"]
#wasm-opt = ["-g", "--disable-sign-ext"]

[features]
default = []
default = ["profile"]
profile = ["screeps-timing", "screeps-timing-annotate"]

sim = ["screeps-game-api/sim"]
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ pub const MEMORY_VERSION: u8 = 1;
static INIT_LOGGING: std::sync::Once = std::sync::Once::new();

#[wasm_bindgen(js_name = loop)]
#[cfg_attr(feature = "profile", screeps_timing_annotate::timing)]
pub fn game_loop() {
#[cfg(feature = "profile")]
{
screeps_timing::start_trace(Box::new(|| {
(screeps::game::cpu::get_used() * 1000.0) as u64
}));
}

INIT_LOGGING.call_once(|| {
// show all output of Info level, adjust as needed
logging::setup_logging(logging::Info);
Expand Down Expand Up @@ -63,6 +71,15 @@ pub fn game_loop() {
// This is done like this because its basically MemHack for you JS people.
memory.write_memory();

#[cfg(feature = "profile")]
{
let trace = screeps_timing::stop_trace();

if let Some(trace_output) = serde_json::to_string(&trace).ok() {
info!("{}", trace_output);
}
}

let heap = game::cpu::get_heap_statistics();
let used = (heap.total_heap_size() / heap.heap_size_limit()) * 100;

Expand Down

0 comments on commit 6a2d7a5

Please sign in to comment.