Skip to content

Commit

Permalink
Add profilers and benchmark
Browse files Browse the repository at this point in the history
Simple scenario to profile application
  • Loading branch information
tagliala committed Sep 16, 2023
1 parent 5cfcb64 commit 51d8312
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions benchmarks/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby -s

# frozen_string_literal: true

require_relative 'benchmark_sample'
require 'benchmark'

Benchmark.bmbm(30) do |x|
x.report('benchmark_sample') do
run_benchmark_sample
end
end
55 changes: 55 additions & 0 deletions benchmarks/benchmark_sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'chrono_model'
require 'rails'

ActiveRecord::Base.establish_connection(adapter: 'chronomodel', database: 'chronomodel_bench')
ActiveRecord::Base.logger = nil
ActiveRecord::Migration.verbose = false

ActiveRecord::Schema.define do
enable_extension :btree_gist

drop_table :foos, if_exists: true

create_table :foos, temporal: true do |t|
t.string :name
t.timestamps
end
end

class Foo < ActiveRecord::Base
include ChronoModel::TimeMachine
end

def run_benchmark_sample(iterations: 100)
# Create
iterations.times { |i| Foo.create! name: "Foo #{i}" }

foo = Foo.create! name: 'Foo'

# Update
iterations.times { |i| foo.update! name: "New Foo #{i}" }

# Destroy
iterations.times { Foo.create!.destroy! }

# Delete
iterations.times { Foo.create!.delete }

# Find
iterations.times { |i| Foo.find(i + 1) }

# Where
iterations.times { |i| Foo.where(id: i + 1) }

# as_of
iterations.times { foo.as_of(1.day.ago) }

# History
iterations.times { foo.history.first }
iterations.times { foo.history.last }

iterations.times { Foo::History.sti_name }
end
13 changes: 13 additions & 0 deletions benchmarks/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby -s

# frozen_string_literal: true

require_relative 'benchmark_sample'
require 'ruby-prof'

result = RubyProf::Profile.profile do
run_benchmark_sample
end

printer = RubyProf::FlatPrinter.new(result)
printer.print($stdout, {})
10 changes: 10 additions & 0 deletions benchmarks/profile_memory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby -s

# frozen_string_literal: true

require_relative 'benchmark_sample'
require 'memory_profiler'

MemoryProfiler.report(allow_files: 'chronomodel/lib') do
run_benchmark_sample
end.pretty_print
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'simplecov'
SimpleCov.start do
add_filter '/benchmarks'
add_filter '/spec'
end

Expand Down

0 comments on commit 51d8312

Please sign in to comment.