Skip to content

Commit

Permalink
Merge pull request #49 from apainintheneck/add-statup-stats-command
Browse files Browse the repository at this point in the history
Add statup stats command
  • Loading branch information
apainintheneck authored Aug 24, 2024
2 parents 30f69f4 + 01a0122 commit b316b2d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ jobs:
- run: rake test:generate-api-diff

- run: rake test:service-diff

- run: rake test:startup-stats
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ running other brew commands simultaneously.
-h, --help Show this message.
```

### brew startup-stats

```
Usage: brew startup-stats [option]
Get information about the state of brew at the time a command is called. This
includes loaded constants, requires and other such information.
--defined Check if a constant is defined.
--require Diagnostics about a single require statement.
--list-requires List all requires made before this command
was run.
--list-constants List all constants loaded before this command
was run.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
-h, --help Show this message.
```

## Development

Linting and readme checks are run on each pull request while integration tests are only run weekly.
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,17 @@ namespace "test" do
end
end

task :"startup-stats" do
cmd "brew", "startup-stats", "--require", "tap"
end

task :all do
%w[
test:api-readall-test
test:branch-compare
test:generate-api-diff
test:service-diff
test:startup-stats
].each_with_index do |task, index|
puts "--------------------" if index.positive?
Rake::Task[task].invoke
Expand Down Expand Up @@ -148,7 +153,7 @@ task :"missing-tests" do
Missing integration tests for the following commands:
#{missing_test_list}
Add tests to the #{INTEGRATON_TESTS} file.
Add tests to the #{INTEGRATION_TESTS_FILE} file.
EOS
end
end
94 changes: 94 additions & 0 deletions cmd/startup-stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# frozen_string_literal: true

module Homebrew
module Cmd
class StartupStats < AbstractCommand
cmd_args do
usage_banner "`startup-stats` [<option>]"
description <<~EOS
Get information about the state of brew at the time a command is called.
This includes loaded constants, requires and other such information.
EOS

flag "--defined=", description: "Check if a constant is defined."
flag "--require=", description: "Diagnostics about a single require statement."
switch "--list-requires", description: "List all requires made before this command was run."
switch "--list-constants", description: "List all constants loaded before this command was run."

%w[
--defined
--require
--list-requires
--list-constants
].combination(2) do |combo|
conflicts(*combo)
end
end

def run
if args.defined
puts Object.const_defined?(args.defined)
elsif args.require
require_diagnostics
elsif args.list_requires?
puts require_list
elsif args.list_constants?
puts constant_list
else
odie "No options provided to the command!"
end
end

def require_list
$LOADED_FEATURES.sort
end

def constant_list
Object.constants.sort
end

def require_diagnostics
odie "'brew' cannot be required directly." if args.require == "brew"

begin
before_require_list = require_list
before_constant_list = constant_list

hash = benchmark { require args.require }
hash => { result:, elapsed_seconds: }

new_require_list = require_list - before_require_list
new_constant_list = constant_list - before_constant_list

if result
oh1 "require '#{args.require}'"
ohai "elapsed seconds"
puts "- #{elapsed_seconds}"
if new_require_list.any?
puts
ohai "new requires"
new_require_list.each { puts "- #{_1}" }
end
if new_constant_list.any?
puts
ohai "new constants"
new_constant_list.each { puts "- #{_1}" }
end
else
odie "'#{args.require}' has already been required."
end
rescue LoadError => e
odie e.to_s
end
end

def benchmark
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = yield
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed_seconds = end_time - start_time
{ result:, elapsed_seconds: }
end
end
end
end

0 comments on commit b316b2d

Please sign in to comment.