Skip to content

Commit

Permalink
adding Profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams committed May 17, 2020
1 parent 63f37a2 commit 4fad579
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ yarn-error.log

# tests/js/test.js is used for testing changes locally
tests/js/test.js

# Profiling
*.string_data
*.string_index
*.events
chrome_profiler.json
82 changes: 82 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rustc-hash = "1.1.0"

# Optional Dependencies
serde = { version = "1.0.106", features = ["derive"], optional = true }
measureme = { version = "0.7.1" }

[dev-dependencies]
criterion = "0.3.2"
Expand Down
6 changes: 6 additions & 0 deletions boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
pub mod builtins;
pub mod environment;
pub mod exec;
pub mod profiler;
pub mod realm;
pub mod syntax;
use crate::{
builtins::value::ResultValue,
exec::{Executor, Interpreter},
profiler::MyProfiler,
realm::Realm,
syntax::{ast::node::Node, lexer::Lexer, parser::Parser},
};
Expand Down Expand Up @@ -76,6 +78,10 @@ pub fn forward(engine: &mut Interpreter, src: &str) -> String {
/// Similar to `forward`, except the current value is returned instad of the string
/// If the interpreter fails parsing an error value is returned instead (error object)
pub fn forward_val(engine: &mut Interpreter, src: &str) -> ResultValue {
// Setup executor
let profiler = MyProfiler::new();
let _timer = profiler.start_event("Main");

// Setup executor
match parser_expr(src) {
Ok(expr) => engine.run(&expr),
Expand Down
30 changes: 30 additions & 0 deletions boa/src/profiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![allow(missing_copy_implementations, missing_debug_implementations)]

use measureme::{EventId, Profiler, TimingGuard};
use std::{path::Path, thread::current};

/// MmapSerializatioSink is faster on macOS and Linux
/// but FileSerializationSink is faster on Windows
#[cfg(not(windows))]
type SerializationSink = measureme::MmapSerializationSink;
#[cfg(windows)]
type SerializationSink = measureme::FileSerializationSink;

pub struct MyProfiler {
profiler: Profiler<SerializationSink>,
}

impl MyProfiler {
pub fn start_event(&self, label: &str) -> TimingGuard<'_, SerializationSink> {
let kind = self.profiler.alloc_string("Generic");
let id = EventId::from_label(self.profiler.alloc_string(label));
let thread_id = current().id().as_u64() as u32;
self.profiler
.start_recording_interval_event(kind, id, thread_id)
}

pub fn new() -> MyProfiler {
let profiler = Profiler::new(Path::new("./my_trace")).unwrap();
MyProfiler { profiler }
}
}

0 comments on commit 4fad579

Please sign in to comment.