summaryrefslogtreecommitdiffstats
path: root/scripts/pybootchartgui/pybootchartgui/tests
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/pybootchartgui/pybootchartgui/tests')
-rw-r--r--scripts/pybootchartgui/pybootchartgui/tests/parser_test.py93
-rw-r--r--scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py78
2 files changed, 171 insertions, 0 deletions
diff --git a/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py b/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py
new file mode 100644
index 0000000000..574c2c7a2b
--- /dev/null
+++ b/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py
@@ -0,0 +1,93 @@
1import sys, os, re, struct, operator, math
2from collections import defaultdict
3import unittest
4
5sys.path.insert(0, os.getcwd())
6
7import parsing
8
9debug = False
10
11def floatEq(f1, f2):
12 return math.fabs(f1-f2) < 0.00001
13
14class TestBCParser(unittest.TestCase):
15
16 def setUp(self):
17 self.name = "My first unittest"
18 self.rootdir = '../examples/1'
19
20 def mk_fname(self,f):
21 return os.path.join(self.rootdir, f)
22
23 def testParseHeader(self):
24 state = parsing.parse_file(parsing.ParserState(), self.mk_fname('header'))
25 self.assertEqual(6, len(state.headers))
26 self.assertEqual(2, parsing.get_num_cpus(state.headers))
27
28 def test_parseTimedBlocks(self):
29 state = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_diskstats.log'))
30 self.assertEqual(141, len(state.disk_stats))
31
32 def testParseProcPsLog(self):
33 state = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_ps.log'))
34 samples = state.ps_stats
35 processes = samples.process_list
36 sorted_processes = sorted(processes, key=lambda p: p.pid )
37
38 for index, line in enumerate(open(self.mk_fname('extract2.proc_ps.log'))):
39 tokens = line.split();
40 process = sorted_processes[index]
41 if debug:
42 print tokens[0:4]
43 print process.pid, process.cmd, process.ppid, len(process.samples)
44 print '-------------------'
45
46 self.assertEqual(tokens[0], str(process.pid))
47 self.assertEqual(tokens[1], str(process.cmd))
48 self.assertEqual(tokens[2], str(process.ppid))
49 self.assertEqual(tokens[3], str(len(process.samples)))
50
51
52 def testparseProcDiskStatLog(self):
53 state_with_headers = parsing.parse_file(parsing.ParserState(), self.mk_fname('header'))
54 state_with_headers.headers['system.cpu'] = 'xxx (2)'
55 samples = parsing.parse_file(state_with_headers, self.mk_fname('proc_diskstats.log')).disk_stats
56 self.assertEqual(141, len(samples))
57
58 for index, line in enumerate(open(self.mk_fname('extract.proc_diskstats.log'))):
59 tokens = line.split('\t')
60 sample = samples[index]
61 if debug:
62 print line.rstrip(),
63 print sample
64 print '-------------------'
65
66 self.assertEqual(tokens[0], str(sample.time))
67 self.assert_(floatEq(float(tokens[1]), sample.read))
68 self.assert_(floatEq(float(tokens[2]), sample.write))
69 self.assert_(floatEq(float(tokens[3]), sample.util))
70
71 def testparseProcStatLog(self):
72 samples = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_stat.log')).cpu_stats
73 self.assertEqual(141, len(samples))
74
75 for index, line in enumerate(open(self.mk_fname('extract.proc_stat.log'))):
76 tokens = line.split('\t')
77 sample = samples[index]
78 if debug:
79 print line.rstrip()
80 print sample
81 print '-------------------'
82 self.assert_(floatEq(float(tokens[0]), sample.time))
83 self.assert_(floatEq(float(tokens[1]), sample.user))
84 self.assert_(floatEq(float(tokens[2]), sample.sys))
85 self.assert_(floatEq(float(tokens[3]), sample.io))
86
87 def testParseLogDir(self):
88 res = parsing.parse([self.rootdir], False)
89 self.assertEqual(4, len(res))
90
91if __name__ == '__main__':
92 unittest.main()
93
diff --git a/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py b/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py
new file mode 100644
index 0000000000..971e125eab
--- /dev/null
+++ b/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py
@@ -0,0 +1,78 @@
1import sys
2import os
3import unittest
4
5sys.path.insert(0, os.getcwd())
6
7import parsing
8import process_tree
9
10class TestProcessTree(unittest.TestCase):
11
12 def setUp(self):
13 self.name = "Process tree unittest"
14 self.rootdir = '../examples/1'
15 self.ps_stats = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_ps.log')).ps_stats
16 self.processtree = process_tree.ProcessTree(self.ps_stats, None, False, for_testing = True)
17
18 def mk_fname(self,f):
19 return os.path.join(self.rootdir, f)
20
21 def flatten(self, process_tree):
22 flattened = []
23 for p in process_tree:
24 flattened.append(p)
25 flattened.extend(self.flatten(p.child_list))
26 return flattened
27
28 def checkAgainstJavaExtract(self, filename, process_tree):
29 for expected, actual in zip(open(filename), self.flatten(process_tree)):
30 tokens = expected.split('\t')
31 self.assertEqual(int(tokens[0]), actual.pid)
32 self.assertEqual(tokens[1], actual.cmd)
33 self.assertEqual(long(tokens[2]), 10 * actual.start_time)
34 self.assert_(long(tokens[3]) - 10 * actual.duration < 5, "duration")
35 self.assertEqual(int(tokens[4]), len(actual.child_list))
36 self.assertEqual(int(tokens[5]), len(actual.samples))
37
38 def testBuild(self):
39 process_tree = self.processtree.process_tree
40 self.checkAgainstJavaExtract(self.mk_fname('extract.processtree.1.log'), process_tree)
41
42 def testMergeLogger(self):
43 self.processtree.merge_logger(self.processtree.process_tree, 'bootchartd', None, False)
44 process_tree = self.processtree.process_tree
45 self.checkAgainstJavaExtract(self.mk_fname('extract.processtree.2.log'), process_tree)
46
47 def testPrune(self):
48 self.processtree.merge_logger(self.processtree.process_tree, 'bootchartd', None, False)
49 self.processtree.prune(self.processtree.process_tree, None)
50 process_tree = self.processtree.process_tree
51 self.checkAgainstJavaExtract(self.mk_fname('extract.processtree.3b.log'), process_tree)
52
53 def testMergeExploders(self):
54 self.processtree.merge_logger(self.processtree.process_tree, 'bootchartd', None, False)
55 self.processtree.prune(self.processtree.process_tree, None)
56 self.processtree.merge_exploders(self.processtree.process_tree, set(['hwup']))
57 process_tree = self.processtree.process_tree
58 self.checkAgainstJavaExtract(self.mk_fname('extract.processtree.3c.log'), process_tree)
59
60 def testMergeSiblings(self):
61 self.processtree.merge_logger(self.processtree.process_tree, 'bootchartd', None, False)
62 self.processtree.prune(self.processtree.process_tree, None)
63 self.processtree.merge_exploders(self.processtree.process_tree, set(['hwup']))
64 self.processtree.merge_siblings(self.processtree.process_tree)
65 process_tree = self.processtree.process_tree
66 self.checkAgainstJavaExtract(self.mk_fname('extract.processtree.3d.log'), process_tree)
67
68 def testMergeRuns(self):
69 self.processtree.merge_logger(self.processtree.process_tree, 'bootchartd', None, False)
70 self.processtree.prune(self.processtree.process_tree, None)
71 self.processtree.merge_exploders(self.processtree.process_tree, set(['hwup']))
72 self.processtree.merge_siblings(self.processtree.process_tree)
73 self.processtree.merge_runs(self.processtree.process_tree)
74 process_tree = self.processtree.process_tree
75 self.checkAgainstJavaExtract(self.mk_fname('extract.processtree.3e.log'), process_tree)
76
77if __name__ == '__main__':
78 unittest.main()