From 44681e97da0868461993a92584afe04d53c4ca42 Mon Sep 17 00:00:00 2001 From: James Dabbs Date: Fri, 5 Jun 2020 07:15:54 -0700 Subject: [PATCH 1/8] Add initial support for custom event tracking --- README.md | 10 ++++++++ lib/rspec_profiling/collectors/csv.rb | 34 ++++++++++++++++++++------ lib/rspec_profiling/config.rb | 3 ++- lib/rspec_profiling/example.rb | 11 ++++++++- lib/rspec_profiling/run.rb | 20 ++++++++++++--- rspec_profiling | Bin 12288 -> 12288 bytes rspec_profiling.gemspec | 1 + spec/run_spec.rb | 18 ++++++++++++-- 8 files changed, 83 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b7652ed..464db9a 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,16 @@ RspecProfiling.configure do |config| end ``` +#### Custom Event Subscriptions + +```Ruby +RspecProfiling.configure do |config| + config.events = %w[event1 event2] +end +``` + +Note that custom events are only currenly reported by the CSV collector. + ### Choose a results collector Results are collected just by running the specs. diff --git a/lib/rspec_profiling/collectors/csv.rb b/lib/rspec_profiling/collectors/csv.rb index ca7cc95..1bbeed6 100644 --- a/lib/rspec_profiling/collectors/csv.rb +++ b/lib/rspec_profiling/collectors/csv.rb @@ -31,24 +31,44 @@ def self.reset # no op end - def initialize - RspecProfiling.config.csv_path ||= 'tmp/spec_benchmarks.csv' + def initialize(config=RspecProfiling.config) + config.csv_path ||= 'tmp/spec_benchmarks.csv' + + @config = config end def insert(attributes) - output << HEADERS.map do |field| - attributes.fetch(field.to_sym) - end + output << static_cells(attributes) + event_cells(attributes) end private + attr_reader :config + def output - @output ||= ::CSV.open(path, "w").tap { |csv| csv << HEADERS } + @output ||= ::CSV.open(path, "w").tap { |csv| csv << HEADERS + event_headers } end def path - RspecProfiling.config.csv_path.call + config.csv_path.call + end + + def static_cells(attributes) + HEADERS.map do |field| + attributes.fetch(field.to_sym) + end + end + + def event_headers + config.events.flat_map do |event| + ["#{event}_count", "#{event}_time"] + end + end + + def event_cells(attributes) + config.events.flat_map do |event| + [attributes[:event_counts][event], attributes[:event_times][event]] + end end end end diff --git a/lib/rspec_profiling/config.rb b/lib/rspec_profiling/config.rb index 2293be9..6b05c94 100644 --- a/lib/rspec_profiling/config.rb +++ b/lib/rspec_profiling/config.rb @@ -7,7 +7,8 @@ def self.config @config ||= OpenStruct.new({ collector: RspecProfiling::Collectors::SQL, vcs: RspecProfiling::VCS::Git, - table_name: 'spec_profiling_results' + table_name: 'spec_profiling_results', + events: [] }) end end diff --git a/lib/rspec_profiling/example.rb b/lib/rspec_profiling/example.rb index e66514a..63b8d76 100644 --- a/lib/rspec_profiling/example.rb +++ b/lib/rspec_profiling/example.rb @@ -21,6 +21,8 @@ class Example def initialize(example) @example = example @counts = Hash.new(0) + @event_counts = Hash.new(0) + @event_times = Hash.new(0) end def file @@ -38,7 +40,7 @@ def description def status execution_result.status end - + def exception execution_result.exception end @@ -63,6 +65,8 @@ def request_time counts[:request_time] end + attr_reader :event_counts, :event_times + def log_query(query, start, finish) unless query[:sql] =~ IGNORED_QUERIES_PATTERN counts[:query_count] += 1 @@ -75,6 +79,11 @@ def log_request(request, start, finish) counts[:request_time] += request[:view_runtime].to_f end + def log_event(name, start, finish) + event_counts[name] += 1 + event_times[name] += (finish - start) + end + private attr_reader :example, :counts diff --git a/lib/rspec_profiling/run.rb b/lib/rspec_profiling/run.rb index 5814a0c..48eaaff 100644 --- a/lib/rspec_profiling/run.rb +++ b/lib/rspec_profiling/run.rb @@ -9,15 +9,18 @@ module RspecProfiling class Run def initialize(collector = RspecProfiling.config.collector.new, - vcs = RspecProfiling.config.vcs.new) + vcs = RspecProfiling.config.vcs.new, + events = RspecProfiling.config.events) @collector = collector @vcs = vcs + @events = events end def start(*args) start_counting_queries start_counting_requests + start_counting_events end def example_started(example) @@ -39,7 +42,10 @@ def example_finished(*args) query_count: @current_example.query_count, query_time: @current_example.query_time, request_count: @current_example.request_count, - request_time: @current_example.request_time + request_time: @current_example.request_time, + events: @events, + event_counts: @current_example.event_counts, + event_times: @current_example.event_times }) end @@ -48,7 +54,7 @@ def example_finished(*args) private - attr_reader :collector, :vcs + attr_reader :collector, :vcs, :events def start_counting_queries ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, query| @@ -61,5 +67,13 @@ def start_counting_requests @current_example.try(:log_request, request, start, finish) end end + + def start_counting_events + events.each do |event| + ActiveSupport::Notifications.subscribe(event) do |extra, start, finish, id, request| + @current_example.try(:log_event, event, start, finish) + end + end + end end end diff --git a/rspec_profiling b/rspec_profiling index d71e81971632faf9de4a85c4c76eaa30160c5223..482e0b8bc20f63172510e4b0562488a1657f8eba 100644 GIT binary patch literal 12288 zcmeI2Pfrs;7{+&71X^erPe?-I(2EcR_J3QvSg;|)Qm~c-r)Iky=qBB5*_j$V?m-hT z#!sRbzlCQnp8Wz|ym)b@q=c4j@Zv_|yV==f-e+d|>~G%8%qD&LqT(`wPrHFB#*&nHAW2e^mjo{|FVA?H;$?V=*ew?yWAn+6OtvP;SxL%D^X4rbKn4Op00;m9 zAOHk_01yBIK;T~^kgdsI7K=ss?K-oY9YTA=Y4n2bsoQb=vqnH@zr$#BG_hMNm+EC) zFYQ#yI2YY0hgWlMD~Da5kuwtDquTyKsdj>&mrro%xPG`_<)a*wt94vGv3;&eUFWsSB?N5U5C8%|00;nqJ4zr#+56JclKx13C@-zZ zaw<_Oc{XJvDDCX3nyx9Twju5UR<&(K;rABpdSpu+l{bTC`X#Smad>a_Y)VN>h}fmH zPtuzFK~pqkL$Nj#6RXCyQrK4Vn|i_2RduowqD=gjR8^l~Nhl$jkKIx}Uo@jF4U`bg z#c#<}bTe||@Y>UYAUvgeV!iB_nT{S|p`` z5~4)>mJCDTdtb!vX9y)ka_pA$f-zy+9Vi)bff00e*l W5C8%|00;m9ATT7bEJy#-Ci@$d$XVlLGAPQ$x#!s9M_v|+B|_cZ#1^4;tqV@ixm3C3EUeefR?TkafOP{$OGq8 z0fW#G@lga7@X9;=*uUg~LcM;(ulaZo58c2UM&6+RyQ>%uiMZ?eP7j8LkrTz?{Y~+a z%O8#bzQNm$JBa(>=}-De^K`;MOh*{~-Th9O9^eKXcB|WQAo;x=jEAD~uo-4+Qda!Z zx6)zpOL1fQ!_v>Co5I(^e*SZQeewNW4_qSwB!C2v01`j~NZ^SHB&0~BHT${zOkUfN z<$TVz?fnB)(>11QWwF$#s_iib%MKft-EiRZU2)&G6I>R~phCqhV|VC;A@2&2dSMvz zf+l~2EJm4`Wd>QQds=l*w|0yQGfeeyB_Ye1ThdKtvQ$fBB_T`MThfhcCEb!x5>m+C z5@VK@Zb>K!$!Bg!s~T4Nu{2f^vY5RkT~*CgN}5m-vXHqY)v6fj6E{{8GM~LA!+PZK zR)vy~x$G^erkOr*Fl{DG+-&BSSdAI!KLTSVAv4)q(yLZFB_@=F 1.3" spec.add_development_dependency "rake" spec.add_development_dependency "rspec" + spec.add_development_dependency "pry" end diff --git a/spec/run_spec.rb b/spec/run_spec.rb index 94ee6ea..5ba9e84 100644 --- a/spec/run_spec.rb +++ b/spec/run_spec.rb @@ -17,10 +17,14 @@ def simulate_request }) end + def simulate_event(name) + ActiveSupport::Notifications.instrument(name, name, 100, 150, 3, {}) + end + describe "#run_example" do let(:collector) { CollectorDouble.new } - let(:vcs) { VcsDouble.new } - let(:run) { described_class.new(collector, vcs) } + let(:vcs) { VcsDouble.new } + let(:run) { described_class.new(collector, vcs, ["custom"]) } let(:result) { collector.results.first } let(:example) do ExampleDouble.new({ @@ -36,6 +40,8 @@ def simulate_test_suite_run simulate_query "SELECT * FROM users LIMIT 1;" simulate_query "SELECT * FROM comments WHERE user_id = 1;" simulate_request + simulate_event "custom" + simulate_event "untracked" run.example_passed end @@ -64,6 +70,10 @@ def simulate_test_suite_run expect(result.request_count).to eq 1 end + it "counts one custom event" do + expect(result.event_counts["custom"]).to eq 1 + end + it "records the file" do expect(result.file).to eq "/something_spec.rb" end @@ -87,6 +97,10 @@ def simulate_test_suite_run it "records the request time" do expect(result.request_time).to eq 10 end + + it "records the custom event time" do + expect(result.event_times["custom"]).to eq 50 + end end class CollectorDouble From 171cfd7d6d6d58a42a3d1489c92389681e957bab Mon Sep 17 00:00:00 2001 From: Zach Mckenzie Date: Tue, 9 Jun 2020 16:38:19 -0700 Subject: [PATCH 2/8] Adding verbose event logging for csv --- README.md | 15 +++++++++++++++ lib/rspec_profiling/collectors/csv.rb | 4 ++-- lib/rspec_profiling/example.rb | 20 ++++++++++++++++---- lib/rspec_profiling/run.rb | 9 +++++---- spec/run_spec.rb | 10 ++++++++-- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 464db9a..fbb1bc8 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,21 @@ end Note that custom events are only currenly reported by the CSV collector. +#### Custom Event Recording + +It is possible to record the event metadata for a spec. + +```Ruby + describe 'Records all active record queries', record_events: %w[sql.active_record] do + it 'Records Rails deprecations', record_events: %w[deprecation.rails] do + ... + end + it 'Records nothing' do + ... + end + end +``` + ### Choose a results collector Results are collected just by running the specs. diff --git a/lib/rspec_profiling/collectors/csv.rb b/lib/rspec_profiling/collectors/csv.rb index 1bbeed6..ff5b481 100644 --- a/lib/rspec_profiling/collectors/csv.rb +++ b/lib/rspec_profiling/collectors/csv.rb @@ -61,13 +61,13 @@ def static_cells(attributes) def event_headers config.events.flat_map do |event| - ["#{event}_count", "#{event}_time"] + ["#{event}_count", "#{event}_time", "#{event}_events"] end end def event_cells(attributes) config.events.flat_map do |event| - [attributes[:event_counts][event], attributes[:event_times][event]] + [attributes[:event_counts][event], attributes[:event_times][event], attributes[:event_events][event]] end end end diff --git a/lib/rspec_profiling/example.rb b/lib/rspec_profiling/example.rb index 63b8d76..646e7a7 100644 --- a/lib/rspec_profiling/example.rb +++ b/lib/rspec_profiling/example.rb @@ -23,6 +23,7 @@ def initialize(example) @counts = Hash.new(0) @event_counts = Hash.new(0) @event_times = Hash.new(0) + @event_events = Hash.new() end def file @@ -65,7 +66,7 @@ def request_time counts[:request_time] end - attr_reader :event_counts, :event_times + attr_reader :event_counts, :event_times, :event_events def log_query(query, start, finish) unless query[:sql] =~ IGNORED_QUERIES_PATTERN @@ -79,9 +80,16 @@ def log_request(request, start, finish) counts[:request_time] += request[:view_runtime].to_f end - def log_event(name, start, finish) - event_counts[name] += 1 - event_times[name] += (finish - start) + def log_event(event_name, event, start, finish) + event_counts[event_name] += 1 + event_times[event_name] += (finish - start) + if verbose_record_event?(event_name) + begin + ((event_events[event_name] ||= []) << event.as_json) + rescue => e + # no op + end + end end private @@ -99,5 +107,9 @@ def execution_result def metadata example.metadata end + + def verbose_record_event?(event_name) + metadata[:record_events].to_a.include?(event_name) + end end end diff --git a/lib/rspec_profiling/run.rb b/lib/rspec_profiling/run.rb index 48eaaff..2018f52 100644 --- a/lib/rspec_profiling/run.rb +++ b/lib/rspec_profiling/run.rb @@ -45,7 +45,8 @@ def example_finished(*args) request_time: @current_example.request_time, events: @events, event_counts: @current_example.event_counts, - event_times: @current_example.event_times + event_times: @current_example.event_times, + event_events: @current_example.event_events }) end @@ -69,9 +70,9 @@ def start_counting_requests end def start_counting_events - events.each do |event| - ActiveSupport::Notifications.subscribe(event) do |extra, start, finish, id, request| - @current_example.try(:log_event, event, start, finish) + events.each do |event_name| + ActiveSupport::Notifications.subscribe(event_name) do |name, start, finish, id, event| + @current_example.try(:log_event, event_name, event, start, finish) end end end diff --git a/spec/run_spec.rb b/spec/run_spec.rb index 5ba9e84..195e23f 100644 --- a/spec/run_spec.rb +++ b/spec/run_spec.rb @@ -1,3 +1,4 @@ +require "active_support" require "active_support/core_ext" require "rspec_profiling/run" require "time" @@ -18,7 +19,7 @@ def simulate_request end def simulate_event(name) - ActiveSupport::Notifications.instrument(name, name, 100, 150, 3, {}) + ActiveSupport::Notifications.instrument(name, name, 100, 150, 3, {name: 'custom', data: {key: 'value'}}) end describe "#run_example" do @@ -30,7 +31,8 @@ def simulate_event(name) ExampleDouble.new({ file_path: "/something_spec.rb", line_number: 15, - full_description: "should do something" + full_description: "should do something", + record_events: %w[custom] }) end @@ -101,6 +103,10 @@ def simulate_test_suite_run it "records the custom event time" do expect(result.event_times["custom"]).to eq 50 end + + it "records the custom event events" do + expect(result.event_events["custom"]).to eq [{"data"=>{"key"=>"value"}, "name"=>"custom"}] + end end class CollectorDouble From 57b2e5063963dc30d2b4bb2128df5f799afcad34 Mon Sep 17 00:00:00 2001 From: Zach Mckenzie Date: Wed, 10 Jun 2020 20:02:34 -0700 Subject: [PATCH 3/8] cleanup the output of the CSV collector --- lib/rspec_profiling/collectors/csv.rb | 2 +- lib/rspec_profiling/example.rb | 3 ++- lib/rspec_profiling/vcs/git.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/rspec_profiling/collectors/csv.rb b/lib/rspec_profiling/collectors/csv.rb index ff5b481..436fa09 100644 --- a/lib/rspec_profiling/collectors/csv.rb +++ b/lib/rspec_profiling/collectors/csv.rb @@ -67,7 +67,7 @@ def event_headers def event_cells(attributes) config.events.flat_map do |event| - [attributes[:event_counts][event], attributes[:event_times][event], attributes[:event_events][event]] + [attributes[:event_counts][event], attributes[:event_times][event], attributes[:event_events][event].to_json] end end end diff --git a/lib/rspec_profiling/example.rb b/lib/rspec_profiling/example.rb index 646e7a7..fac302a 100644 --- a/lib/rspec_profiling/example.rb +++ b/lib/rspec_profiling/example.rb @@ -83,9 +83,10 @@ def log_request(request, start, finish) def log_event(event_name, event, start, finish) event_counts[event_name] += 1 event_times[event_name] += (finish - start) + event_events[event_name] ||= [] if verbose_record_event?(event_name) begin - ((event_events[event_name] ||= []) << event.as_json) + event_events[event_name] << event.as_json rescue => e # no op end diff --git a/lib/rspec_profiling/vcs/git.rb b/lib/rspec_profiling/vcs/git.rb index f608c82..5775176 100644 --- a/lib/rspec_profiling/vcs/git.rb +++ b/lib/rspec_profiling/vcs/git.rb @@ -12,7 +12,7 @@ def sha end def time - Time.parse `git show -s --format=%ci #{sha}` + Time.parse `git show -s --format=%ci #{sha}`.strip end end end From ca09a900a80b3a5ae277fe15fa92c1a7233d0317 Mon Sep 17 00:00:00 2001 From: Zach Mckenzie Date: Fri, 12 Jun 2020 15:25:11 -0700 Subject: [PATCH 4/8] removing dependencies on sqlite and pg if you are not using them --- README.md | 20 +++++++++++++++++--- lib/rspec_profiling.rb | 16 ++++++++++++++-- lib/rspec_profiling/collectors/psql.rb | 2 +- lib/rspec_profiling/collectors/sql.rb | 2 +- lib/rspec_profiling/config.rb | 2 +- lib/rspec_profiling/run.rb | 2 -- rspec_profiling.gemspec | 2 -- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fbb1bc8..8514a19 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,13 @@ which results will be collected. bundle exec rake rspec_profiling:install ``` +If you are planning on using `sqlite` or `pg` ensure to add the depency to your gemfile + +``` + gem 'sqlite', require: false + gem 'pg', require: false +``` + ## Usage ### Choose a version control system @@ -105,8 +112,15 @@ Results are collected just by running the specs. #### SQLite3 -By default, profiles are collected in an SQL database. Make sure you've -run the installation rake task before attempting. +Make sure you've run the installation rake task before attempting. + +You can configure `RspecProfiling` to collect results in a SQL database in `config/initializers/rspec_profiling.rb`: + +```Ruby +RspecProfiling.configure do |config| + config.collector = RspecProfiling::Collectors::SQL +end +``` You can review results by running the RspecProfiling console. The console has a preloaded `results` variable. @@ -137,7 +151,7 @@ debugging, such as `exception` and `status`. #### CSV -You can configure `RspecProfiling` to collect results in a CSV in `config/initializers/rspec_profiling.rb`: +By default, profiles are collected in an a CSV file. You can configure `RspecProfiling` to collect results in a CSV in `config/initializers/rspec_profiling.rb`: ```Ruby RspecProfiling.configure do |config| diff --git a/lib/rspec_profiling.rb b/lib/rspec_profiling.rb index ad77951..4023c28 100644 --- a/lib/rspec_profiling.rb +++ b/lib/rspec_profiling.rb @@ -4,12 +4,24 @@ require "rspec_profiling/version" require "rspec_profiling/run" require "rspec_profiling/collectors/csv" -require "rspec_profiling/collectors/sql" -require "rspec_profiling/collectors/psql" require "rspec_profiling/vcs/git" require "rspec_profiling/vcs/svn" require "rspec_profiling/vcs/git_svn" +begin + require "rspec_profiling/collectors/sql" +rescue LoadError + #no op +end + +begin + require "rspec_profiling/collectors/psql" +rescue LoadError + #no op +end + + + module RspecProfiling class Railtie < Rails::Railtie railtie_name :rspec_profiling diff --git a/lib/rspec_profiling/collectors/psql.rb b/lib/rspec_profiling/collectors/psql.rb index 9509746..48c5442 100644 --- a/lib/rspec_profiling/collectors/psql.rb +++ b/lib/rspec_profiling/collectors/psql.rb @@ -47,7 +47,7 @@ def uninstall end def insert(attributes) - results.create!(attributes.except(:created_at)) + results.create!(attributes.except(:created_at, :events, :event_counts, :event_times, :event_events)) end def results diff --git a/lib/rspec_profiling/collectors/sql.rb b/lib/rspec_profiling/collectors/sql.rb index c35e57f..fd58446 100644 --- a/lib/rspec_profiling/collectors/sql.rb +++ b/lib/rspec_profiling/collectors/sql.rb @@ -47,7 +47,7 @@ def uninstall end def insert(attributes) - results.create!(attributes.except(:created_at)) + results.create!(attributes.except(:created_at, :events, :event_counts, :event_times, :event_events)) end def results diff --git a/lib/rspec_profiling/config.rb b/lib/rspec_profiling/config.rb index 6b05c94..2a5d67b 100644 --- a/lib/rspec_profiling/config.rb +++ b/lib/rspec_profiling/config.rb @@ -5,7 +5,7 @@ def self.configure def self.config @config ||= OpenStruct.new({ - collector: RspecProfiling::Collectors::SQL, + collector: RspecProfiling::Collectors::CSV, vcs: RspecProfiling::VCS::Git, table_name: 'spec_profiling_results', events: [] diff --git a/lib/rspec_profiling/run.rb b/lib/rspec_profiling/run.rb index 2018f52..fdde526 100644 --- a/lib/rspec_profiling/run.rb +++ b/lib/rspec_profiling/run.rb @@ -2,8 +2,6 @@ require "rspec_profiling/vcs/git" require "rspec_profiling/vcs/svn" require "rspec_profiling/vcs/git_svn" -require "rspec_profiling/collectors/sql" -require "rspec_profiling/collectors/psql" require "rspec_profiling/collectors/csv" module RspecProfiling diff --git a/rspec_profiling.gemspec b/rspec_profiling.gemspec index 5383950..b2b0197 100644 --- a/rspec_profiling.gemspec +++ b/rspec_profiling.gemspec @@ -18,9 +18,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_dependency "sqlite3" spec.add_dependency "activerecord" - spec.add_dependency "pg" spec.add_dependency "rails" spec.add_development_dependency "bundler", "> 1.3" From e75cb6983a4f12072821d14174f8d62d2c3770f5 Mon Sep 17 00:00:00 2001 From: Zach Mckenzie Date: Thu, 12 Nov 2020 11:19:24 -0800 Subject: [PATCH 5/8] adding json reporting to collectors --- lib/rspec_profiling/collectors/csv.rb | 3 ++ lib/rspec_profiling/collectors/json.rb | 68 +++++++++++++++++++++++++ lib/rspec_profiling/run.rb | 19 ++++++- rspec_profiling | Bin 12288 -> 0 bytes rspec_profiling.gemspec | 1 + 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 lib/rspec_profiling/collectors/json.rb delete mode 100644 rspec_profiling diff --git a/lib/rspec_profiling/collectors/csv.rb b/lib/rspec_profiling/collectors/csv.rb index 436fa09..0bd253e 100644 --- a/lib/rspec_profiling/collectors/csv.rb +++ b/lib/rspec_profiling/collectors/csv.rb @@ -6,6 +6,7 @@ class CSV HEADERS = %w{ branch commit_hash + seed date file line_number @@ -17,6 +18,8 @@ class CSV query_time request_count request_time + start_memory + end_memory } def self.install diff --git a/lib/rspec_profiling/collectors/json.rb b/lib/rspec_profiling/collectors/json.rb new file mode 100644 index 0000000..1bfbf79 --- /dev/null +++ b/lib/rspec_profiling/collectors/json.rb @@ -0,0 +1,68 @@ +module RspecProfiling + module Collectors + class JSON + KEYS = %w{ + branch + commit_hash + seed + date + file + line_number + description + status + exception + time + query_count + query_time + request_count + request_time + start_memory + end_memory + } + + def self.install + # no op + end + + def self.uninstall + # no op + end + + def self.reset + # no op + end + + def initialize(config=RspecProfiling.config) + config.output_file_path ||= 'tmp/spec_benchmarks.json' + + @config = config + end + + def insert(attributes) + output << merge_attributes_and_events(attributes) + "\n" + end + + private + + attr_reader :config + + def output + @output ||= ::File.open(path, "w") + end + + def path + config.output_file_path.call + end + + def merge_attributes_and_events(attributes) + config.events.flat_map do |event| + attributes["#{event}_counts"] = attributes[:event_counts][event] + attributes["#{event}_times"] = attributes[:event_times][event] + attributes["#{event}_events"] = attributes[:event_events][event] + end + + attributes.except(:event_counts, :event_times, :event_events, :events).to_json + end + end + end +end diff --git a/lib/rspec_profiling/run.rb b/lib/rspec_profiling/run.rb index fdde526..abd4865 100644 --- a/lib/rspec_profiling/run.rb +++ b/lib/rspec_profiling/run.rb @@ -1,8 +1,10 @@ +require "get_process_mem" require "rspec_profiling/example" require "rspec_profiling/vcs/git" require "rspec_profiling/vcs/svn" require "rspec_profiling/vcs/git_svn" require "rspec_profiling/collectors/csv" +require "rspec_profiling/collectors/json" module RspecProfiling class Run @@ -13,6 +15,7 @@ def initialize(collector = RspecProfiling.config.collector.new, @collector = collector @vcs = vcs @events = events + @seed = RSpec.configuration.seed end def start(*args) @@ -22,6 +25,7 @@ def start(*args) end def example_started(example) + start_recording_memory example = example.example if example.respond_to?(:example) @current_example = Example.new(example) end @@ -30,6 +34,7 @@ def example_finished(*args) collector.insert({ branch: vcs.branch, commit_hash: vcs.sha, + seed: @seed, date: vcs.time, file: @current_example.file, line_number: @current_example.line_number, @@ -44,7 +49,9 @@ def example_finished(*args) events: @events, event_counts: @current_example.event_counts, event_times: @current_example.event_times, - event_events: @current_example.event_events + event_events: @current_example.event_events, + start_memory: @start_memory, + end_memory: end_memory }) end @@ -53,7 +60,15 @@ def example_finished(*args) private - attr_reader :collector, :vcs, :events + attr_reader :collector, :vcs, :events, :seed, :start_memory + + def end_memory + GetProcessMem.new.mb + end + + def start_recording_memory + @start_memory = GetProcessMem.new.mb + end def start_counting_queries ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, query| diff --git a/rspec_profiling b/rspec_profiling deleted file mode 100644 index 482e0b8bc20f63172510e4b0562488a1657f8eba..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2Pfrs;7{+&71X^erPe?-I(2EcR_J3QvSg;|)Qm~c-r)Iky=qBB5*_j$V?m-hT z#!sRbzlCQnp8Wz|ym)b@q=c4j@Zv_|yV==f-e+d|>~G%8%qD&LqT(`wPrHFB#*&nHAW2e^mjo{|FVA?H;$?V=*ew?yWAn+6OtvP;SxL%D^X4rbKn4Op00;m9 zAOHk_01yBIK;T~^kgdsI7K=ss?K-oY9YTA=Y4n2bsoQb=vqnH@zr$#BG_hMNm+EC) zFYQ#yI2YY0hgWlMD~Da5kuwtDquTyKsdj>&mrro%xPG`_<)a*wt94vGv3;&eUFWsSB?N5U5C8%|00;nqJ4zr#+56JclKx13C@-zZ zaw<_Oc{XJvDDCX3nyx9Twju5UR<&(K;rABpdSpu+l{bTC`X#Smad>a_Y)VN>h}fmH zPtuzFK~pqkL$Nj#6RXCyQrK4Vn|i_2RduowqD=gjR8^l~Nhl$jkKIx}Uo@jF4U`bg z#c#<}bTe||@Y>UYAUvgeV!iB_nT{S|p`` z5~4)>mJCDTdtb!vX9y)ka_pA$f-zy+9Vi)bff00e*l W5C8%|00;m9ATT7bEJy#-Ci@$d$XV 1.3" spec.add_development_dependency "rake" From 57a45a6ec75459b6b5103f132180d2cc3eeb4dc4 Mon Sep 17 00:00:00 2001 From: Zach Mckenzie Date: Thu, 19 Nov 2020 09:50:58 -0800 Subject: [PATCH 6/8] adding additional data hash to config --- lib/rspec_profiling/collectors/json.rb | 2 ++ lib/rspec_profiling/config.rb | 3 ++- lib/rspec_profiling/rspec.rb | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/rspec_profiling/collectors/json.rb b/lib/rspec_profiling/collectors/json.rb index 1bfbf79..6e8cdff 100644 --- a/lib/rspec_profiling/collectors/json.rb +++ b/lib/rspec_profiling/collectors/json.rb @@ -61,6 +61,8 @@ def merge_attributes_and_events(attributes) attributes["#{event}_events"] = attributes[:event_events][event] end + attributes.merge(config.additional_data) + attributes.except(:event_counts, :event_times, :event_events, :events).to_json end end diff --git a/lib/rspec_profiling/config.rb b/lib/rspec_profiling/config.rb index 2a5d67b..43c5572 100644 --- a/lib/rspec_profiling/config.rb +++ b/lib/rspec_profiling/config.rb @@ -8,7 +8,8 @@ def self.config collector: RspecProfiling::Collectors::CSV, vcs: RspecProfiling::VCS::Git, table_name: 'spec_profiling_results', - events: [] + events: [], + additional_data: {} }) end end diff --git a/lib/rspec_profiling/rspec.rb b/lib/rspec_profiling/rspec.rb index 53fc308..fcf2574 100644 --- a/lib/rspec_profiling/rspec.rb +++ b/lib/rspec_profiling/rspec.rb @@ -1,7 +1,7 @@ require "rspec_profiling" RSpec.configure do |config| - runner = RspecProfiling::Run.new(RspecProfiling.config.collector.new, + runner = RspecProfiling::Run.new(RspecProfiling.config.collector.new, RspecProfiling.config.vcs.new) config.reporter.register_listener( From 188fbd5498ceb9811a1741e74feb6f8d7b0e85b9 Mon Sep 17 00:00:00 2001 From: Zach Mckenzie Date: Thu, 19 Nov 2020 10:36:34 -0800 Subject: [PATCH 7/8] fixes the additional data merge --- lib/rspec_profiling/collectors/json.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rspec_profiling/collectors/json.rb b/lib/rspec_profiling/collectors/json.rb index 6e8cdff..ab934cd 100644 --- a/lib/rspec_profiling/collectors/json.rb +++ b/lib/rspec_profiling/collectors/json.rb @@ -61,7 +61,7 @@ def merge_attributes_and_events(attributes) attributes["#{event}_events"] = attributes[:event_events][event] end - attributes.merge(config.additional_data) + attributes.merge!(config.additional_data) attributes.except(:event_counts, :event_times, :event_events, :events).to_json end From 28d58cebcf717407d6ccfe2f324c83816a1c971f Mon Sep 17 00:00:00 2001 From: Bob Laskowski Date: Thu, 8 Feb 2024 13:23:56 -0700 Subject: [PATCH 8/8] consistently use chomp, increment version --- lib/rspec_profiling/vcs/git.rb | 2 +- lib/rspec_profiling/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rspec_profiling/vcs/git.rb b/lib/rspec_profiling/vcs/git.rb index 5775176..73ce2c0 100644 --- a/lib/rspec_profiling/vcs/git.rb +++ b/lib/rspec_profiling/vcs/git.rb @@ -12,7 +12,7 @@ def sha end def time - Time.parse `git show -s --format=%ci #{sha}`.strip + Time.parse `git show -s --format=%ci #{sha}`.chomp end end end diff --git a/lib/rspec_profiling/version.rb b/lib/rspec_profiling/version.rb index c08fc41..09e54a9 100644 --- a/lib/rspec_profiling/version.rb +++ b/lib/rspec_profiling/version.rb @@ -1,3 +1,3 @@ module RspecProfiling - VERSION = "0.0.6" + VERSION = "0.0.7" end