Class: Perfer::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/perfer/session.rb

Defined Under Namespace

Classes: DSL, MetadataSetter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, name = nil, &block) ⇒ Session

Returns a new instance of Session.



5
6
7
8
9
10
11
12
13
14
# File 'lib/perfer/session.rb', line 5

def initialize(file, name = nil, &block)
  @file = file
  @name = name || file.base.to_s
  @store = Store.for_session(self)
  @results = nil # not an Array, so it errors out if we forgot to load

  run(&block) if block_given?

  Perfer.sessions << self
end

Instance Attribute Details

#current_jobObject

Returns the value of attribute current_job.



4
5
6
# File 'lib/perfer/session.rb', line 4

def current_job
  @current_job
end

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/perfer/session.rb', line 3

def file
  @file
end

#jobsObject (readonly)

Returns the value of attribute jobs.



3
4
5
# File 'lib/perfer/session.rb', line 3

def jobs
  @jobs
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/perfer/session.rb', line 3

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/perfer/session.rb', line 3

def name
  @name
end

#next_job_metadataObject

Returns the value of attribute next_job_metadata.



4
5
6
# File 'lib/perfer/session.rb', line 4

def 
  @next_job_metadata
end

#resultsObject (readonly)

Returns the value of attribute results.



3
4
5
# File 'lib/perfer/session.rb', line 3

def results
  @results
end

#storeObject (readonly)

Returns the value of attribute store.



3
4
5
# File 'lib/perfer/session.rb', line 3

def store
  @store
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/perfer/session.rb', line 3

def type
  @type
end

Instance Method Details

#add_bench_file_checksumObject



48
49
50
51
52
53
54
# File 'lib/perfer/session.rb', line 48

def add_bench_file_checksum
  checksum = Digest::SHA1.hexdigest(@file.binread)
  if checksum.respond_to?(:encoding) and checksum.encoding != Encoding::ASCII
    checksum.force_encoding(Encoding::ASCII)
  end
  @metadata[:bench_file_checksum] = checksum
end

#add_config_metadataObject



35
36
37
38
39
# File 'lib/perfer/session.rb', line 35

def 
  config = Perfer.configuration.to_hash
  config.delete(:verbose) # Not relevant to save
  @metadata.merge!(config)
end

#add_git_metadataObject



41
42
43
44
45
46
# File 'lib/perfer/session.rb', line 41

def 
  if Git.repository?
    @metadata[:git_branch] = Git.current_branch
    @metadata[:git_commit] = Git.current_commit
  end
end

#add_job(job_type, title, *args, &block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/perfer/session.rb', line 96

def add_job(job_type, title, *args, &block)
  check_benchmark_type(job_type)
  check_unique_job_title(title)
  @jobs << job = job_type.new(self, title, *args, &block)
  job.run
  @next_job_metadata = nil
end

#add_result(result) ⇒ Object



60
61
62
63
# File 'lib/perfer/session.rb', line 60

def add_result(result)
  @results_to_save << result
  ResultsFormatter.new(result).report
end

#graphObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/perfer/session.rb', line 84

def graph
  load_results
  # consider only first job for now
  last_runtime = @results.last[:runtime]
  job = @results.reverse_each.take_while { |r| r[:runtime] == last_runtime }.last[:job]
  data = @results.select { |r| r[:job] == job }.
                  sort_by { |r| r[:ruby] }.
                  chunk { |r| r[:ruby] }.
                  map { |ruby, results| results.last }
  RGrapher.new.boxplot(data)
end

#load_resultsObject



56
57
58
# File 'lib/perfer/session.rb', line 56

def load_results
  @results = @store.load
end

#report(options = {}) ⇒ Object



79
80
81
82
# File 'lib/perfer/session.rb', line 79

def report(options = {})
  load_results
  SessionFormatter.new(self).report(options)
end

#run(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/perfer/session.rb', line 65

def run(&block)
  setup_for_run
  puts "Session #{@name} with #{@metadata[:ruby]}"
  print "Taking #{Perfer.configuration.measurements} measurements of"
  puts " at least #{Perfer.configuration.minimal_time}s"

  block.call DSL.new(self)

  @results_to_save.each { |result|
    @store.append(result)
  }
  @results_to_save.clear
end

#setup_for_runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/perfer/session.rb', line 16

def setup_for_run
  @jobs = []
  @type = nil # will be decided by API usage (iterate/bench)
  @results_to_save = []
  @next_job_metadata = nil

  @metadata = {
    :file => @file.path,
    :session => @name,
    :ruby => RUBY_DESCRIPTION,
    :command_line => Platform.command_line,
    :run_time => Time.now
  }
  
  
  add_bench_file_checksum
  @metadata.freeze
end