Skip to content

Commit 4fad579

Browse files
committed
adding Profiler
1 parent 63f37a2 commit 4fad579

5 files changed

Lines changed: 125 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ yarn-error.log
1717

1818
# tests/js/test.js is used for testing changes locally
1919
tests/js/test.js
20+
21+
# Profiling
22+
*.string_data
23+
*.string_index
24+
*.events
25+
chrome_profiler.json

Cargo.lock

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

boa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rustc-hash = "1.1.0"
2020

2121
# Optional Dependencies
2222
serde = { version = "1.0.106", features = ["derive"], optional = true }
23+
measureme = { version = "0.7.1" }
2324

2425
[dev-dependencies]
2526
criterion = "0.3.2"

boa/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
pub mod builtins;
3737
pub mod environment;
3838
pub mod exec;
39+
pub mod profiler;
3940
pub mod realm;
4041
pub mod syntax;
4142
use crate::{
4243
builtins::value::ResultValue,
4344
exec::{Executor, Interpreter},
45+
profiler::MyProfiler,
4446
realm::Realm,
4547
syntax::{ast::node::Node, lexer::Lexer, parser::Parser},
4648
};
@@ -76,6 +78,10 @@ pub fn forward(engine: &mut Interpreter, src: &str) -> String {
7678
/// Similar to `forward`, except the current value is returned instad of the string
7779
/// If the interpreter fails parsing an error value is returned instead (error object)
7880
pub fn forward_val(engine: &mut Interpreter, src: &str) -> ResultValue {
81+
// Setup executor
82+
let profiler = MyProfiler::new();
83+
let _timer = profiler.start_event("Main");
84+
7985
// Setup executor
8086
match parser_expr(src) {
8187
Ok(expr) => engine.run(&expr),

boa/src/profiler.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![allow(missing_copy_implementations, missing_debug_implementations)]
2+
3+
use measureme::{EventId, Profiler, TimingGuard};
4+
use std::{path::Path, thread::current};
5+
6+
/// MmapSerializatioSink is faster on macOS and Linux
7+
/// but FileSerializationSink is faster on Windows
8+
#[cfg(not(windows))]
9+
type SerializationSink = measureme::MmapSerializationSink;
10+
#[cfg(windows)]
11+
type SerializationSink = measureme::FileSerializationSink;
12+
13+
pub struct MyProfiler {
14+
profiler: Profiler<SerializationSink>,
15+
}
16+
17+
impl MyProfiler {
18+
pub fn start_event(&self, label: &str) -> TimingGuard<'_, SerializationSink> {
19+
let kind = self.profiler.alloc_string("Generic");
20+
let id = EventId::from_label(self.profiler.alloc_string(label));
21+
let thread_id = current().id().as_u64() as u32;
22+
self.profiler
23+
.start_recording_interval_event(kind, id, thread_id)
24+
}
25+
26+
pub fn new() -> MyProfiler {
27+
let profiler = Profiler::new(Path::new("./my_trace")).unwrap();
28+
MyProfiler { profiler }
29+
}
30+
}

0 commit comments

Comments
 (0)