[Why]
old logical:
lkp-tests/stats/script < script_log > tmpfile
lkp-tests/sbin/dump-stat < tmpfile > $script.json
this flow will escape special characters
[How]
use return of function to keep original characters
lkp-tests/stats/script.rb
func(read(script_log)) --> hash
compass-ci/lib/dump-stats.rb
hash --> dump_stats() --> $script.json
[Dependence]
lkp-tests/stats/$script.rb, for example:
file_name: lkp-tests/stats/proc-vmstats.rb
function name: proc_vmstats()
input: Array(String)
["time: 231231234", "nr_free_page: 89721", ...]
output: Hash(String, Array(Number|String) | Number | String)
{
"time" => [231231234, 231231235],
"nr_free_page" => [89721,88564],
...
}
or
{
"time" => 231231234,
"nr_free_page" => 89721,
...
}
Signed-off-by: Lu Weitao <luweitaobe(a)163.com>
---
lib/stats.rb | 19 ++++++++++++++-----
lib/stats_wrapper.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 5 deletions(-)
diff --git a/lib/stats.rb b/lib/stats.rb
index c01d38d..94e370a 100644
--- a/lib/stats.rb
+++ b/lib/stats.rb
@@ -14,11 +14,16 @@ end
def available_stats
vailable_stats = []
+ available_stats_func = []
Dir.open(LKP_SRC + '/stats').each do |file|
- vailable_stats << file unless file.start_with?('.')
+ if file.end_with?('.rb')
+ available_stats_func << file.chomp('.rb')
+ else
+ vailable_stats << file unless file.start_with?('.')
+ end
end
- vailable_stats
+ [vailable_stats, available_stats_func]
end
def assign_default_stats(stats_list)
@@ -32,13 +37,17 @@ class Stats
def initialize(result_root)
@result_root = result_root
@job = load_yaml(File.join(result_root, 'job.yaml'))
- @available_stats = available_stats
+ @available_stats, @available_stats_func = available_stats
end
def extract_stats
stats_list = assign_stats_list
stats_list.each do |stat|
- StatsWrapper.wrapper(*stat)
+ if @available_stats_func.include?(stat)
+ StatsWrapper.wrapper_func(*stat)
+ else
+ StatsWrapper.wrapper(*stat)
+ end
end
end
@@ -48,7 +57,7 @@ class Stats
stats_list << ['time', @job['suite'] + '.time']
stats_list << 'stderr'
@job.each_key do |k|
- if @available_stats.include?(k)
+ if @available_stats.include?(k) || @available_stats_func.include?(k)
stats_list << k
end
end
diff --git a/lib/stats_wrapper.rb b/lib/stats_wrapper.rb
index 8a6d6a9..faa81df 100644
--- a/lib/stats_wrapper.rb
+++ b/lib/stats_wrapper.rb
@@ -6,6 +6,11 @@ LKP_SRC ||= ENV['LKP_SRC'] || '/c/lkp-tests'
require "#{LKP_SRC}/lib/log.rb"
require 'tempfile'
require 'English'
+require_relative './dump_stat'
+
+Dir[File.expand_path("#{LKP_SRC}/stats/*.rb")].uniq.each do |file|
+ require file
+end
PROGRAM_DIR = "#{LKP_SRC}/stats"
@@ -35,6 +40,27 @@ module StatsWrapper
delete_log_package
end
+ def self.wrapper_func(program, program_time = nil)
+ @program = program
+ @stats_group = program_time || program
+ @log = "#{RESULT_ROOT}/#{@stats_group}"
+
+ return unless File.exist?("#{(a)log}.yaml") || pretreatment
+
+ if File.exist?("#{(a)log}.yaml")
+ log_lines = read_log("#{(a)log}.yaml")
+ stat_result = parse_simple_log_yaml(log_lines)
+ else
+ log_lines = read_log(@log)
+ call_func_cmd = "#{(a)program.gsub('-', '_')}(log_lines)" # eg: proc_vmstats(log_lines)
+ stat_result = eval(call_func_cmd)
+ end
+ return unless DumpStat.dump_stat(@stats_group, stat_result)
+
+ check_empty_json
+ delete_log_package
+ end
+
def self.pretreatment
return unless available_program?
return unless unzip_log
@@ -261,3 +287,21 @@ module StatsWrapper
File.delete(@tmpfile) if File.exist?(@tmpfile)
end
end
+
+# read line of log
+# return Array(String)
+def read_log(log_path)
+ return nil unless File.exist?(log_path)
+
+ File.readlines(log_path)
+end
+
+def parse_simple_log_yaml(log_lines)
+ result = Hash.new { |hash, key| hash[key] = [] }
+ log_lines.each do |line|
+ key, value = line.split(/:?\s+/)
+ result[key] << value
+ end
+
+ result
+end
--
2.23.0