5
5
import time
6
6
import threading
7
7
import re
8
- from config_fuzzer import RunParameters
8
+ from xml .dom import minidom
9
+ from xml .parsers .expat import ExpatError
10
+ from config_fuzzer import RunParameters
9
11
10
12
def get_emma_coverage (restore_count ):
11
13
# adb shell am broadcast -a edu.gatech.m3.emma.COLLECT_COVERAGE
@@ -15,51 +17,41 @@ def get_emma_coverage(restore_count):
15
17
output , err = cmd .communicate ()
16
18
print 'get_emma_coverage- return code: ' + ' output: ' + str (output ) + ' ' + str (err ) + ' '
17
19
20
+ def get_jacoco_coverage (restore_count , app_package_name , app_class_files_path , avd_serial ):
21
+ # adb shell am broadcast -a edu.gatech.m3.emma.COLLECT_COVERAGE
22
+ cov_file_name = 'coverage' + str (restore_count )
23
+
24
+ cmd = subprocess .Popen (['../../scripts/pull_coverage_jacoco.sh' , cov_file_name , app_package_name , app_class_files_path , avd_serial ],
25
+ stdout = subprocess .PIPE , stderr = subprocess .PIPE )
26
+ output , err = cmd .communicate ()
27
+ print 'get_jacoco_coverage- return code: ' + ' output: ' + str (output ) + ' ' + str (err ) + ' '
28
+
18
29
def get_ella_coverage (restore_count ):
19
30
cov_file_name = 'coverage' + str (restore_count )
20
-
21
-
31
+
32
+
22
33
cmd = subprocess .Popen (['../../scripts/pull_coverage_ella.sh' , cov_file_name ],stdout = subprocess .PIPE , stderr = subprocess .PIPE )
23
34
output , err = cmd .communicate ()
24
- print 'get_ella_coverage: return msg: error: ' + str (output ) + ' ' + str (err )
35
+ print 'get_ella_coverage: return msg: error: ' + str (output ) + ' ' + str (err )
25
36
26
37
def compute_coverage ():
27
38
#using compute_coverage to compute
28
39
cmd = subprocess .Popen ('../../scripts/compute_coverage.sh' ,stdout = subprocess .PIPE , stderr = subprocess .PIPE )
29
40
output , err = cmd .communicate ()
30
41
print 'compute_coverage-return code: ' + ' output: ' + str (output ) + ' ' + str (err ) + ' '
31
42
32
- def compute_coverage_ella ( ):
43
+ def compute_coverage_jacoco ( app_class_files_path ):
33
44
#using compute_coverage to compute
34
-
35
- cmd = subprocess .Popen ('../../scripts/compute_coverage_ella.sh' ,stdout = subprocess .PIPE , stderr = subprocess .PIPE )
45
+ cmd = subprocess .Popen (['../../scripts/compute_coverage_jacoco.sh' , app_class_files_path ],stdout = subprocess .PIPE , stderr = subprocess .PIPE )
36
46
output , err = cmd .communicate ()
37
47
print 'compute_coverage-return code: ' + ' output: ' + str (output ) + ' ' + str (err ) + ' '
38
48
39
- def read_coverage_ella ():
40
- if not os .path .isfile ('../../output/ella_files/cov' ):
41
- return 0
42
- f = open ('../../output/ella_files/cov' , 'r' )
43
- lines = f .readlines ()
44
- f .close ()
45
-
46
- return int (lines [0 ])
49
+ def compute_coverage_ella ():
50
+ #using compute_coverage to compute
47
51
48
- def read_current_coverage ():
49
- if RunParameters .OPEN_SOURCE :
50
- return read_coverage ()
51
- else :
52
- return read_coverage_ella ()
53
- def compute_current_coverage ():
54
- if RunParameters .OPEN_SOURCE :
55
- compute_coverage ()
56
- else :
57
- compute_coverage_ella ()
58
- def pull_coverage_files (restore_count ):
59
- if RunParameters .OPEN_SOURCE :
60
- get_emma_coverage (restore_count )
61
- else :
62
- get_ella_coverage (restore_count )
52
+ cmd = subprocess .Popen ('../../scripts/compute_coverage_ella.sh' ,stdout = subprocess .PIPE , stderr = subprocess .PIPE )
53
+ output , err = cmd .communicate ()
54
+ print 'compute_coverage-return code: ' + ' output: ' + str (output ) + ' ' + str (err ) + ' '
63
55
64
56
def read_coverage ():
65
57
if not os .path .isfile ('../../output/coverage.txt' ):
@@ -82,6 +74,64 @@ def read_coverage():
82
74
83
75
return int (s .replace ("%" , "" ))
84
76
77
+ def read_coverage_jacoco ():
78
+ if not os .path .isfile ('../../output/coverage.xml' ):
79
+ return 0
80
+
81
+ try :
82
+ # see the format of coverage report generated by Jacoco in xml
83
+ xmldoc = minidom .parse ('../../output/coverage.xml' )
84
+ counters = xmldoc .getElementsByTagName ('counter' )
85
+ line_coverage = 0
86
+ for counter in counters :
87
+ type_name = counter .getAttribute ('type' )
88
+ if type_name == 'LINE' :
89
+ missed_lines = int (counter .getAttribute ('missed' ))
90
+ covered_lines = int (counter .getAttribute ('covered' ))
91
+ line_coverage = covered_lines * 100.0 / (missed_lines + covered_lines )
92
+
93
+ if line_coverage == 0 :
94
+ return 0
95
+ else :
96
+ print "-----------"
97
+ print "current jacoco line coverage: " + str (line_coverage )
98
+ print "-----------"
99
+ return line_coverage
100
+ except ExpatError :
101
+ print "parse xml error, catch it!"
102
+ return 0
103
+
104
+ def read_coverage_ella ():
105
+ if not os .path .isfile ('../../output/ella_files/cov' ):
106
+ return 0
107
+ f = open ('../../output/ella_files/cov' , 'r' )
108
+ lines = f .readlines ()
109
+ f .close ()
110
+ print "[tingsu] current ella method coverage: " + str (lines [0 ])
111
+
112
+ return int (lines [0 ])
113
+
114
+ def read_current_coverage ():
115
+ if RunParameters .OPEN_SOURCE :
116
+ # return read_coverage()
117
+ return read_coverage_jacoco ()
118
+ else :
119
+ return read_coverage_ella ()
120
+
121
+ def compute_current_coverage (app_class_files_path ):
122
+ if RunParameters .OPEN_SOURCE :
123
+ # compute_coverage()
124
+ compute_coverage_jacoco (app_class_files_path )
125
+ else :
126
+ compute_coverage_ella ()
127
+
128
+ def pull_coverage_files (restore_count , app_package_name , app_class_files_path , avd_serial ):
129
+ if RunParameters .OPEN_SOURCE :
130
+ # get_emma_coverage(restore_count)
131
+ get_jacoco_coverage (restore_count , app_package_name , app_class_files_path , avd_serial )
132
+ else :
133
+ get_ella_coverage (restore_count )
134
+
85
135
def record_coverage ():
86
136
start_time = time .time ()
87
137
file_path = '../../output/coverage_time.csv'
@@ -92,10 +142,13 @@ def record_coverage():
92
142
while True :
93
143
with open (file_path , 'a' ) as coverage_file :
94
144
t = time .time () - start_time
95
-
96
- compute_coverage ()
97
- coverage = read_coverage ()
98
-
145
+
146
+ # compute_coverage()
147
+ # coverage = read_coverage()
148
+ # TODO, record_coverage() is not used in the project, so it is okay for now without giving proper parameters.
149
+ compute_coverage_jacoco ()
150
+ coverage = read_coverage_jacoco ()
151
+
99
152
line = str (round (t ,1 )) + "," + str (coverage ) + "\n "
100
153
coverage_file .writelines (line )
101
154
coverage_file .flush ()
@@ -114,3 +167,4 @@ def record_coverage():
114
167
compute_coverage_ella ()
115
168
tp = read_coverage_ella ()
116
169
print str (tp )
170
+
0 commit comments