1#!/usr/local/bin/bash
2# Script to format tests results into a single xml file.
3# See https://wiki.jenkins-ci.org/display/JENKINS/PerfPublisher+Plugin
4# -----
5# 2014/11/17 - Written by AB <Alexis.Breust@imag.fr>
6
7XMLFILE=$1
8tests=$2
9COMPILER=$3
10
11# choose gdate on OS X
12if command -v "gdate" >/dev/null; then
13    DATE=gdate
14else
15    DATE=date
16fi
17#=================#
18# Plateform infos #
19#=================#
20
21COMPILERVERSION=$($COMPILER --version 2>&1 | head -1)
22
23if command -v "lscpu" >/dev/null; then
24    CPUFREQ=$(lscpu | grep "MHz" | rev | cut -f1 -d' ' | rev)
25else
26    CPUFREQ=$((`sysctl -n hw.cpufrequency`/1000000))
27fi
28
29ARCH=$(uname -m)
30OSNAME=$(uname -s)
31OSVERSION=$(uname -r)
32
33if hash lsb_release 2>/dev/null
34	then DISTRIB=$(lsb_release -ds)
35	else DISTRIB='Unknown distribution'
36fi
37
38#==========#
39# Prologue #
40#==========#
41
42if [[ -f $XMLFILE ]]
43then
44	echo '----> WARNING: File '$XMLFILE' is not empty.'
45	echo '---->          Results will be added to its end.'
46fi
47
48#========#
49# Header #
50#========#
51
52echo '<?xml version="1.0" encoding="UTF-8"?>' >> $XMLFILE
53echo '<report name="tests-report" categ="tests">' >> $XMLFILE
54
55#=======#
56# Start #
57#=======#
58
59echo '<start>' >> $XMLFILE
60echo '<date format="YYYYMMDD" val="'$($DATE +%Y%m%d)'" />' >> $XMLFILE
61echo '<time format="HHMMSS" val="'$($DATE +%H%M%S)'" />' >> $XMLFILE
62echo '</start>' >> $XMLFILE
63
64#=======#
65# Tests #
66#=======#
67
68for test in $tests
69do
70	if [[ ! -f $test ]]
71	then
72		#File does not exist: compile it
73		echo '[Compiling]' $test
74		COMPILESTART=$($DATE +%s%3N)
75		COMPILELOG=$(make $test 2>&1; echo 'Returned state: '$?)
76		COMPILEEND=$($DATE +%s%3N)
77		COMPILETIME=$(($COMPILEEND - $COMPILESTART))
78		COMPILECHECK=$(echo $COMPILELOG | grep -o '[^ ]*$')
79		COMPILETIMERELEVANT='true'
80	else
81		#File does exist
82		echo '[Already compiled]' $benchmark
83		COMPILELOG='(Previously compiled)'
84		COMPILETIME='0.0'
85		COMPILECHECK='0'
86		COMPILETIMERELEVANT='false'
87	fi
88
89	if [[ $COMPILECHECK -ne 0 ]]
90	then
91		#Compilation failure
92		# EXECUTED='no' - keep it to yes so that Jenkins
93		# uses it within its results
94		EXECUTED='yes'
95		PASSED='no'
96		STATE='0'
97		EXECUTIONLOG='(Not executed)'
98		EXECUTIONTIME='0.0'
99		COMPILETIMERELEVANT='false'
100		EXECUTIONTIMERELEVANT='false'
101		ERRORLOG='Does not compile.'
102		echo '-> Does not compile.'
103	else
104		#Compilation success
105		echo '[Executing]' $test
106		EXECUTED='yes'
107		EXECUTIONSTART=$($DATE +%s%3N)
108		EXECUTIONLOG=$(./$test  2>&1; echo 'Returned state: '$?)
109		EXECUTIONEND=$($DATE +%s%3N)
110		EXECUTIONTIME=$(($EXECUTIONEND - $EXECUTIONSTART))
111		EXECUTIONCHECK=$(echo $EXECUTIONLOG | grep -o '[^ ]*$')
112
113		if [[ $EXECUTIONCHECK -ne 0 ]]
114		then
115			#Execution failure
116			PASSED='no'
117			STATE='0'
118			EXECUTIONTIMERELEVANT='false'
119			ERRORLOG='Execution failure.'
120			echo '-> Execution failure.'
121		else
122			#Execution success
123			PASSED='yes'
124			STATE='100'
125			EXECUTIONTIMERELEVANT='true'
126			ERRORLOG=''
127		fi
128	fi
129
130	echo '<test name="'$test'" executed="'$EXECUTED'">' >> $XMLFILE
131	echo '<targets><target>TEST</target></targets>' >> $XMLFILE
132	echo '<platform>' >> $XMLFILE
133	echo '<os>' >> $XMLFILE
134	echo '<name><![CDATA['$OSNAME']]></name>' >> $XMLFILE
135	echo '<version><![CDATA['$OSVERSION']]></version>' >> $XMLFILE
136	echo '<distribution><![CDATA['$DISTRIB']]></distribution>' >> $XMLFILE
137	echo '</os>' >> $XMLFILE
138	echo '<processor arch="'$ARCH'">' >> $XMLFILE
139	echo '<frequency unit="MHz" cpufreq="'$CPUFREQ'" />' >> $XMLFILE
140	echo '</processor>' >> $XMLFILE
141	echo '<compiler name="'$COMPILER'" version="'$COMPILERVERSION'" />' >> $XMLFILE
142	echo '</platform>' >> $XMLFILE
143	echo '<result>' >> $XMLFILE
144
145	# Logs
146	echo '<success passed="'$PASSED'" state="'$STATE'" />' >> $XMLFILE
147	echo '<errorlog><![CDATA['$ERRORLOG']]></errorlog>' >> $XMLFILE
148	echo '<log name="Compile output"><![CDATA['"$COMPILELOG"']]></log>' >> $XMLFILE
149	echo '<log name="Execution output"><![CDATA['"$test $EXECUTIONLOG"']]></log>' >> $XMLFILE
150
151	# Times
152	echo '<compiletime unit="ms" mesure="'$COMPILETIME'" isRelevant="'$COMPILETIMERELEVANT'" />' >> $XMLFILE
153	echo '<executiontime unit="ms" mesure="'$EXECUTIONTIME'" isRelevant="'$EXECUTIONTIMERELEVANT'" />' >> $XMLFILE
154
155	echo '</result>' >> $XMLFILE
156	echo '</test>' >> $XMLFILE
157done
158
159#========#
160# Footer #
161#========#
162
163echo '</report>' >> $XMLFILE
164
165#==========#
166# Epilogue #
167#==========#
168
169echo 'Results correctly exported to' $XMLFILE
170
171