Heat context code refactor part 2
[yardstick.git] / yardstick / benchmark / scenarios / compute / perf_benchmark.bash
1 #!/bin/sh
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 set -e
13
14 # Commandline arguments
15 PAYLOAD_OP=$1
16 shift
17 DURATION=$1
18 shift
19 EVENTS=("$@")
20 OUTPUT_FILE=/tmp/perfout.txt
21
22 # run perf test
23 run_perf()
24 {
25     COMMA_SEP_E=$( IFS=$','; echo "${EVENTS[*]}" )
26
27     if [[ $PAYLOAD_OP == dd* ]]
28     then
29         sudo perf stat -o $OUTPUT_FILE -e ${COMMA_SEP_E[@]} $PAYLOAD_OP &
30         sleep $DURATION
31         sudo killall -q -u root dd
32     else
33         sudo perf stat -o $OUTPUT_FILE -e ${COMMA_SEP_E[@]} $PAYLOAD_OP
34     fi
35 }
36
37 # write the result to stdout in json format
38 output_json()
39 {
40     EVENTS+=('time')
41
42     last_pos=$(( ${#EVENTS[*]} - 1 ))
43     last=${EVENTS[$last_pos]}
44
45     echo -n {
46     for EVENT in ${EVENTS[@]}
47     do
48         value=$(cat $OUTPUT_FILE | grep $EVENT | awk 'match($0,/[0-9]+|[0-9]+\.[0-9]*/, a) { print a[0]}')
49
50         if [[ $EVENT != $last ]]
51         then
52             echo -n \"$EVENT\": $value,
53         else
54             echo -n \"$EVENT\": $value
55         fi
56     done
57     echo }
58 }
59
60 # main entry
61 main()
62 {
63     run_perf > /dev/null 2>&1
64     sleep 1
65     output_json
66 }
67
68 main