Skip to content

Commit

Permalink
chore(benchmark): enhance the codes for measuring performance between…
Browse files Browse the repository at this point in the history
… each version

[ci skip]
  • Loading branch information
tonytonyjan committed Jan 14, 2018
1 parent 868609a commit 4b05c43
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
14 changes: 1 addition & 13 deletions benchmark/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'bundler'
Bundler.setup(:benchmark)

SAMPLES = {
ascii: [
%w[al al], %w[martha marhta], %w[jones johnson], %w[abcvwxyz cabvwxyz],
%w[dwayne duane], %w[dixon dicksonx], %w[fvie ten]
].freeze,
utf8: [
%w[馬英九 馬英丸], %w[蔡英文 蔡中文], %w[簡煒航 簡偉航], %w[焦玟綾 焦紋綾],
%w[眼球中央電視台 眼球中英電視台], %w[床前明月光 床前日月光],
%w[海水退了就知道誰沒穿褲子 海水退了就知道誰沒穿襪子],
%w[阿里山的姑娘美如水 阿里山的姑娘沒乳水]
].freeze
}.freeze
require 'samples'

def gem_name_with_version(gem)
"#{gem} (#{Gem.loaded_specs[gem].version})"
Expand Down
41 changes: 24 additions & 17 deletions benchmark/measure.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# frozen_string_literal: true

require File.expand_path('../env', __FILE__)
require 'jaro_winkler'
require File.expand_path('../samples', __FILE__)
gem 'jaro_winkler', ENV['JARO_WINKLER_VERSION'] || ARGV[0] || raise('missing ENV["JARO_WINKLER_VERSION"]')
require 'benchmark'
require 'csv'

csv = CSV.new($stdout)
n = 100_000
version = Gem::Version.new(Gem.loaded_specs["jaro_winkler"].version)

jobs = case JaroWinkler::VERSION
when /\A1\.1/
{
ascii: -> { n.times { SAMPLES[:ascii].each { |str1, str2| JaroWinkler.c_distance(str1, str2) } } }
}
when /\A1\.[2-3]/
{
ascii: -> { n.times { SAMPLES[:ascii].each { |str1, str2| JaroWinkler.c_distance(str1, str2) } } },
utf8: -> { n.times { SAMPLES[:utf8].each { |str1, str2| JaroWinkler.c_distance(str1, str2) } } }
}
if version >= Gem::Version.new('1.1.0') && version < Gem::Version.new('1.2.4')
require 'jaro_winkler.bundle'
else
{
ascii: -> { n.times { SAMPLES[:ascii].each { |str1, str2| JaroWinkler.distance(str1, str2) } } },
utf8: -> { n.times { SAMPLES[:utf8].each { |str1, str2| JaroWinkler.distance(str1, str2) } } }
}
require 'jaro_winkler'
end

jobs = {
ascii: -> { n.times { SAMPLES[:ascii].each { |str1, str2| JaroWinkler.jaro_winkler_distance(str1, str2) } } }
}

if version >= Gem::Version.new('1.1.0')
jobs[:ascii] = -> { n.times { SAMPLES[:ascii].each { |str1, str2| JaroWinkler.c_distance(str1, str2) } } }
end

if version >= Gem::Version.new('1.2.0')
jobs[:utf8] = -> { n.times { SAMPLES[:utf8].each { |str1, str2| JaroWinkler.c_distance(str1, str2) } } }
end

if version >= Gem::Version.new('1.4.0')
jobs[:ascii] = -> { n.times { SAMPLES[:ascii].each { |str1, str2| JaroWinkler.distance(str1, str2) } } }
jobs[:utf8] = -> { n.times { SAMPLES[:utf8].each { |str1, str2| JaroWinkler.distance(str1, str2) } } }
end

# rehearsal
Expand All @@ -35,5 +42,5 @@
GC.start
tms = Benchmark.measure(label, &job)
# version,label,utime,stime,cutime,cstime,real
csv << [JaroWinkler::VERSION, *tms.to_a]
csv << [version, *tms.to_a]
end
12 changes: 12 additions & 0 deletions benchmark/samples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SAMPLES = {
ascii: [
%w[al al], %w[martha marhta], %w[jones johnson], %w[abcvwxyz cabvwxyz],
%w[dwayne duane], %w[dixon dicksonx], %w[fvie ten]
].freeze,
utf8: [
%w[馬英九 馬英丸], %w[蔡英文 蔡中文], %w[簡煒航 簡偉航], %w[焦玟綾 焦紋綾],
%w[眼球中央電視台 眼球中英電視台], %w[床前明月光 床前日月光],
%w[海水退了就知道誰沒穿褲子 海水退了就知道誰沒穿襪子],
%w[阿里山的姑娘美如水 阿里山的姑娘沒乳水]
].freeze
}.freeze
9 changes: 9 additions & 0 deletions bin/measure
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
echo version,label,utime,stime,cutime,cstime,real
gem search -ear jaro_winkler \
| grep -o '\((.*)\)$' \
| tr -d '() ' \
| tr ',' "\n" \
| grep -o '\d\.\d\.\d' \
| sort \
| xargs -I{} ruby "`dirname $0`"/../benchmark/measure.rb '{}'

0 comments on commit 4b05c43

Please sign in to comment.