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 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 88 89 90 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 117 118 119 120 121 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
|
#!/usr/bin/env ruby
require "#{File.dirname(__FILE__)}/helper.rb"
def compare_scrub_methods
snip = "<div>foo</div><foo>fuxx <b>quux</b></foo><script>i have a chair</script>"
puts "starting with:\n#{snip}"
puts
puts RailsSanitize.new.sanitize(snip) # => Rails.sanitize / scrub!(:prune).to_s
puts Loofah::Helpers.sanitize(snip)
puts "--"
puts RailsSanitize.new.strip_tags(snip) # => Rails.strip_tags / parse().text
puts Loofah::Helpers.strip_tags(snip)
puts "--"
puts Sanitize.clean(snip, Sanitize::Config::RELAXED) # => scrub!(:strip).to_s
puts Loofah.scrub_fragment(snip, :strip).to_s
puts "--"
puts HTML5libSanitize.new.sanitize(snip) # => scrub!(:escape).to_s
puts Loofah.scrub_fragment(snip, :escape).to_s
puts "--"
puts HTMLFilter.new.filter(snip)
puts Loofah.scrub_fragment(snip, :strip).to_s
puts
end
module TestSet
def test_set options={}
scale = options[:rehearse] ? 10 : 1
puts self.class.name
n = 100 / scale
puts " Large document, #{BIG_FILE.length} bytes (x#{n})"
bench BIG_FILE, n, false
puts
n = 1000 / scale
puts " Small fragment, #{FRAGMENT.length} bytes (x#{n})"
bench FRAGMENT, n, true
puts
n = 10_000 / scale
puts " Text snippet, #{SNIPPET.length} bytes (x#{n})"
bench SNIPPET, n, true
puts
end
end
class HeadToHead < Measure
end
class HeadToHeadRailsSanitize < Measure
include TestSet
def bench(content, ntimes, fragment_p)
clear_measure
measure "Loofah::Helpers.sanitize", ntimes do
Loofah::Helpers.sanitize content
end
sanitizer = RailsSanitize.new
measure "ActionView sanitize", ntimes do
sanitizer.sanitize(content)
end
end
end
class HeadToHeadRailsStripTags < Measure
include TestSet
def bench(content, ntimes, fragment_p)
clear_measure
measure "Loofah::Helpers.strip_tags", ntimes do
Loofah::Helpers.strip_tags content
end
sanitizer = RailsSanitize.new
measure "ActionView strip_tags", ntimes do
sanitizer.strip_tags(content)
end
end
end
class HeadToHeadSanitizerSanitize < Measure
include TestSet
def bench(content, ntimes, fragment_p)
clear_measure
measure "Loofah :strip", ntimes do
if fragment_p
Loofah.scrub_fragment(content, :strip).to_s
else
Loofah.scrub_document(content, :strip).to_s
end
end
measure "Sanitize.clean", ntimes do
Sanitize.clean(content, Sanitize::Config::RELAXED)
end
end
end
class HeadToHeadHtml5LibSanitize < Measure
include TestSet
def bench(content, ntimes, fragment_p)
clear_measure
measure "Loofah :escape", ntimes do
if fragment_p
Loofah.scrub_fragment(content, :escape).to_s
else
Loofah.scrub_document(content, :escape).to_s
end
end
html5_sanitizer = HTML5libSanitize.new
measure "HTML5lib.sanitize", ntimes do
html5_sanitizer.sanitize(content)
end
end
end
class HeadToHeadHTMLFilter < Measure
include TestSet
def bench(content, ntimes, fragment_p)
clear_measure
measure "Loofah::Helpers.sanitize", ntimes do
Loofah::Helpers.sanitize content
end
sanitizer = HTMLFilter.new
measure "HTMLFilter.filter", ntimes do
sanitizer.filter(content)
end
end
end
puts "Nokogiri version: #{Nokogiri::VERSION_INFO.inspect}"
puts "Loofah version: #{Loofah::VERSION.inspect}"
benches = []
benches << HeadToHeadRailsSanitize.new
benches << HeadToHeadRailsStripTags.new
benches << HeadToHeadSanitizerSanitize.new
benches << HeadToHeadHtml5LibSanitize.new
benches << HeadToHeadHTMLFilter.new
puts "---------- rehearsal ----------"
benches.each { |bench| bench.test_set :rehearse => true }
puts "---------- realsies ----------"
benches.each { |bench| bench.test_set }
|