1#!/bin/bash
2# Script to format benchmarks 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
8benchmarks=$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="benchmarks-report" categ="benchmarks">' >> $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# Benchmarks #
66#============#
67
68for benchmark in $benchmarks
69do
70	if [[ ! -f $benchmark ]]
71	then
72		#File does not exist: compile it
73		echo '[Compiling]' $benchmark
74		COMPILESTART=$($DATE +%s%3N)
75		COMPILELOG=$(make $benchmark 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		PERFORMANCEFLOPS='0.0'
100		COMPILETIMERELEVANT='false'
101		EXECUTIONTIMERELEVANT='false'
102		PERFORMANCEFLOPSRELEVANT='false'
103		ERRORLOG='Does not compile.'
104		echo '-> Does not compile.'
105	else
106		#Compilation success
107		echo '[Executing]' $benchmark
108		EXECUTED='yes'
109		EXECUTIONLOG=$(./$benchmark 2>&1)
110
111		if [[ ${EXECUTIONLOG} != "Time:"* ]]
112		then
113			#Execution failure
114			PASSED='no'
115			STATE='0'
116			EXECUTIONTIME='0.0'
117			PERFORMANCEFLOPS='0.0'
118			EXECUTIONTIMERELEVANT='false'
119			PERFORMANCEFLOPSRELEVANT='false'
120			ERRORLOG='Unexpected output.'
121			echo '-> Unexpected output.'
122		else
123			#Execution success
124			PASSED='yes'
125			STATE='100'
126			EXECUTIONTIME=$(echo $EXECUTIONLOG | cut -d' ' -f2)
127			PERFORMANCEFLOPS=$(echo $EXECUTIONLOG | cut -d' ' -f4)
128			EXECUTIONTIMERELEVANT='true'
129			if [[ ${PERFORMANCEFLOPS} != "Irrelevant" ]]
130			then
131				PERFORMANCEFLOPSRELEVANT='true'
132			else
133				PERFORMANCEFLOPSRELEVANT='false'
134				PERFORMANCEFLOPS='0.0'
135			fi
136			ERRORLOG=''
137		fi
138	fi
139
140	echo '<test name="'$benchmark'" executed="'$EXECUTED'">' >> $XMLFILE
141	echo '<targets><target>BENCHMARK</target></targets>' >> $XMLFILE
142	echo '<platform>' >> $XMLFILE
143	echo '<os>' >> $XMLFILE
144	echo '<name><![CDATA['$OSNAME']]></name>' >> $XMLFILE
145	echo '<version><![CDATA['$OSVERSION']]></version>' >> $XMLFILE
146	echo '<distribution><![CDATA['$DISTRIB']]></distribution>' >> $XMLFILE
147	echo '</os>' >> $XMLFILE
148	echo '<processor arch="'$ARCH'">' >> $XMLFILE
149	echo '<frequency unit="MHz" cpufreq="'$CPUFREQ'" />' >> $XMLFILE
150	echo '</processor>' >> $XMLFILE
151	echo '<compiler name="'$COMPILER'" version="'$COMPILERVERSION'" />' >> $XMLFILE
152	echo '</platform>' >> $XMLFILE
153	echo '<result>' >> $XMLFILE
154
155	# Logs
156	echo '<success passed="'$PASSED'" state="'$STATE'" />' >> $XMLFILE
157	echo '<errorlog><![CDATA['$ERRORLOG']]></errorlog>' >> $XMLFILE
158	echo '<log name="Compile output"><![CDATA['"$COMPILELOG"']]></log>' >> $XMLFILE
159	echo '<log name="Execution output"><![CDATA['"$benchmark $EXECUTIONLOG"']]></log>' >> $XMLFILE
160
161	# Times
162	echo '<compiletime unit="ms" mesure="'$COMPILETIME'" isRelevant="'$COMPILETIMERELEVANT'" />' >> $XMLFILE
163	echo '<executiontime unit="s" mesure="'$EXECUTIONTIME'" isRelevant="'$EXECUTIONTIMERELEVANT'" />' >> $XMLFILE
164	echo '<performance unit="GFLOPS" mesure="'$PERFORMANCEFLOPS'" isRelevant="'$PERFORMANCEFLOPSRELEVANT'" />' >> $XMLFILE
165
166	echo '</result>' >> $XMLFILE
167	echo '</test>' >> $XMLFILE
168done
169
170#========#
171# Footer #
172#========#
173
174echo '</report>' >> $XMLFILE
175
176#==========#
177# Epilogue #
178#==========#
179
180echo 'Results correctly exported to' $XMLFILE
181
182