1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2012 The ChromiumOS Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Tests for the tabulator module.""" 8 9 10__author__ = '[email protected] (Ahmad Sharif)' 11 12# System modules 13import unittest 14 15# Local modules 16from cros_utils import tabulator 17 18 19class TabulatorTest(unittest.TestCase): 20 """Tests for the Tabulator class.""" 21 22 def testResult(self): 23 table = ['k1', ['1', '3'], ['55']] 24 result = tabulator.Result() 25 cell = tabulator.Cell() 26 result.Compute(cell, table[2], table[1]) 27 expected = ' '.join([str(float(v)) for v in table[2]]) 28 self.assertTrue(cell.value == expected) 29 30 result = tabulator.AmeanResult() 31 cell = tabulator.Cell() 32 result.Compute(cell, table[2], table[1]) 33 self.assertTrue(cell.value == float(table[2][0])) 34 35 def testStdResult(self): 36 table = ['k1', [], ['1', '2']] 37 result = tabulator.StdResult() 38 cell = tabulator.Cell() 39 result.Compute(cell, table[2], table[1]) 40 self.assertTrue(cell.value == 0.5) 41 42 def testStdResultOfSampleSize1(self): 43 table = ['k1', [], ['1']] 44 result = tabulator.StdResult() 45 cell = tabulator.Cell() 46 result.Compute(cell, table[2], table[1]) 47 self.assertTrue(cell.value == 0.0) 48 49 def testStringMean(self): 50 smr = tabulator.StringMeanResult() 51 cell = tabulator.Cell() 52 value = 'PASS' 53 values = [value for _ in range(3)] 54 smr.Compute(cell, values, None) 55 self.assertTrue(cell.value == value) 56 values.append('FAIL') 57 smr.Compute(cell, values, None) 58 self.assertTrue(cell.value == '?') 59 60 def testStorageFormat(self): 61 sf = tabulator.StorageFormat() 62 cell = tabulator.Cell() 63 base = 1024.0 64 cell.value = base 65 sf.Compute(cell) 66 self.assertTrue(cell.string_value == '1.0K') 67 cell.value = base**2 68 sf.Compute(cell) 69 self.assertTrue(cell.string_value == '1.0M') 70 cell.value = base**3 71 sf.Compute(cell) 72 self.assertTrue(cell.string_value == '1.0G') 73 74 def testLerp(self): 75 c1 = tabulator.Color(0, 0, 0, 0) 76 c2 = tabulator.Color(255, 0, 0, 0) 77 c3 = tabulator.Color.Lerp(0.5, c1, c2) 78 self.assertTrue(c3.r == 127.5) 79 self.assertTrue(c3.g == 0) 80 self.assertTrue(c3.b == 0) 81 self.assertTrue(c3.a == 0) 82 c3.Round() 83 self.assertTrue(c3.r == 127) 84 85 def testGmean(self): 86 a = [1.0e+308] * 3 87 # pylint: disable=protected-access 88 b = tabulator.Result()._GetGmean(a) 89 self.assertTrue(0.99e+308 <= b <= 1.01e+308) 90 91 def testIgnoreMinMax(self): 92 amr = tabulator.AmeanResult(ignore_min_max=True) 93 cell = tabulator.Cell() 94 values = [1, 2] 95 amr.Compute(cell, values, None) 96 self.assertTrue(cell.value == 1.5) 97 values = [1, 2, 8] 98 amr.Compute(cell, values, None) 99 self.assertTrue(cell.value == 2) 100 101 def testTableGenerator(self): 102 # yapf: disable 103 runs = [[{'k1': '10', 'k2': '12'}, 104 {'k1': '13', 'k2': '14', 'k3': '15'}], 105 [{'k1': '50', 'k2': '51', 'k3': '52', 'k4': '53'}]] 106 # yapf: enable 107 labels = ['vanilla', 'modified'] 108 tg = tabulator.TableGenerator(runs, labels) 109 table = tg.GetTable() 110 header = table.pop(0) 111 112 self.assertTrue(header == ['keys', 'vanilla', 'modified']) 113 row = table.pop(0) 114 self.assertTrue(row == ['k1', ['10', '13'], ['50']]) 115 row = table.pop(0) 116 self.assertTrue(row == ['k2', ['12', '14'], ['51']]) 117 row = table.pop(0) 118 self.assertTrue(row == ['k3', [None, '15'], ['52']]) 119 row = table.pop(0) 120 self.assertTrue(row == ['k4', [None, None], ['53']]) 121 122 table = tg.GetTable() 123 columns = [ 124 tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), 125 tabulator.Column(tabulator.AmeanRatioResult(), 126 tabulator.PercentFormat()), 127 ] 128 tf = tabulator.TableFormatter(table, columns) 129 table = tf.GetCellTable() 130 self.assertTrue(table) 131 132 def testSamplesTableGenerator(self): 133 # yapf: disable 134 keyvals = { 135 'bench1': [[{'samples': 1}, {'samples': 2}], 136 [{'samples': 3}, {'samples': 4}]], 137 'bench2': [[{'samples': 5}, {}], 138 [{'samples': 6}, {'samples': 7}]] 139 } 140 # yapf: enable 141 weights = {'bench1': 0.2, 'bench2': 0.7} 142 iter_counts = {'bench1': 2, 'bench2': 2} 143 labels = ['vanilla', 'modified'] 144 tg = tabulator.SamplesTableGenerator(keyvals, labels, iter_counts, weights) 145 (table, new_keyvals, new_iter_counts) = tg.GetTable() 146 147 columns = [ 148 tabulator.Column(tabulator.IterationResult(), tabulator.Format()), 149 tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), 150 tabulator.Column(tabulator.AmeanRatioResult(), 151 tabulator.PercentFormat()), 152 ] 153 # This is the function to load column info. 154 tf = tabulator.TableFormatter(table, columns, samples_table=True) 155 # This is the function where to do all weighting calculation. 156 cell_table = tf.GetCellTable('summary') 157 self.assertTrue(cell_table) 158 159 header = table.pop(0) 160 self.assertTrue(header == ['Benchmarks', 'Weights', 'vanilla', 'modified']) 161 row = table.pop(0) 162 # yapf: disable 163 self.assertTrue(row == ['bench1', 0.2, 164 ((2, 0), [1 * 0.2, 2 * 0.2]), 165 ((2, 0), [3 * 0.2, 4 * 0.2])]) 166 row = table.pop(0) 167 self.assertTrue(row == ['bench2', 0.7, 168 ((1, 1), [5 * 0.7, None]), 169 ((2, 0), [6 * 0.7, 7 * 0.7])]) 170 row = table.pop(0) 171 self.assertTrue(row == ['Composite Benchmark (samples)', 'N/A', 172 ((1, 1), [1 * 0.2 + 5 * 0.7, None]), 173 ((2, 0), [3 * 0.2 + 6 * 0.7, 4 * 0.2 + 7 * 0.7])]) 174 # yapf: enable 175 self.assertTrue('Composite Benchmark' in new_keyvals.keys()) 176 self.assertTrue('Composite Benchmark' in new_iter_counts.keys()) 177 178 def testColspan(self): 179 simple_table = [ 180 ['binary', 'b1', 'b2', 'b3'], 181 ['size', 100, 105, 108], 182 ['rodata', 100, 80, 70], 183 ['data', 100, 100, 100], 184 ['debug', 100, 140, 60], 185 ] 186 columns = [ 187 tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), 188 tabulator.Column(tabulator.MinResult(), tabulator.Format()), 189 tabulator.Column(tabulator.AmeanRatioResult(), 190 tabulator.PercentFormat()), 191 tabulator.Column(tabulator.AmeanRatioResult(), 192 tabulator.ColorBoxFormat()), 193 ] 194 our_table = [simple_table[0]] 195 for row in simple_table[1:]: 196 our_row = [row[0]] 197 for v in row[1:]: 198 our_row.append([v]) 199 our_table.append(our_row) 200 201 tf = tabulator.TableFormatter(our_table, columns) 202 cell_table = tf.GetCellTable() 203 self.assertTrue(cell_table[0][0].colspan == 1) 204 self.assertTrue(cell_table[0][1].colspan == 2) 205 self.assertTrue(cell_table[0][2].colspan == 4) 206 self.assertTrue(cell_table[0][3].colspan == 4) 207 for row in cell_table[1:]: 208 for cell in row: 209 self.assertTrue(cell.colspan == 1) 210 211 212if __name__ == '__main__': 213 unittest.main() 214