Class: PerformanceTester::CsvLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/performance_tester/csv_logger.rb

Overview

Writes or appends csv data to the filesystem

Instance Attribute Summary

Attributes inherited from Logger

#options, #outcome, #run_name

Instance Method Summary collapse

Methods inherited from Logger

#initialize

Constructor Details

This class inherits a constructor from PerformanceTester::Logger

Instance Method Details

#header_lineObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/performance_tester/csv_logger.rb', line 29

def header_line
  [
    'Run name',
    'Run datetime',
    'Host alias',
    'Path',
    'Content type',
    'Elapsed time'
  ]
end

#logObject



11
12
13
14
15
16
17
18
# File 'lib/performance_tester/csv_logger.rb', line 11

def log
  with_csv do |csv|
    outcome.requests.each do |request|
      csv << request_line(request)
    end
    csv << total_line
  end
end

#pathObject



7
8
9
# File 'lib/performance_tester/csv_logger.rb', line 7

def path
  options.fetch(:path) { 'performance_test.csv' }
end

#request_line(request) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/performance_tester/csv_logger.rb', line 40

def request_line(request)
  [
    run_name,
    outcome.started_at,
    request.host_alias,
    request.path,
    request.content_type,
    request.time_elapsed
  ]
end

#total_lineObject



51
52
53
# File 'lib/performance_tester/csv_logger.rb', line 51

def total_line
  [run_name, outcome.started_at, '', '', '', outcome.total_time_elapsed]
end

#with_csvObject



20
21
22
23
24
25
26
27
# File 'lib/performance_tester/csv_logger.rb', line 20

def with_csv
  append = File.exists?(path)
  flags = append ? 'ab' : 'wb'
  CSV.open(path, flags) do |csv|
    csv << header_line unless append
    yield csv
  end
end