Created
December 2, 2016 17:37
-
-
Save guigs/9bf8294571624c146ffaa52b347406a5 to your computer and use it in GitHub Desktop.
Coverband cache ignored files benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
require 'benchmark/memory' | |
require 'set' | |
SIZE = 10_000 | |
FILES = Array.new(100) do | |
dir = rand(2) == 1 ? Dir.pwd : '/other' | |
dir << '/gems' unless rand(3) == 1 | |
name = [*('A'..'Z')].sample(8).join | |
"#{dir}/#{name}" | |
end | |
def get_file | |
FILES.sample | |
end | |
def get_number | |
rand(200) | |
end | |
@ignore_patterns = ['internal:prelude', 'gems'] | |
@project_directory = File.expand_path(Dir.pwd) | |
def track_file?(file) | |
!@ignore_patterns.any?{ |pattern| file.include?(pattern) } && file.start_with?(@project_directory) | |
end | |
def reject | |
track = {} | |
SIZE.times do | |
file = get_file | |
n = get_number | |
track[file] ||= Hash.new(0) | |
track[file][n] += 1 | |
end | |
track.reject!{|file, _lines| !track_file?(file) } | |
end | |
def without_cache | |
track = {} | |
SIZE.times do | |
file = get_file | |
n = get_number | |
if track_file?(file) | |
track[file] ||= Hash.new(0) | |
track[file][n] += 1 | |
end | |
end | |
end | |
def with_cache | |
track = {} | |
ignored = Set.new | |
SIZE.times do | |
file = get_file | |
n = get_number | |
if !ignored.include?(file) | |
if track_file?(file) | |
track[file] ||= Hash.new(0) | |
track[file][n] += 1 | |
else | |
ignored << file | |
end | |
end | |
end | |
end | |
def benchmark(x) | |
x.report("reject: ") { reject } | |
x.report("without_cache: ") { without_cache } | |
x.report("with_cache: ") { with_cache } | |
x.compare! | |
end | |
Benchmark.memory(&method(:benchmark)) | |
Benchmark.ips(&method(:benchmark)) | |
# Results | |
# Memory Comparison: | |
# without_cache: : 45392 allocated | |
# with_cache: : 52216 allocated - 1.15x more | |
# reject: : 413731 allocated - 9.11x more | |
# | |
# Speed Comparison: | |
# with_cache: : 144.1 i/s | |
# without_cache: : 86.1 i/s - 1.67x slower | |
# reject: : 79.4 i/s - 1.81x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment