diff options
author | Austin Ziegler <austin@zieglers.ca> | 2019-04-03 16:47:19 -0400 |
---|---|---|
committer | Austin Ziegler <austin@zieglers.ca> | 2019-04-03 17:07:23 -0400 |
commit | fd0230eef88d397e72405424ab32a6f47331bc3e (patch) | |
tree | 70496c46e700d4cd25f30c4cf0d0631dbb661d5e /support | |
parent | 1c8e13c7cd6b187c564afe8a05cadf51d8880fe1 (diff) | |
download | mime-types-fd0230eef88d397e72405424ab32a6f47331bc3e.tar.gz |
Add benchmarks for memory profiler
Diffstat (limited to 'support')
-rw-r--r-- | support/benchmarks/memory_profiler.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/support/benchmarks/memory_profiler.rb b/support/benchmarks/memory_profiler.rb new file mode 100644 index 0000000..c5ae405 --- /dev/null +++ b/support/benchmarks/memory_profiler.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +if RUBY_VERSION < '2.3' + warn 'Cannot use memory_profiler on less than 2.3.8.' + exit 1 +end + +begin + require 'memory_profiler' +rescue Exception # rubocop:disable Lint/RescueException + warn "Memory profiling requires the gem 'memory_profiler'." + exit 1 +end + +module Benchmarks + # Use Memory Profiler to profile memory + class ProfileMemory + def self.report(columnar: false, full: false, mime_types_only: false, top_x: nil) + new( + columnar: columnar, + full: full, + mime_types_only: mime_types_only, + top_x: top_x + ).report + end + + def initialize(columnar:, full:, mime_types_only:, top_x:) + @columnar = !!columnar + @full = !!full + @mime_types_only = !!mime_types_only + + @top_x = top_x + + return unless @top_x + + @top_x = top_x.to_i + @top_x = 10 if @top_x <= 0 + end + + def report + collect.pretty_print + end + + private + + def collect + report_params = { + top: @top_x, + allow_files: @mime_types_only ? %r{mime-types/lib} : nil + }.delete_if { |_k, v| v.nil? } + + if @columnar + MemoryProfiler.report(**report_params) do + require 'mime/types' + MIME::Types.first.to_h if @full + end + else + MemoryProfiler.report(**report_params) do + require 'mime/types/full' + end + end + end + end +end |