File: perf_spec.rb

package info (click to toggle)
ruby-countries 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,268 kB
  • sloc: ruby: 2,687; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

require 'spec_helper'

ALL_LOCALES = %i[
  af am ar as az be bg bn br bs ca cs cy da de
  dz el en eo es et eu fa fi fo fr ga gl gu he
  hi hr hu hy ia id is it ja ka kk km kn ko ku
  lt lv mi mk ml mn mr ms mt nb ne nl nn oc or
  pa pl ps pt ro ru rw si sk sl so sq sr sv sw
  ta te th ti tk tl tr tt ug uk ve vi wa wo xh
  zh zu
].freeze

describe ISO3166::Data, perf: true, order: :defined do
  def perf_report(_name)
    require 'benchmark'
    require 'memory_profiler'
    require 'ruby-prof'
    # profile the code
    RubyProf.start

    100.times do
      yield if block_given?
    end

    result = RubyProf.stop

    # print a flat profile to text
    printer = RubyProf::FlatPrinter.new(result)
    printer.print($stdout)
  end

  it 'responds to call' do
    perf_report('translations') do
      ISO3166.reset
      data = ISO3166::Data.new('US').call
      expect(data['translated_names']).to be_a Array
    end
  end

  it 'locales will load prior to return results' do
    ISO3166.configuration.locales = %i[es de en]
    report = MemoryProfiler.report do
      ISO3166::Data.update_cache
    end

    report.pretty_print(to_file: 'tmp/3_locales.txt')
    puts Benchmark.measure { ISO3166::Data.update_cache }

    ISO3166.configure do |config|
      config.locales = ALL_LOCALES
    end
    puts Benchmark.measure { ISO3166::Data.update_cache }

    report = MemoryProfiler.report do
      ISO3166::Data.update_cache
    end

    report.pretty_print(to_file: 'tmp/all_locales.txt')
  end

  it 'loades a specific country quickly' do
    codes = Dir['lib/countries/data/countries/*.yaml'].map { |x| File.basename(x, File.extname(x)) }.uniq
    Benchmark.bmbm do |bm|
      bm.report('find_by_alpha2') do
        codes.map { |cc| ISO3166::Country.find_country_by_alpha2 cc }
      end
      bm.report('new') do
        codes.map { |cc| ISO3166::Country.new cc }
      end
    end
  end
end