Module: Expressir::Benchmark

Defined in:
lib/expressir/benchmark.rb

Class Method Summary collapse

Class Method Details

.measure_collection(files) ⇒ Array

Benchmarks file collection processing

Parameters:

  • files (Array<String>)

    List of files to process

Returns:

  • (Array)

    Results from processing files



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/expressir/benchmark.rb', line 59

def measure_collection(files)
  unless Expressir.configuration.benchmark_enabled?
    # Process each file individually even when benchmarking is disabled
    results = []
    files.each do |file|
      file_results = yield(file)
      results.concat(Array(file_results))
    end
    return results
  end

  results = []
  total_time = ::Benchmark.measure do
    files.each_with_index do |file, i|
      if Expressir.configuration.benchmark_verbose?
        puts "#{i + 1}/#{files.length} Processing #{file}"
      end

      file_results = measure_file(file) do
        yield(file)
      end

      results.concat(Array(file_results))
    end
  end

  output_collection_result(files.length, total_time.real, results)
  results
end

.measure_file(file) ⇒ Object

Measures the execution time of schema loading and optionally reports it

Parameters:

  • file (String)

    File being processed

  • options (Hash)

    Options for benchmarking

Returns:

  • The result of the block



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
# File 'lib/expressir/benchmark.rb', line 12

def measure_file(file)
  return yield unless Expressir.configuration.benchmark_enabled?

  if Expressir.configuration.benchmark_ips?
    require "benchmark/ips"

    result = nil
    ::Benchmark.ips do |x|
      x.config(time: Expressir.configuration.benchmark_iterations,
               warmup: Expressir.configuration.benchmark_warmup)

      x.report("Loading #{file}") do
        result = yield
      end

      if Expressir.configuration.benchmark_save?
        x.save! "expressir_benchmark_#{File.basename(file)}"
      end
    end

    # Calculate objects per second
    if result.respond_to?(:schemas)
      objects_per_second = calculate_objects_per_second(result)
      puts "Objects per second: #{objects_per_second}" if Expressir.configuration.benchmark_verbose?
    end

  else
    # Standard benchmarking
    result = nil
    time = ::Benchmark.measure do
      result = yield
    end

    if result.respond_to?(:schemas)
      objects_per_second = calculate_objects_per_second(result, time.real)
      output_benchmark_result(file, time.real, objects_per_second)
    else
      output_benchmark_result(file, time.real)
    end

  end
  result
end

.measure_referencesObject

Measures reference resolution

Returns:

  • The result of the block



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/expressir/benchmark.rb', line 91

def measure_references
  return yield unless Expressir.configuration.benchmark_enabled?

  if Expressir.configuration.benchmark_ips?
    require "benchmark/ips"

    result = nil
    ::Benchmark.ips do |x|
      x.config(time: Expressir.configuration.benchmark_iterations,
               warmup: Expressir.configuration.benchmark_warmup)

      x.report("Reference resolution") do
        result = yield
      end
    end

  else
    result = nil
    time = ::Benchmark.measure do
      result = yield
    end

    output_benchmark_result("Reference resolution", time.real)
  end
  result
end

.measure_with_cache(path, cache_path = nil) ⇒ Hash

Benchmark with caching

Parameters:

  • path (String)

    Path to the file

  • cache_path (String) (defaults to: nil)

    Path for the cache file

Returns:

  • (Hash)

    Results with timing information



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/expressir/benchmark.rb', line 122

def measure_with_cache(path, cache_path = nil)
  return yield(path) unless Expressir.configuration.benchmark_enabled?

  require "tempfile"
  temp_file = nil

  unless cache_path
    temp_file = Tempfile.new(["expressir_cache", ".bin"])
    cache_path = temp_file.path
  end

  results = {}

  # Measure parsing time
  parsing_time = ::Benchmark.measure do
    results[:repository] = yield(path)
  end
  results[:parsing_time] = parsing_time.real

  # Measure cache writing time
  cache_write_time = ::Benchmark.measure do
    Expressir::Express::Cache.to_file(cache_path, results[:repository])
  end
  results[:cache_write_time] = cache_write_time.real

  # Measure cache reading time
  cache_read_time = ::Benchmark.measure do
    results[:cached_repository] =
      Expressir::Express::Cache.from_file(cache_path)
  end
  results[:cache_read_time] = cache_read_time.real

  # Output results
  if Expressir.configuration.benchmark_verbose?
    puts "Parsing time:      #{results[:parsing_time].round(4)}s"
    puts "Cache write time:  #{results[:cache_write_time].round(4)}s"
    puts "Cache read time:   #{results[:cache_read_time].round(4)}s"

    if results[:repository].respond_to?(:schemas)
      objects = count_objects(results[:repository])
      puts "Total objects:     #{objects}"
      puts "Objects per second (parsing): #{(objects / results[:parsing_time]).round(2)}"
      puts "Objects per second (reading): #{(objects / results[:cache_read_time]).round(2)}"
    end
  end

  # Clean up temp file if we created one
  if temp_file
    temp_file.close
    temp_file.unlink
  end

  results
end