blob: 31bf83d3f89f8765dd0aeff99a78d1ab4880e7f4 [file] [log] [blame]
Evelina Dumitrescuc7faa092016-09-28 15:13:29 -07001#!/usr/bin/python2
2
3# Copyright 2016 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""Unit tests for the utility module."""
7
8import collections
9import csv
10import unittest
11
12import utils
13
14
15class UtilsTest(unittest.TestCase):
16 """Test class for utility module."""
17
18 def __init__(self, *args, **kwargs):
19 super(UtilsTest, self).__init__(*args, **kwargs)
20 self._pprof_top_csv_file = 'testdata/input/pprof_top_csv/file1.csv'
21 self._pprof_top_file = 'testdata/input/pprof_top/file1.pprof'
22 self._pprof_tree_csv_file = 'testdata/input/pprof_tree_csv/file1.csv'
23 self._pprof_tree_file = 'testdata/input/pprof_tree/file1.pprof'
24 self._pairwise_inclusive_count_test_file = \
25 'testdata/input/pairwise_inclusive_count_test.csv'
26 self._pairwise_inclusive_count_reference_file = \
27 'testdata/input/pairwise_inclusive_count_reference.csv'
28 self._inclusive_count_test_file = \
29 'testdata/input/inclusive_count_test.csv'
30 self._inclusive_count_reference_file = \
31 'testdata/input/inclusive_count_reference.csv'
32
33 def testParseFunctionGroups(self):
34 cwp_function_groups_lines = \
35 ['group1 /a\n', 'group2 /b\n', 'group3 /c\n', 'group4 /d\n']
36 expected_output = [('group1', '/a'), ('group2', '/b'), ('group3', '/c'),
37 ('group4', '/d')]
38 result = utils.ParseFunctionGroups(cwp_function_groups_lines)
39
40 self.assertListEqual(expected_output, result)
41
42 def testParsePProfTopOutput(self):
43 result_pprof_top_output = utils.ParsePprofTopOutput(self._pprof_top_file)
44 expected_pprof_top_output = {}
45
46 with open(self._pprof_top_csv_file) as input_file:
47 statistics_reader = csv.DictReader(input_file, delimiter=',')
48
49 for statistic in statistics_reader:
50 if statistic['file']:
51 function_key = ','.join([statistic['function'], statistic['file']])
52 else:
53 function_key = statistic['function']
54 expected_pprof_top_output[function_key] = \
55 (statistic['flat'], statistic['flat_p'], statistic['sum_p'],
56 statistic['cum'], statistic['cum_p'])
57
58 self.assertDictEqual(result_pprof_top_output, expected_pprof_top_output)
59
60 def testParsePProfTreeOutput(self):
61 result_pprof_tree_output = utils.ParsePprofTreeOutput(self._pprof_tree_file)
62 expected_pprof_tree_output = collections.defaultdict(dict)
63
64 with open(self._pprof_tree_csv_file) as input_file:
65 statistics_reader = csv.DictReader(input_file, delimiter=',')
66
67 for statistic in statistics_reader:
68 parent_function_key = \
69 ','.join([statistic['parent_function'],
70 statistic['parent_function_file']])
71 child_function_key = \
72 ','.join([statistic['child_function'],
73 statistic['child_function_file']])
74
75 expected_pprof_tree_output[parent_function_key][child_function_key] = \
76 float(statistic['inclusive_count_fraction'])
77
78 self.assertDictEqual(result_pprof_tree_output, expected_pprof_tree_output)
79
80 def testParseCWPInclusiveCountFile(self):
81 expected_inclusive_statistics_test = \
82 {'func_i,/c/d/file_i': ('i', 5, 4.4, utils.EXTRA_FUNCTION),
83 'func_j,/e/file_j': ('j', 6, 5.5, utils.EXTRA_FUNCTION),
84 'func_f,/a/b/file_f': ('f', 4, 2.3, utils.EXTRA_FUNCTION),
85 'func_h,/c/d/file_h': ('h', 1, 3.3, utils.EXTRA_FUNCTION),
86 'func_k,/e/file_k': ('k', 7, 6.6, utils.EXTRA_FUNCTION),
87 'func_g,/a/b/file_g': ('g', 2, 2.2, utils.EXTRA_FUNCTION)}
88 expected_inclusive_statistics_reference = \
89 {'func_i,/c/d/file_i': ('i', 5, 4.0, utils.EXTRA_FUNCTION),
90 'func_j,/e/file_j': ('j', 6, 5.0, utils.EXTRA_FUNCTION),
91 'func_f,/a/b/file_f': ('f', 1, 1.0, utils.EXTRA_FUNCTION),
92 'func_l,/e/file_l': ('l', 7, 6.0, utils.EXTRA_FUNCTION),
93 'func_h,/c/d/file_h': ('h', 4, 3.0, utils.EXTRA_FUNCTION),
94 'func_g,/a/b/file_g': ('g', 5, 4.4, utils.EXTRA_FUNCTION)}
95 result_inclusive_statistics_test = \
96 utils.ParseCWPInclusiveCountFile(self._inclusive_count_test_file)
97 result_inclusive_statistics_reference = \
98 utils.ParseCWPInclusiveCountFile(self._inclusive_count_reference_file)
99
100 self.assertDictEqual(result_inclusive_statistics_test,
101 expected_inclusive_statistics_test)
102 self.assertDictEqual(result_inclusive_statistics_reference,
103 expected_inclusive_statistics_reference)
104
105 def testParseCWPPairwiseInclusiveCountFile(self):
106 expected_pairwise_inclusive_statistics_test = {
107 'func_f': {'func_g,/a/b/file_g2': 0.01,
108 'func_h,/c/d/file_h': 0.02,
109 'func_i,/c/d/file_i': 0.03},
110 'func_g': {'func_j,/e/file_j': 0.4,
111 'func_m,/e/file_m': 0.6}
112 }
113 expected_pairwise_inclusive_statistics_reference = {
114 'func_f': {'func_g,/a/b/file_g': 0.1,
115 'func_h,/c/d/file_h': 0.2,
116 'func_i,/c/d/file_i': 0.3},
117 'func_g': {'func_j,/e/file_j': 0.4}
118 }
119 result_pairwise_inclusive_statistics_test = \
120 utils.ParseCWPPairwiseInclusiveCountFile(
121 self._pairwise_inclusive_count_test_file)
122 result_pairwise_inclusive_statistics_reference = \
123 utils.ParseCWPPairwiseInclusiveCountFile(
124 self._pairwise_inclusive_count_reference_file)
125
126 self.assertDictEqual(result_pairwise_inclusive_statistics_test,
127 expected_pairwise_inclusive_statistics_test)
128 self.assertDictEqual(result_pairwise_inclusive_statistics_reference,
129 expected_pairwise_inclusive_statistics_reference)
130
131
132if __name__ == '__main__':
133 unittest.main()