-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Because the ensure calls `_stop` instead of `stop`, the block form produces invalid json * Perfetto is pretty permissive about this and still accepts the file - but other tools break and JSON parsing is broken * Also adds first specs for the project, covering a few different scenarios 🎉
- Loading branch information
Showing
9 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ gems.locked | |
*.bundle | ||
example*json | ||
thread_name_with_extension.json | ||
.DS_Store | ||
.DS_Store | ||
.rspec_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
ruby-3.2.0 | ||
ruby-3.2.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ def start(file) | |
begin | ||
yield | ||
ensure | ||
_stop | ||
stop | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require "json" | ||
require "net/http" | ||
require "perfetto_trace" | ||
|
||
RSpec.describe GvlTracing do | ||
let(:trace_path) { "tmp/gvl.json" } | ||
|
||
it "has a version number" do | ||
expect(GvlTracing::VERSION).not_to be nil | ||
end | ||
|
||
describe "#start" do | ||
it "fails if already started" do | ||
expect { | ||
GvlTracing.start(trace_path) { GvlTracing.start(trace_path) } | ||
}.to raise_error(/Already started/) | ||
end | ||
|
||
describe "with a block" do | ||
before do | ||
GvlTracing.start(trace_path) {} | ||
end | ||
|
||
it "stops tracing after the block" do | ||
expect(File.exist?(trace_path)).to be_truthy | ||
end | ||
|
||
it "creates valid json" do | ||
expect { JSON.parse(File.read(trace_path)) }.to_not raise_error | ||
end | ||
end | ||
end | ||
|
||
describe "#stop" do | ||
it "fails if not started" do | ||
expect { GvlTracing.stop }.to raise_error(/Tracing not running/) | ||
end | ||
|
||
it "closes out the json" do | ||
GvlTracing.start(trace_path) | ||
expect { JSON.parse(File.read(trace_path)) }.to raise_error(JSON::ParserError) | ||
|
||
GvlTracing.stop | ||
expect(JSON.parse(File.read(trace_path))).to be_an(Array) | ||
end | ||
end | ||
|
||
describe "order of events" do | ||
it "writes events in valid order" do | ||
GvlTracing.start(trace_path) do | ||
Thread.new {}.join | ||
Thread.new {}.join | ||
end | ||
|
||
trace = PerfettoTrace.new(trace_path) | ||
threads = trace.non_main_threads | ||
|
||
threads.each do |thread| | ||
events = trace.events_by_thread[thread.tid] | ||
names = events.map(&:name).reject { |n| !n } | ||
|
||
expect(names).to match_array(%w[thread_name running wants_gvl waiting died]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
class PerfettoTrace | ||
def initialize(file_path) | ||
@trace = JSON.parse(File.read(file_path)) | ||
end | ||
|
||
def threads | ||
@trace | ||
.select { |j| j["ph"] == "M" && j["name"] == "thread_name" } | ||
.uniq { |j| j["args"]["tid"] } | ||
.map { |j| Row.new(j) } | ||
end | ||
|
||
def non_main_threads | ||
threads.select { |t| t.thread_name != "Main Thread" } | ||
end | ||
|
||
def events_by_thread | ||
@trace | ||
.group_by { |j| j["tid"] } | ||
.map { |tid, events| [tid, events.map { |j| Row.new(j) }] } | ||
.to_h | ||
end | ||
|
||
class Row | ||
def initialize(row) | ||
@row = row | ||
end | ||
|
||
def meta? = ph == "M" | ||
|
||
def phase_begin? = ph == "B" | ||
|
||
def phase_end? = ph == "E" | ||
|
||
def phase = @row["ph"] | ||
|
||
def pid = @row["pid"] | ||
|
||
def tid = @row["tid"] | ||
|
||
def ts = @row["ts"] | ||
|
||
def name = @row["name"] | ||
|
||
def args = @row["args"] | ||
|
||
def thread_name = @row.dig("args", "name") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "gvl-tracing" | ||
require "pry" | ||
require "pry-byebug" | ||
|
||
RSpec.configure do |config| | ||
config.example_status_persistence_file_path = ".rspec_status" | ||
|
||
config.disable_monkey_patching! | ||
|
||
config.expect_with :rspec do |c| | ||
c.syntax = :expect | ||
end | ||
end |