Class: PigCI::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/pig_ci/profiler.rb

Direct Known Subclasses

DatabaseRequest, Memory, RequestTime

Defined Under Namespace

Classes: DatabaseRequest, Memory, RequestTime

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i18n_key: nil, log_file: nil, historical_log_file: nil) ⇒ Profiler

Returns a new instance of Profiler.



4
5
6
7
8
9
# File 'lib/pig_ci/profiler.rb', line 4

def initialize(i18n_key: nil, log_file: nil, historical_log_file: nil)
  @i18n_key = i18n_key || self.class.name.underscore.split("/").last
  @log_file = log_file || PigCI.tmp_directory.join("#{@i18n_key}.txt")
  @historical_log_file = historical_log_file || PigCI.tmp_directory.join("#{@i18n_key}.json")
  @log_value = 0
end

Instance Attribute Details

#historical_log_fileObject

Returns the value of attribute historical_log_file.



2
3
4
# File 'lib/pig_ci/profiler.rb', line 2

def historical_log_file
  @historical_log_file
end

#i18n_keyObject

Returns the value of attribute i18n_key.



2
3
4
# File 'lib/pig_ci/profiler.rb', line 2

def i18n_key
  @i18n_key
end

#log_fileObject

Returns the value of attribute log_file.



2
3
4
# File 'lib/pig_ci/profiler.rb', line 2

def log_file
  @log_file
end

#log_valueObject

Returns the value of attribute log_value.



2
3
4
# File 'lib/pig_ci/profiler.rb', line 2

def log_value
  @log_value
end

Instance Method Details

#increment!Object

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/pig_ci/profiler.rb', line 34

def increment!(*)
  raise NotImplementedError
end

#log_request!(request_key) ⇒ Object



19
20
21
22
23
# File 'lib/pig_ci/profiler.rb', line 19

def log_request!(request_key)
  File.open(log_file, "a+") do |f|
    f.puts([request_key, log_value].join("|"))
  end
end

#reset!Object



15
16
17
# File 'lib/pig_ci/profiler.rb', line 15

def reset!
  @log_value = 0
end

#save!Object



25
26
27
28
29
30
31
32
# File 'lib/pig_ci/profiler.rb', line 25

def save!
  historical_data = PigCI::Metric::Historical.new(historical_log_file: @historical_log_file)
  historical_data.add_change_percentage_and_append!(
    timestamp: PigCI.run_timestamp,
    metric: i18n_key,
    data: PigCI::Metric::Current.new(log_file: log_file).to_h
  )
end

#setup!Object



11
12
13
# File 'lib/pig_ci/profiler.rb', line 11

def setup!
  File.open(log_file, "w") { |file| file.truncate(0) }
end