File: base.rb

package info (click to toggle)
ruby-fast-gettext 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: ruby: 3,200; makefile: 4
file content (58 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download | duplicates (4)
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
# frozen_string_literal: true

require 'benchmark'
$LOAD_PATH.unshift 'lib'

RUNS = 500_000

def locale_folder(domain)
  path =
    case domain
    when 'test' then File.join(__dir__, '..', 'spec', 'locale')
    when 'large' then File.join(__dir__, 'locale')
    end

  mo = File.join(path, 'de', 'LC_MESSAGES', "#{domain}.mo")
  raise unless File.exist?(mo)

  path
end

def results_test(&block)
  print "#{result(&block)}s / #{memory}K <-> "
end

def results_large
  print "#{(result { _('login') == 'anmelden' })}s / #{memory}K / #{namespace}"
  puts ""
end

def result
  Benchmark.realtime do
    RUNS.times do
      raise "not translated" unless yield
    end
  end.round(2)
end

def memory
  (calculate_memory - @default_memory) / 1000
end

def calculate_memory
  GC.stat[:total_allocated_objects]
end

def namespace
  calculate_namespace - @default_namespace
end

def calculate_namespace
  methods.size
end

GC.start
GC.disable

@default_memory = calculate_memory
@default_namespace = calculate_namespace