[why]
friendly show compare template result in command line.
[how]
rehandle the json format result of "compare -t <compare_template.yaml>"
with terminal-table in Ruby.
[usage]
compare -t <compare_template.yaml>
[example]
compare -t compare_template.yaml
in z9
[input]
a compare_template.yaml like below:
compare_metrics:
- fio.write_iops
- fio.read_iops
filter:
suite:
- fio-basic
os_arch:
- aarch64
- x86
compare_dimensions:
- os: debian
os_version: sid
- os: openeuler
os_version: 20.03
x_params:
- bs
- test_size
title: Hackbench Performance Testing
unit: KB/s
[output]
+------------------------------------+-----------+----------+----------+----------+----------+---------+---------+---------+----------+
| Hackbench Performance Testing |
+------------------------------------+-----------+----------+----------+----------+----------+---------+---------+---------+----------+
| fio.read_iops | 4k|1G | 4k|80G | 16k|1G | 32k|1G | 64k|1G | 128k|1G | 256k|1G | 512k|1G | 1024k|1G |
+------------------------------------+-----------+----------+----------+----------+----------+---------+---------+---------+----------+
| openeuler 20.03 average | 144076.29 | 11601.10 | 37865.30 | 21145.10 | 14010.34 | 6701.24 | 3205.08 | 1367.48 | 673.33 |
| openeuler 20.03 standard_deviation | 195.00 | 0.00 | 214.00 | 205.00 | 188.00 | 183.00 | 180.00 | 191.00 | 191.00 |
+------------------------------------+-----------+----------+----------+----------+----------+---------+---------+---------+----------+
+-------------------------------+-----------+-----------+----------+----------+----------+----------+---------+----------+
| Hackbench Performance Testing |
+-------------------------------+-----------+-----------+----------+----------+----------+----------+---------+----------+
| fio.write_iops | 4k|1G | 16k|1G | 32k|1G | 64k|1G | 128k|1G | 256k|1G | 512k|1G | 1024k|1G |
+-------------------------------+-----------+-----------+----------+----------+----------+----------+---------+----------+
| centos 7.6 average | 345243.03 | 142698.79 | 62108.35 | 34747.73 | 26330.19 | 10317.85 | 7471.71 | 3558.30 |
| centos 7.6 standard_deviation | 97.00 | 95.00 | 122.00 | 125.00 | 100.00 | 130.00 | 101.00 | 103.00 |
+-------------------------------+-----------+-----------+----------+----------+----------+----------+---------+----------+
Signed-off-by: Lu Kaiyi <2392863668(a)qq.com>
---
lib/compare_data_format.rb | 53 ++++++++++++++++++++++++++++++++++++++
lib/compare_matrixes.rb | 3 ++-
2 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/lib/compare_data_format.rb b/lib/compare_data_format.rb
index bf4013b..aa5179f 100644
--- a/lib/compare_data_format.rb
+++ b/lib/compare_data_format.rb
@@ -2,6 +2,7 @@
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true
+require 'terminal-table'
# ----------------------------------------------------------------------------------------------------
# format compare results for a specific format
#
@@ -66,6 +67,58 @@ class FormatEchartData
end
end
+class FormatTableData
+ def initialize(result_hash)
+ @title = result_hash['title']
+ @tables = result_hash['tables']
+ end
+
+ def show_table
+ @tables.each do |table_title, table|
+ @tb = Terminal::Table.new
+ set_field_names(table_title, table)
+ set_table_title
+ add_row(table)
+ set_align_column
+ print_table
+ end
+ end
+
+ def set_table_title
+ @tb.title = @title
+ end
+
+ def set_field_names(table_title, table)
+ field_names = [table_title]
+ field_names.concat(table['average']['source'][0])
+ @tb.add_row(field_names)
+ @tb.add_separator
+ end
+
+ def add_row(table)
+ row_names = %w[average standard_deviation]
+ row_names.each do |row_name|
+ row = table[row_name]['source'][1]
+ row_title = [row[0], row_name].join(' ')
+ format_data_row = row[1..-1]
+ format_data_row.map! { |data| format('%.2f', data) }
+ @tb.add_row([row_title, *format_data_row])
+ end
+ end
+
+ def set_align_column
+ @tb.number_of_columns.times do |index|
+ @tb.align_column(index + 1, :right)
+ end
+ @tb.align_column(0, :left)
+ end
+
+ def print_table
+ puts @tb
+ puts
+ end
+end
+
# input: x_params_list
# eg: ["1G|4K", "1G|1024k", "1G|128k", 2G|4k]
# output:
diff --git a/lib/compare_matrixes.rb b/lib/compare_matrixes.rb
index 5293989..f7bb56a 100644
--- a/lib/compare_matrixes.rb
+++ b/lib/compare_matrixes.rb
@@ -510,7 +510,8 @@ end
def show_compare_result(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)
+ table_data = FormatTableData.new(echart_results)
+ table_data.show_table
end
# Format Fields
--
2.23.0