-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local firefox viewer #144
Merged
Merged
Local firefox viewer #144
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module AppProfiler | ||
module Exec # :nodoc: | ||
protected | ||
|
||
def valid_commands | ||
raise NotImplementedError | ||
end | ||
|
||
def ensure_command_valid(command) | ||
unless valid_command?(command) | ||
raise ArgumentError, "Illegal command: #{command.join(" ")}." | ||
end | ||
end | ||
|
||
def valid_command?(command) | ||
valid_commands.any? do |valid_command| | ||
next unless valid_command.size == command.size | ||
|
||
valid_command.zip(command).all? do |valid_part, part| | ||
part.match?(valid_part) | ||
end | ||
end | ||
end | ||
|
||
def exec(*command, silent: false, environment: {}) | ||
ensure_command_valid(command) | ||
|
||
if silent | ||
system(environment, *command, out: File::NULL).tap { |return_code| yield unless return_code } | ||
else | ||
system(environment, *command).tap { |return_code| yield unless return_code } | ||
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,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require "app_profiler/exec" | ||
|
||
module AppProfiler | ||
module Viewer | ||
class FirefoxViewer < BaseViewer | ||
include Exec | ||
|
||
CHILD_PIDS = [] | ||
|
||
at_exit { Process.wait if CHILD_PIDS.any? } | ||
|
||
trap("INT") do | ||
CHILD_PIDS.each { |pid| Process.kill("INT", pid) } | ||
sleep(0.5) | ||
end | ||
|
||
class ProfileViewerError < StandardError; end | ||
|
||
VALID_COMMANDS = [ | ||
["which", "profile-viewer"], | ||
["gem", "install", "profile-viewer"], | ||
["profile-viewer", /.*\.json/], | ||
] | ||
private_constant(:VALID_COMMANDS) | ||
|
||
class << self | ||
def view(profile, params = {}) | ||
new(profile).view(**params) | ||
end | ||
end | ||
|
||
def valid_commands | ||
VALID_COMMANDS | ||
end | ||
|
||
def initialize(profile) | ||
super() | ||
@profile = profile | ||
end | ||
|
||
def view(_params = {}) | ||
profile_viewer(@profile.file.to_s) | ||
end | ||
|
||
private | ||
|
||
def setup_profile_viewer | ||
exec("which", "profile-viewer", silent: true) do | ||
gem_install("profile_viewer") | ||
end | ||
@profile_viewer_initialized = true | ||
end | ||
|
||
def profile_viewer_setup | ||
@profile_viewer_initialized || false | ||
end | ||
|
||
def gem_install(gem) | ||
exec("gem", "install", gem) do | ||
raise ProfileViewerError, "Failed to run gem install #{gem}." | ||
end | ||
end | ||
|
||
def profile_viewer(path) | ||
setup_profile_viewer unless profile_viewer_setup | ||
|
||
CHILD_PIDS << fork do | ||
Bundler.with_unbundled_env do | ||
exec("profile-viewer", path) do | ||
raise ProfileViewerError, "Failed to run profile-viewer." | ||
end | ||
end | ||
end | ||
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for a future PR, i wonder if we could just use the gem and extract the sources from that. This branch is out of date and doesn't have the latest changes already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I'll try to account for this in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We make changes to the source so I don't think extracting from the gem is the best idea, unless we plan to copy it somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes are basically just so we can compile it as a package IIRC. We might be able to get away with just extracting the sources from the gem as-is?