[Why]
With template compare mode, the result(data_set) isn't friendly for echart
[How]
Make the result more friendly for echart like:
{
"title": "Hackbench Performance Testing",
"unit": "KB/s",
"x_name": "test_size|bs",
"tables": {
"fio.read_iops": {
"average": {
"dimensions": [
"compare_dimension",
"openeuler 20.03"
],
"source": [
["1G|4k", "1G|16k", "1G|32k", "1G|64k", "1G|128k", "1G|256k", "1G|512k", "1G|1024k", "80G|4k"],
["openeuler 20.03", 144076.2903315, 37865.30472368628, 21145.10375497826, 14010.34254665909,
6701.240849466667, 3205.077255, 1367.476930860465, 673.3270888666667, 11601.099817
]
]
},
"standard_deviation": {...},
"change": {...}
},
"fio.write_iops": {...}
...
}
}
Signed-off-by: Lu Weitao <luweitaobe(a)163.com>
---
lib/compare_data_format.rb | 77 ++++++++++++++++++++++++++++++++++----
lib/compare_matrixes.rb | 3 +-
2 files changed, 71 insertions(+), 9 deletions(-)
diff --git a/lib/compare_data_format.rb b/lib/compare_data_format.rb
index 3d82550..b4f26ee 100644
--- a/lib/compare_data_format.rb
+++ b/lib/compare_data_format.rb
@@ -5,14 +5,75 @@
# ----------------------------------------------------------------------------------------------------
# format compare results for a specific format
#
+class FormatEchartData
+ def initialize(metrics_compare_results, template_params)
+ @metrics_compare_results = metrics_compare_results
+ @template_params = template_params
+ @data_set = {}
+ end
-def format_for_echart(metrics_compare_results, template_params)
- echart_result = {}
- echart_result['title'] = template_params['title']
- echart_result['unit'] = template_params['unit']
- x_params = template_params['x_params']
- echart_result['x_name'] = x_params.join('|') if x_params
- echart_result['tables'] = metrics_compare_results
+ def format_for_echart
+ echart_result = {}
+ echart_result['title'] = @template_params['title']
+ echart_result['unit'] = @template_params['unit']
+ @x_name = @template_params['x_params']
+ echart_result['x_name'] = @x_name.join('|') if @x_name
+ echart_result['tables'] = convert_to_echart_dataset
- echart_result
+ echart_result
+ end
+
+ def convert_to_echart_dataset
+ @x_params = sort_x_params((a)metrics_compare_results.keys)
+ @metrics_compare_results.each_value do |metrics_values|
+ metrics_values.each do |metric, metric_value|
+ assign_echart_data_set(metric, metric_value)
+ end
+ end
+
+ @data_set
+ end
+
+ def assign_echart_data_set(metric, metric_value)
+ @data_set[metric] = {}
+ metric_value.each do |value_type, values| # value_type can be: average, standard_deviation, change
+ @data_set[metric][value_type] = {
+ 'dimensions' => ['compare_dimension']
+ }
+
+ dimension_list = values.keys.sort
+ @data_set[metric][value_type]['dimensions'] += dimension_list
+ @data_set[metric][value_type]['source'] = assign_echart_source(metric, value_type, dimension_list)
+ end
+ end
+
+ def assign_echart_source(metric, value_type, dimensions)
+ source = []
+ source << @x_params
+ dimensions.each do |dimension|
+ dimension_values = [dimension]
+ @x_params.each do |x_param|
+ if @metrics_compare_results[x_param][metric]
+ dimension_values << @metrics_compare_results[x_param][metric][value_type][dimension]
+ end
+ end
+ source << dimension_values
+ end
+
+ source
+ end
+end
+
+# input: x_params_list
+# eg: ["1G|4K", "1G|1024k", "1G|128k", 2G|4k]
+# output:
+# ["1G|4K", "1G|128k", "1G|1024k", "2G|4k"]
+def sort_x_params(x_params_list)
+ x_params_hash = {}
+ x_params_list.each do |param|
+ params = param.gsub(/[a-zA-Z]+$/, '0').split('|').map(&:to_i)
+ x_params_hash[params] = param
+ end
+
+ x_params_hash.sort.map { |h| h[1] }
end
diff --git a/lib/compare_matrixes.rb b/lib/compare_matrixes.rb
index 119d42d..762c087 100644
--- a/lib/compare_matrixes.rb
+++ b/lib/compare_matrixes.rb
@@ -498,7 +498,8 @@ def get_dimentions_combination(dimension_list)
end
def show_compare_result(metrics_compare_results, template_params)
- echart_results = format_for_echart(metrics_compare_results, template_params)
+ formatter = FormatEchartData.new(metrics_compare_results, template_params)
+ echart_results = formatter.format_for_echart
print JSON.pretty_generate(echart_results)
end
--
2.23.0