Skip to content

Commit

Permalink
Merge pull request #25 from andrewvc/match_direct
Browse files Browse the repository at this point in the history
Add new `execute` method for getting a Regexp MatchData
  • Loading branch information
jordansissel committed Aug 1, 2016
2 parents 0b74592 + eb9eaba commit e3ba22f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELIST
@@ -1,3 +1,6 @@
* 0.11.3
- Add optimized match_direct method

* 0.10.7
- add Grok::PatternError exception class
- raise PatternError when a pattern contains an unknown %{expansion}
Expand Down
2 changes: 1 addition & 1 deletion grok.gemspec
Expand Up @@ -5,7 +5,7 @@ Gem::Specification.new do |spec|

#svnrev = %x{svn info}.split("\n").grep(/Revision:/).first.split(" ").last.to_i
spec.name = "jls-grok"
spec.version = "0.11.2"
spec.version = "0.11.3"

spec.summary = "grok bindings for ruby"
spec.description = "Grok ruby bindings - pattern match/extraction tool"
Expand Down
23 changes: 18 additions & 5 deletions lib/grok-pure.rb
@@ -1,4 +1,4 @@
require "rubygems"
require "rubygems"
require "logger"
require "cabin"
require "grok/pure/discovery"
Expand Down Expand Up @@ -172,21 +172,34 @@ def match(text)
end
end # def match

# Returns the matched regexp object directly for performance at the
# cost of usability.
#
# Returns MatchData on success, nil on failure.
#
# Can be used with #capture
def execute(text)
@regexp.match(text)
end

# Optimized match and capture instead of calling them separately
# This could be DRYed up by using #match and #capture directly
# but there's a bit of a worry that that may lower perf.
# This should be benchmarked!
def match_and_capture(text)
match = @regexp.match(text)
match = execute(text)
if match
@logger.debug? and @logger.debug("Regexp match object", :names => match.names,
:captures => match.captures)
@captures_func.call(match) { |k,v| yield k,v }
capture(match) {|k,v| yield k,v}
return true
else
return false
end
end # def match_and_capture

def capture(match, block)
@captures_func.call(match) { |k,v| block.call k,v }
def capture(match, &block)
@captures_func.call(match,&block)
end # def capture

public
Expand Down
2 changes: 1 addition & 1 deletion lib/grok/pure/match.rb
Expand Up @@ -12,7 +12,7 @@ def initialize

public
def each_capture(&block)
@grok.capture(@match, block)
@grok.capture(@match, &block)
end # def each_capture

public
Expand Down
8 changes: 4 additions & 4 deletions test/pure-ruby/benchmark_pattern.rb
Expand Up @@ -14,16 +14,16 @@ def init_grok(named_captures_only)
end

Benchmark.bmbm(10) do |bm|
bm.report("100k Named Captures On") do
bm.report("10m Named Captures On") do
grok = init_grok(true)
(1..100000).each do
(1..10_000_000).each do
match = grok.match(@log_line)
match.each_capture { |name, val| }
end
end
bm.report("100k Named Captures Off") do
bm.report("10m Named Captures Off") do
grok = init_grok(false)
(1..100000).each do
(1..10_000_000).each do
match = grok.match(@log_line)
match.each_capture { |name, val| }
end
Expand Down

0 comments on commit e3ba22f

Please sign in to comment.