Last active
June 5, 2017 17:17
-
-
Save kei-q/91a1714f468e98dfda6494f159865ba6 to your computer and use it in GitHub Desktop.
fluentd/lib/plugin/output.rbのextract_placeholders調査
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
Warming up -------------------------------------- | |
rewrite2 && full 9.576k i/100ms | |
rewrite && full 9.081k i/100ms | |
orig && full 8.088k i/100ms | |
Calculating ------------------------------------- | |
rewrite2 && full 100.151k (± 4.4%) i/s - 507.528k in 5.078509s | |
rewrite && full 94.131k (± 5.0%) i/s - 472.212k in 5.030891s | |
orig && full 83.549k (± 3.9%) i/s - 420.576k in 5.041819s | |
Comparison: | |
rewrite2 && full: 100151.5 i/s | |
rewrite && full: 94130.8 i/s - same-ish: difference falls within error | |
orig && full: 83548.8 i/s - 1.20x slower | |
Warming up -------------------------------------- | |
rewrite2 && simple 37.682k i/100ms | |
rewrite && simple 35.668k i/100ms | |
orig && simple 26.577k i/100ms | |
Calculating ------------------------------------- | |
rewrite2 && simple 423.057k (± 3.4%) i/s - 2.148M in 5.083244s | |
rewrite && simple 405.711k (± 2.7%) i/s - 2.033M in 5.014899s | |
orig && simple 296.869k (± 2.7%) i/s - 1.488M in 5.017274s | |
Comparison: | |
rewrite2 && simple: 423056.7 i/s | |
rewrite && simple: 405711.0 i/s - same-ish: difference falls within error | |
orig && simple: 296868.6 i/s - 1.43x slower |
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" | |
Metadata = Struct.new(:tag, :variables) | |
CHUNK_KEY_PLACEHOLDER_PATTERN = /\$\{[-_.@a-zA-Z0-9]+\}/ | |
CHUNK_TAG_PLACEHOLDER_PATTERN = /\$\{(tag(?:\[\d+\])?)\}/ | |
FULL_PATTERN = '/mypath/%Y/%m/%d/%H-%M/${tag}/${tag[1]}/${tag[2]}/${key1}/${key2}/tail' | |
SIMPLE_PATTERN = '/mypath' | |
SAMPLE_METADATA = Metadata.new('fluentd.test.output', {key1: 'value1', key2: 'value2'}) | |
def rewrite2(str, metadata, chunk_keys, chunk_key_tag) | |
rvalue = str | |
# ${tag}, ${tag[0]}, ${tag[1]}, ... | |
if chunk_key_tag | |
if rvalue =~ CHUNK_TAG_PLACEHOLDER_PATTERN | |
hash = {'${tag}' => metadata.tag} | |
metadata.tag.split('.').each_with_index do |part, i| | |
hash["${tag[#{i}]}"] = part | |
end | |
rvalue = rvalue.gsub(CHUNK_TAG_PLACEHOLDER_PATTERN, hash) | |
end | |
elsif rvalue =~ CHUNK_TAG_PLACEHOLDER_PATTERN | |
log.warn "tag placeholder '#{$1}' not replaced. tag:#{metadata.tag}, template:#{str}" | |
end | |
# ${a_chunk_key}, ... | |
if chunk_keys.empty? | |
if rvalue =~ CHUNK_KEY_PLACEHOLDER_PATTERN | |
log.warn "chunk key placeholder '#{$1}' not replaced. templace:#{str}" | |
end | |
elsif metadata.variables | |
hash = {'${tag}' => '${tag}'} | |
chunk_keys.each do |key| | |
hash["${#{key}}"] = metadata.variables[key.to_sym] | |
end | |
rvalue = rvalue.gsub(CHUNK_KEY_PLACEHOLDER_PATTERN, hash) | |
end | |
rvalue | |
end | |
def rewrite1(str, metadata, chunk_keys, chunk_key_tag) | |
rvalue = str | |
# ${tag}, ${tag[0]}, ${tag[1]}, ... | |
if chunk_key_tag | |
if rvalue =~ CHUNK_TAG_PLACEHOLDER_PATTERN | |
rvalue = rvalue.gsub('${tag}', metadata.tag) | |
metadata.tag.split('.').each_with_index do |part, i| | |
rvalue = rvalue.gsub("${tag[#{i}]}", part) | |
end | |
rvalue = rvalue.gsub(CHUNK_TAG_PLACEHOLDER_PATTERN, '') | |
end | |
elsif rvalue =~ CHUNK_TAG_PLACEHOLDER_PATTERN | |
log.warn "tag placeholder '#{$1}' not replaced. tag:#{metadata.tag}, template:#{str}" | |
end | |
# ${a_chunk_key}, ... | |
if chunk_keys.empty? | |
if rvalue =~ CHUNK_KEY_PLACEHOLDER_PATTERN | |
log.warn "chunk key placeholder '#{$1}' not replaced. templace:#{str}" | |
end | |
elsif metadata.variables | |
chunk_keys.each do |key| | |
rvalue = rvalue.gsub("${#{key}}", metadata.variables[key.to_sym]) | |
end | |
rvalue = rvalue.gsub(CHUNK_KEY_PLACEHOLDER_PATTERN, {'${tag}' => '${tag}'}) | |
end | |
rvalue | |
end | |
def original(str, metadata, chunk_keys, chunk_key_tag) | |
rvalue = str.dup | |
# ${tag}, ${tag[0]}, ${tag[1]}, ... | |
if chunk_key_tag | |
if str.include?('${tag}') | |
rvalue = rvalue.gsub('${tag}', metadata.tag) | |
end | |
if str =~ CHUNK_TAG_PLACEHOLDER_PATTERN | |
hash = {} | |
metadata.tag.split('.').each_with_index do |part, i| | |
hash["${tag[#{i}]}"] = part | |
end | |
rvalue = rvalue.gsub(CHUNK_TAG_PLACEHOLDER_PATTERN, hash) | |
end | |
if rvalue =~ CHUNK_TAG_PLACEHOLDER_PATTERN | |
log.warn "tag placeholder '#{$1}' not replaced. tag:#{metadata.tag}, template:#{str}" | |
end | |
end | |
# ${a_chunk_key}, ... | |
if !chunk_keys.empty? && metadata.variables | |
hash = {'${tag}' => '${tag}'} # not to erase this wrongly | |
chunk_keys.each do |key| | |
hash["${#{key}}"] = metadata.variables[key.to_sym] | |
end | |
rvalue = rvalue.gsub(CHUNK_KEY_PLACEHOLDER_PATTERN, hash) | |
end | |
if rvalue =~ CHUNK_KEY_PLACEHOLDER_PATTERN | |
log.warn "chunk key placeholder '#{$1}' not replaced. templace:#{str}" | |
end | |
rvalue | |
end | |
metadata = SAMPLE_METADATA | |
chunk_keys = [:key1, :key2] | |
chunk_key_tag = true | |
Benchmark.ips do |x| | |
x.report("rewrite2 && full") { rewrite2(FULL_PATTERN, metadata, chunk_keys, chunk_key_tag) } | |
x.report("rewrite && full") { rewrite1(FULL_PATTERN, metadata, chunk_keys, chunk_key_tag) } | |
x.report("orig && full") { original(FULL_PATTERN, metadata, chunk_keys, chunk_key_tag) } | |
x.compare! | |
end | |
Benchmark.ips do |x| | |
x.report("rewrite2 && simple") { rewrite2(SIMPLE_PATTERN, metadata, chunk_keys, chunk_key_tag) } | |
x.report("rewrite && simple") { rewrite1(SIMPLE_PATTERN, metadata, chunk_keys, chunk_key_tag) } | |
x.report("orig && simple") { original(SIMPLE_PATTERN, metadata, chunk_keys, chunk_key_tag) } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment