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_cpuinfo_with_top, #dump_gfxinfo, #dump_meminfo, #dump_package_info, #dump_tcp_rec, #dump_tcp_snd

Constructor Details

#initialize(opts = {}) ⇒ Cpu

Returns a new instance of Cpu.



16
17
18
19
# File 'lib/droid/monitor/cpu.rb', line 16

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

Instance Attribute Details

#cpu_usageObject (readonly)

Returns the value of attribute cpu_usage.



12
13
14
# File 'lib/droid/monitor/cpu.rb', line 12

def cpu_usage
  @cpu_usage
end

Instance Method Details

#clear_cpu_usageObject



21
22
23
# File 'lib/droid/monitor/cpu.rb', line 21

def clear_cpu_usage
  @cpu_usage = []
end

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



148
149
150
151
# File 'lib/droid/monitor/cpu.rb', line 148

def create_graph(data_file_path, graph_opts = {}, output_file_path) # rubocop:disable Style/OptionalArguments
  save(Droid::Monitor::GoogleApiTemplate.create_graph(data_file_path, graph_opts),
       output_file_path)
end

#dump_cpu_usage(dump_data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/droid/monitor/cpu.rb', line 25

def dump_cpu_usage(dump_data)
  scanned_dump = dump_data.scan(/^.*#{package}.*$/).map(&:strip).first
  return [] if scanned_dump.nil?

  dump = scanned_dump.split(/\s/).reject(&:empty?)
  raise 'no package' if /^Load:$/ =~ dump[0]

  dump
rescue StandardError => e
  puts e
  []
end

#dump_cpu_usage_with_top(dump_data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/droid/monitor/cpu.rb', line 38

def dump_cpu_usage_with_top(dump_data)
  return [] if dump_data.nil?

  dump = dump_data.split(/\s/).reject(&:empty?)
  raise 'no package' if /^Load:$/ =~ dump[0]

  dump
rescue StandardError => e
  puts e
  []
end

#export_as_google_api_format(from_cpu_usage) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/droid/monitor/cpu.rb', line 111

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

#export_as_google_api_format_with_top(from_cpu_usage) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/droid/monitor/cpu.rb', line 129

def export_as_google_api_format_with_top(from_cpu_usage)
  google_api_data_format = empty_google_api_format_with_top

  from_cpu_usage.each do |hash|
    a_google_api_data_format = {
      c: [
        { v: hash[:time] },
        { v: hash[:total_cpu].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



59
60
61
# File 'lib/droid/monitor/cpu.rb', line 59

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

#save_cpu_usage_as_google_api_with_top(file_path) ⇒ Object



63
64
65
# File 'lib/droid/monitor/cpu.rb', line 63

def save_cpu_usage_as_google_api_with_top(file_path)
  save(export_as_google_api_format_with_top(@cpu_usage), file_path)
end

#store_cpu_usage(dumped_cpu) ⇒ Object



67
68
69
# File 'lib/droid/monitor/cpu.rb', line 67

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

#store_cpu_usage_with_top(dumped_cpu) ⇒ Object



71
72
73
# File 'lib/droid/monitor/cpu.rb', line 71

def store_cpu_usage_with_top(dumped_cpu)
  @cpu_usage.push merge_current_time(transfer_total_cpu_with_top_to_hash(dumped_cpu))
end

#store_dumped_cpu_usageObject

called directory



51
52
53
# File 'lib/droid/monitor/cpu.rb', line 51

def store_dumped_cpu_usage
  store_cpu_usage(dump_cpu_usage(dump_cpuinfo))
end

#store_dumped_cpu_usage_with_topObject



55
56
57
# File 'lib/droid/monitor/cpu.rb', line 55

def store_dumped_cpu_usage_with_top
  store_cpu_usage_with_top(dump_cpu_usage_with_top(dump_cpuinfo_with_top))
end

#transfer_total_cpu_to_hash(dump_cpu_array) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/droid/monitor/cpu.rb', line 75

def transfer_total_cpu_to_hash(dump_cpu_array)
  if dump_cpu_array.length <= 1
    {
      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

#transfer_total_cpu_with_top_to_hash(dump_cpu_array) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/droid/monitor/cpu.rb', line 95

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