diff options
author | Austin Ziegler <austin@zieglers.ca> | 2014-06-04 01:16:56 -0400 |
---|---|---|
committer | Austin Ziegler <austin@zieglers.ca> | 2014-10-06 12:17:00 -0400 |
commit | e5fbf251cb6c6c7f1d3f860c9b1406afa509c14e (patch) | |
tree | a31bd2cef166d69cbab181bd8d4b62d05ef2bbe1 | |
parent | 159f879f6f5b5f7f1a7bd57fc88af0bde0bf1f52 (diff) | |
download | mime-types-e5fbf251cb6c6c7f1d3f860c9b1406afa509c14e.tar.gz |
Reorganize benchmarking.
-rw-r--r-- | Rakefile | 90 | ||||
-rw-r--r-- | support/benchmarker.rb | 55 | ||||
-rw-r--r-- | support/benchmarks/load.rb | 55 |
3 files changed, 120 insertions, 80 deletions
@@ -32,12 +32,68 @@ spec = Hoe.spec 'mime-types' do self.extra_dev_deps << ['coveralls', '~> 0.7'] end -desc 'Benchmark' -task :benchmark, :repeats do |t, args| - $LOAD_PATH.unshift('support') - require 'benchmarker' +task :support do + %w(lib support).each { |path| + $LOAD_PATH.unshift(File.join(Rake.application.original_dir, path)) + } +end + +namespace :benchmark do + desc 'Benchmark Load Times' + task :load, [ :repeats ] => :support do |t, args| + require 'benchmarks/load' + Benchmarks::Load.report(File.join(Rake.application.original_dir, 'lib'), + args.repeats) + end + + task objects: :support do + GC.start + objects_before = ObjectSpace.count_objects + + require "mime/types" + GC.start + objects_after = ObjectSpace.count_objects + for key, delta in objects_before.keys.grep(/T_/).map { |key| [key, objects_after[key] - objects_before[key]] }.sort_by { |key, delta| -delta } + printf "%10s +%6d\n", key, delta + end + end - Benchmarker.benchmark(args.repeats) + task :type_for, [ :repeated ] => :support do |t, args| + repeats = args.repeats.to_i + repeats = 5000 if repeats <= 0 + + require 'mime/types' + + extensions = MIME::Types.send(:__types__). + instance_variable_get(:@extension_index).keys + + require 'benchmark' + Benchmark.bm(17) do |mark| + mark.report("Normal:") { + repeats.times { extensions.each { |ext| MIME::Types.type_for(ext) } } + } + + class MIME::Types + def type_for(filename, platform = false) + types = Array(filename).flat_map { |fn| + @extension_index[fn.chomp.downcase.split(/\./o).last] + }.compact.sort { |a, b| a.priority_compare(b) }.uniq + + if platform + MIME.deprecated(self, __method__, + "using the platform parameter") + types.select(&:platform?) + else + types + end + end + end + + mark.report("Split:") { + repeats.times { extensions.each { |ext| MIME::Types.type_for(ext) } } + } + end + end end namespace :test do @@ -65,15 +121,13 @@ end namespace :mime do desc "Download the current MIME type registrations from IANA." - task :iana, :destination do |t, args| - $LOAD_PATH.unshift('support') + task :iana, [ :destination ] => :support do |t, args| require 'iana_registry' IANARegistry.download(to: args.destination) end desc "Download the current MIME type configuration from Apache." - task :apache, :destination do |t, args| - $LOAD_PATH.unshift('support') + task :apache, [ :destination ] => :support do |t, args| require 'apache_mime_types' ApacheMIMETypes.download(to: args.destination) end @@ -84,8 +138,7 @@ Rake::Task['gem'].prerequisites.unshift("convert:yaml:json") namespace :convert do namespace :yaml do desc "Convert from YAML to JSON" - task :json, :source, :destination, :multiple_files do |t, args| - $LOAD_PATH.unshift('support') + task :json, [ :source, :destination, :multiple_files ] => :support do |t, args| require 'convert' Convert.from_yaml_to_json(args) end @@ -93,26 +146,13 @@ namespace :convert do namespace :json do desc "Convert from JSON to YAML" - task :yaml, :source, :destination, :multiple_files do |t, args| - $LOAD_PATH.unshift('support') + task :yaml, [ :source, :destination, :multiple_files ] => :support do |t, args| require 'convert' Convert.from_json_to_yaml(args) end end end -task :objects do - GC.start - objects_before = ObjectSpace.count_objects - $:.unshift File.expand_path("../lib", __FILE__) - require "mime/types" - GC.start - objects_after = ObjectSpace.count_objects - for key, delta in objects_before.keys.grep(/T_/).map { |key| [key, objects_after[key] - objects_before[key]] }.sort_by { |key, delta| -delta } - printf "%10s +%6d\n", key, delta - end -end - Rake::Task['travis'].prerequisites.replace(%w(test:coveralls)) # vim: syntax=ruby diff --git a/support/benchmarker.rb b/support/benchmarker.rb deleted file mode 100644 index 7765e42..0000000 --- a/support/benchmarker.rb +++ /dev/null @@ -1,55 +0,0 @@ -# -*- ruby encoding: utf-8 -*- - -$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'benchmark' - -class Benchmarker - def self.benchmark(repeats) - new(repeats.to_i).benchmark - end - - def initialize(repeats = nil) - @cache_file = File.expand_path('../cache.mtc', __FILE__) - @repeats = repeats.to_i - @repeats = 50 if repeats.zero? - end - - def reload_mime_types(repeats = 1, force_load = false) - path = File.expand_path('../../lib', __FILE__) - - repeats.times { - Object.send(:remove_const, :MIME) if defined? MIME - $LOADED_FEATURES.delete_if { |n| n =~ /#{path}/ } - require 'mime/types' - MIME::Types.send(:__types__) if force_load - } - end - - def benchmark - remove_cache - - Benchmark.bm(17) do |mark| - mark.report("Normal:") { reload_mime_types(@repeats) } - - ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes' - mark.report("Lazy:") { reload_mime_types(@repeats) } - mark.report("Lazy+Load:") { reload_mime_types(@repeats, true) } - - ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD') - - ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file - reload_mime_types - - mark.report("Cached:") { reload_mime_types(@repeats) } - ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes' - mark.report("Lazy Cached:") { reload_mime_types(@repeats) } - mark.report("Lazy Cached Load:") { reload_mime_types(@repeats, true) } - end - ensure - remove_cache - end - - def remove_cache - File.unlink(@cache_file) if File.exist?(@cache_file) - end -end diff --git a/support/benchmarks/load.rb b/support/benchmarks/load.rb new file mode 100644 index 0000000..8a36072 --- /dev/null +++ b/support/benchmarks/load.rb @@ -0,0 +1,55 @@ +# -*- ruby encoding: utf-8 -*- + +require 'benchmark' + +module Benchmarks + class Load + def self.report(load_path, repeats) + new(load_path, repeats.to_i).report + end + + def initialize(load_path, repeats = nil) + @cache_file = File.expand_path('../cache.mtc', __FILE__) + @repeats = repeats.to_i + @repeats = 50 if repeats <= 0 + @load_path = load_path + end + + def reload_mime_types(repeats = 1, force_load = false) + repeats.times { + Object.send(:remove_const, :MIME) if defined? ::MIME + $LOADED_FEATURES.delete_if { |n| n =~ /#{@load_path}/ } + require 'mime/types' + ::MIME::Types.send(:__types__) if force_load + } + end + + def report + remove_cache + + Benchmark.bm(17) do |mark| + mark.report("Normal:") { reload_mime_types(@repeats) } + + ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes' + mark.report("Lazy:") { reload_mime_types(@repeats) } + mark.report("Lazy+Load:") { reload_mime_types(@repeats, true) } + + ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD') + + ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file + reload_mime_types + + mark.report("Cached:") { reload_mime_types(@repeats) } + ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes' + mark.report("Lazy Cached:") { reload_mime_types(@repeats) } + mark.report("Lazy Cached Load:") { reload_mime_types(@repeats, true) } + end + ensure + remove_cache + end + + def remove_cache + File.unlink(@cache_file) if File.exist?(@cache_file) + end + end +end |