Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Jun 28, 2024
1 parent f8ddd77 commit 8533a83
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
52 changes: 52 additions & 0 deletions benchmarks/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby -s
# frozen_string_literal: true

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

I18n.config.available_locales = :en
Money.locale_backend = :i18n
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

Benchmark.bmbm(30) do |x|
x.report('README Examples') do
10_000.times do
money = Money.from_cents(1000, "USD")
money.cents #=> 1000
money.currency #=> Currency.new("USD")

Money.from_cents(1000, "USD") == Money.from_cents(1000, "USD") #=> true
Money.from_cents(1000, "USD") == Money.from_cents(100, "USD") #=> false
Money.from_cents(1000, "USD") == Money.from_cents(1000, "EUR") #=> false
Money.from_cents(1000, "USD") != Money.from_cents(1000, "EUR") #=> true

Money.from_cents(1000, "USD") + Money.from_cents(500, "USD") == Money.from_cents(1500, "USD")
Money.from_cents(1000, "USD") - Money.from_cents(200, "USD") == Money.from_cents(800, "USD")
Money.from_cents(1000, "USD") / 5 == Money.from_cents(200, "USD")
Money.from_cents(1000, "USD") * 5 == Money.from_cents(5000, "USD")

Money.from_amount(5, "USD") == Money.from_cents(500, "USD") # 5 USD
Money.from_amount(5, "JPY") == Money.from_cents(5, "JPY") # 5 JPY
Money.from_amount(5, "TND") == Money.from_cents(5000, "TND") # 5 TND

Money.from_cents(1000, "USD").with_currency("EUR") == Money.from_cents(1000, "EUR")

Money.from_cents(100, "USD").format #=> "$1.00"
Money.from_cents(100, "GBP").format #=> "£1.00"
Money.from_cents(100, "EUR").format #=> "€1.00"
end
end

x.report('Money#from_cents') do
100_000.times do
Money.from_cents(100, "USD")
end
end

x.report('Money#format') do
50_000.times do
Money.from_cents(100, "USD").format
end
end
end
20 changes: 20 additions & 0 deletions benchmarks/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby -s
# frozen_string_literal: true

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'money'
require 'money/version'
require 'ruby-prof'

puts "Money: #{Money::VERSION}"

I18n.config.available_locales = :en
Money.locale_backend = :i18n
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

result = RubyProf::Profile.profile do
Money.from_cents(100, "USD").format
end

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

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

# explicitly define locales
I18n.config.available_locales = :en
Money.locale_backend = :i18n
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

report = MemoryProfiler.report(allow_files: 'money') do
Money.from_cents(100, "USD").format
end

report.pretty_print

0 comments on commit 8533a83

Please sign in to comment.