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.



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



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

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
31
32
33
# File 'lib/droid/monitor/cpu.rb', line 23

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

  dump = scanned_dump.split(/\s/).reject(&:empty?)
  fail 'no package' if /^Load:$/ =~ dump[0]
  dump
rescue StandardError => e
  puts e
  []
end

#dump_cpu_usage_with_top(dump_data) ⇒ Object



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

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

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

  puts dump

  dump
rescue StandardError => e
  puts e
  []
end

#export_as_google_api_format(from_cpu_usage) ⇒ Object



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

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
144
# 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



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

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

#save_cpu_usage_as_google_api_with_top(file_path) ⇒ Object



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

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

#store_cpu_usage(dumped_cpu) ⇒ Object



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

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

#store_cpu_usage_with_top(dumped_cpu) ⇒ Object



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

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

#store_dumped_cpu_usageObject

called directory



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

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

#store_dumped_cpu_usage_with_topObject



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

def store_dumped_cpu_usage_with_top
  self.store_cpu_usage_with_top(self.dump_cpu_usage_with_top(self.dump_cpuinfo_with_top))
end

#transfer_total_cpu_to_hash(dump_cpu_array) ⇒ Object



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

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



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

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