Class: Droid::Monitor::Cpu

Inherits:
Adb
  • Object
show all
Includes:
Utils
Defined in:
lib/droid/monitor/cpu.rb

Instance Attribute Summary collapse

Attributes inherited from Adb

#api_level, #device_serial, #package

Instance Method Summary collapse

Methods included from Utils

#merge_current_time, #save

Methods inherited from Adb

#device_sdk_version, #dump_cpuinfo, #dump_meminfo

Constructor Details

#initialize(opts = {}) ⇒ Cpu

Returns a new instance of Cpu.



14
15
16
17
# File 'lib/droid/monitor/cpu.rb', line 14

def initialize(opts = {})
  super(opts)
  @cpu_usage = []
end

Instance Attribute Details

#cpu_usageObject (readonly)

Returns the value of attribute cpu_usage.



10
11
12
# File 'lib/droid/monitor/cpu.rb', line 10

def cpu_usage
  @cpu_usage
end

Instance Method Details

#clear_cpu_usageObject



19
20
21
# File 'lib/droid/monitor/cpu.rb', line 19

def clear_cpu_usage
  @cpu_usage = []
end

#create_graph(data_file_path, graph_opts = {}, output_file_path) ⇒ Object



87
88
89
90
# File 'lib/droid/monitor/cpu.rb', line 87

def create_graph(data_file_path, graph_opts = {}, output_file_path)
  self.save(Droid::Monitor::GoogleApiTemplate.create_graph(data_file_path, graph_opts),
            output_file_path)
end

#dump_cpu_usage(dump_data) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/droid/monitor/cpu.rb', line 23

def dump_cpu_usage(dump_data)
  dump = dump_data.scan(/^.*#{self.package}.*$/).map(&:strip).first.split(/\s/).reject(&:empty?)
  fail 'no package' if /^Load:$/ =~ dump[0]
  dump
rescue StandardError => e
  puts e
  []
end

#export_as_google_api_format(from_cpu_usage) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/droid/monitor/cpu.rb', line 65

def export_as_google_api_format(from_cpu_usage)
  google_api_data_format = empty_google_api_format

  from_cpu_usage.each do |hash|

    a_google_api_data_format = {
      c: [
        { v: hash[:time] },
        { v: hash[:total_cpu].delete('%').to_f },
        { v: hash[:user].delete('%').to_f },
        { v: hash[:kernel].delete('%').to_f },
      ]
    }
    google_api_data_format[:rows].push(a_google_api_data_format)
  end

  JSON.generate google_api_data_format
end

#save_cpu_usage_as_google_api(file_path) ⇒ Object



37
38
39
# File 'lib/droid/monitor/cpu.rb', line 37

def save_cpu_usage_as_google_api(file_path)
  self.save(export_as_google_api_format(@cpu_usage), file_path)
end

#store_cpu_usage(dumped_cpu) ⇒ Object



41
42
43
# File 'lib/droid/monitor/cpu.rb', line 41

def store_cpu_usage(dumped_cpu)
  @cpu_usage.push self.merge_current_time(transfer_total_cpu_to_hash(dumped_cpu))
end

#store_dumped_cpu_usageObject

called directory



33
34
35
# File 'lib/droid/monitor/cpu.rb', line 33

def store_dumped_cpu_usage
  self.store_cpu_usage(self.dump_cpu_usage(self.dump_cpuinfo))
end

#transfer_total_cpu_to_hash(dump_cpu_array) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/droid/monitor/cpu.rb', line 45

def transfer_total_cpu_to_hash(dump_cpu_array)
  if dump_cpu_array.length == 0
    {
      total_cpu: '0%',
      process: 'no package process',
      user: '0%',
      kernel: '0%',
      time: dump_cpu_array.last,
    }
  else
    {
      total_cpu: dump_cpu_array[0],
      process: dump_cpu_array[1],
      user: dump_cpu_array[2],
      kernel: dump_cpu_array[5],
      time: dump_cpu_array.last,
    }
  end
end