Class: Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/analyzer.rb,
lib/analyzer/rss.rb,
lib/analyzer/version.rb

Defined Under Namespace

Classes: Rss

Constant Summary collapse

VERSION =
'1.0.5'

Instance Method Summary collapse

Constructor Details

#initialize(*test_files, bootstrap: nil, lib: nil, n: nil) ⇒ Analyzer

Returns a new instance of Analyzer.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/analyzer.rb', line 8

def initialize(*test_files, bootstrap: nil, lib: nil, n: nil)
  @bootstrap = bootstrap
  @lib = lib
  @n = n || [1]
  @test_files = test_files
  
  @keys = test_files.map { |fn| File.basename(fn, '.*') }
  depth = -1
  while @keys.uniq.length < @keys.length
    @keys = test_files.map { |fn| ( File.dirname(fn).split(File::SEPARATOR)[depth..-1] + [File.basename(fn, '.*')] ).join(File::SEPARATOR) }
    depth -= 1
  end
end

Instance Method Details

#bootstrapObject



30
31
32
33
34
35
36
37
# File 'lib/analyzer.rb', line 30

def bootstrap
  if @bootstrap
    system("ruby", "-r", "#{File.expand_path(@lib)}", File.expand_path(@bootstrap))
    if $?.exitstatus > 0
      exit 1
    end
  end
end

#calculate_results_for(type, src, n = 1) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/analyzer.rb', line 54

def calculate_results_for(type, src, n=1)
  lib = @lib ? File.read(File.expand_path(@lib)) : ''
  setup = "N=#{n}\n"
  
  if src.index("\n__SETUP__\n")
    src = src.split("\n__SETUP__\n")
    setup << src[0]
    src = src[1]
  end
  
  analyzer_dir = File.expand_path("../", __FILE__)
  template = File.read(File.expand_path("../templates/#{type}.rb.erb", __FILE__))
  _script = ''
  ERB.new(template, nil, nil, "_script").result(binding)
  results = `ruby <<'TESTSCRIPT'\n#{_script}\nTESTSCRIPT`

  if $? == 0
    JSON.parse(results)
  else
    puts results
    exit
  end
end

#escape(value) ⇒ Object



100
101
102
# File 'lib/analyzer.rb', line 100

def escape(value)
  value.gsub('_', '\\\\_')
end

#key(value) ⇒ Object



22
23
24
# File 'lib/analyzer.rb', line 22

def key(value)
  @keys[@test_files.index(value)]
end

#output_gc_results(results, output) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/analyzer.rb', line 82

def output_gc_results(results, output)
  last_row = results.shift
  first_row = last_row
  results.each do |row|
    output.puts [row[0], row[0] - first_row[0], row[1], row[2], row[1] - last_row[1], row[2] - last_row[2], row[3], row[4], row[3] - last_row[3], row[4] - last_row[4]].join(' ')
    last_row = row
  end
end

#output_ips_results(results, output) ⇒ Object



78
79
80
# File 'lib/analyzer.rb', line 78

def output_ips_results(results, output)
  output.write(results.join("\n"))
end

#output_rss_results(results, output) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/analyzer.rb', line 91

def output_rss_results(results, output)
  last_row = results.shift
  first_row = last_row
  results.each do |row|
    output.puts [row[0], row[0] - first_row[0], row[1], row[1] - last_row[1]].join(' ')
    last_row = row
  end
end

#plot(output) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/analyzer.rb', line 104

def plot(output)
  Dir.mktmpdir do |dir|
    write_results_for('ips', dir)
    write_results_for('gc', dir)
    write_results_for('rss', dir)
    
    template = File.read(File.expand_path('../templates/gnuplot.sh.erb', __FILE__))
    _script = ''
    data_dir = dir
    ERB.new(template, nil, nil, "_script").result(binding)
    `gnuplot <<-GNUPLOT\n#{_script}\nGNUPLOT`
  end
end

#testsObject



26
27
28
# File 'lib/analyzer.rb', line 26

def tests
  @test_files.map{ |f| key(f) }
end

#write_results_for(type, dir) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/analyzer.rb', line 39

def write_results_for(type, dir)
  @n.each do |n|
    @test_files.each do |src|
      if key(src).index(File::SEPARATOR)
        FileUtils.mkdir_p(File.join(dir, type, n.to_s, File.dirname(key(src))))
      else
        FileUtils.mkdir_p(File.join(dir, type, n.to_s))
      end
      File.open(File.join(dir, type, n.to_s, key(src)), 'w') do |file|
        send(:"output_#{type}_results", calculate_results_for(type, File.read(src), n), file)
      end
    end
  end
end