github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/benchmark/scripts/common.sh (about) 1 #!/bin/bash 2 # 3 # Copyright IBM Corp. All Rights Reserved. 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 8 9 set -e 10 11 ############################################################################################################################# 12 # This shell script contains common functions that can be used across benchmark tests 13 # Following is the description of the list of variables that this script uses 14 # 15 # OUTPUT_DIR_ROOT - Root dir for tests results 16 # RAW_OUTPUT_FILE - File name that contains the raw output produced by a test that otherwiese is printed on screen 17 # RESULTS_FILE - File name that contains the test parameters and the time taken by the test (in csv format) 18 # PKG_NAME - Name of the golang package for the test 19 # FUNCTION_NAME - Name of the Benchmark function 20 # TEST_PARAMS - Parameters for the test 21 # RESULTANT_DIRS - An optional list of dirs whose size needs to be captured in the results in the RESULTS_FILE 22 # 23 # The default values for some of the above variables are set in this script and can be overridden by a test specific 24 # script. For remaining variables, a test specific script needs to set the appropriate values before calling the 25 # function 'executeTest' of this script 26 # 27 # The result file for a test gets created in a csv format in the folder 28 # $OUTPUT_DIR_ROOT/<last_element_of>$PKG_NAME<segment>/$FUNCTION_NAME 29 ############################################################################################################################# 30 31 32 OUTPUT_DIR_ROOT=`echo /tmp` 33 RAW_OUTPUT_FILE="output_LTE.log" 34 RESULTS_FILE="results.csv" 35 36 benchmarkLineRegex="^Benchmark.*[[:blank:]]+[[:digit:]]+[[:blank:]]+([[:digit:]]+).*$" 37 testParamRegex="-\([^=]*\)=\([^,]*\)" 38 39 echo "**Note**: This is a Benchmark test. Please make sure to set an appropriate value for ulimit in your OS." 40 echo "Recommended value is 10000 for default parameters." 41 echo "Current ulimit=`ulimit -n`" 42 TESTS_SETUP_DONE=() 43 44 ## Execute test and generate data file 45 function executeTest { 46 runTestSetup 47 cmd="go test -v -timeout 1000m $PKG_NAME -testParams=\"$TEST_PARAMS\" -bench=$FUNCTION_NAME" 48 echo $cmd 49 RAW_OUTPUT=`eval $cmd || true` 50 if [[ "$RAW_OUTPUT" == *"FAIL"* ]]; then 51 printf "%s\n" "$RAW_OUTPUT" 52 echo "Failed to run the test."; 53 return 1; 54 fi 55 writeResults 56 } 57 58 function writeResults { 59 outputDir=`getOuputDir` 60 echo "Test Output Start:" 61 echo "$RAW_OUTPUT" 62 echo "Test Output Finish" 63 while read -r line; do 64 echo $line >> $outputDir/$RAW_OUTPUT_FILE 65 if [[ $line =~ $benchmarkLineRegex ]]; then 66 resultsDataLine="`extractParamValues`, `nanosToSec ${BASH_REMATCH[1]}`" 67 for d in $RESULTANT_DIRS; do 68 if [ -d $d ] 69 then 70 dirSizeKBs=`du -sk $d | cut -f1` 71 resultsDataLine="$resultsDataLine, `kbsToMbs $dirSizeKBs`" 72 else 73 resultsDataLine="$resultsDataLine, DoesNotExist" 74 fi 75 done 76 echo $resultsDataLine >> $outputDir/$RESULTS_FILE 77 fi 78 done <<< "$RAW_OUTPUT" 79 } 80 81 function runTestSetup { 82 outputDir=`getOuputDir` 83 for d in ${TESTS_SETUP_DONE[@]} 84 do 85 if [ $d == $outputDir ] 86 then 87 return 88 fi 89 done 90 createOutputDir 91 writeResultsFileHeader 92 TESTS_SETUP_DONE+=($outputDir) 93 } 94 95 function writeResultsFileHeader { 96 outputDir=`getOuputDir` 97 echo "" >> $outputDir/$RESULTS_FILE 98 echo "# `date`" >> $outputDir/$RESULTS_FILE 99 headerLine="# `extractParamNames`, Time_Spent(s)" 100 for d in $RESULTANT_DIRS; do 101 headerLine="$headerLine, Size_$(basename $d)(mb)" 102 done 103 echo "$headerLine" >> $outputDir/$RESULTS_FILE 104 } 105 106 function extractParamNames { 107 echo $TEST_PARAMS | sed "s/$testParamRegex/\1/g" 108 } 109 110 function extractParamValues { 111 echo $TEST_PARAMS | sed "s/$testParamRegex/\2/g" 112 } 113 114 function getOuputDir { 115 pkgName=$(basename $PKG_NAME) 116 outputDir="$OUTPUT_DIR_ROOT/$pkgName/$FUNCTION_NAME" 117 if [ ! -z "$OUTPUT_DIR" ]; then 118 outputDir="$OUTPUT_DIR_ROOT/$pkgName/$OUTPUT_DIR" 119 fi 120 echo $outputDir 121 } 122 123 function createOutputDir { 124 outputDir=`getOuputDir` 125 if [ ! -d "$outputDir" ]; then 126 mkdir -p $outputDir 127 else 128 echo "INFO: outputDIR [$outputDir] already exists. Output will be appended to existing file" 129 fi 130 } 131 132 function clearOSCache { 133 platform=`uname` 134 if [[ $platform == 'Darwin' ]]; then 135 echo "Clearing os cache" 136 sudo purge 137 else 138 echo "WARNING: Platform [$platform] is not supported for clearing os cache." 139 fi 140 } 141 142 function nanosToSec { 143 nanos=$1 144 echo $(awk "BEGIN {printf \"%.2f\", ${nanos}/1000000000}") 145 } 146 147 function kbsToMbs { 148 kbs=$1 149 echo $(awk "BEGIN {printf \"%.2f\", ${kbs}/1024}") 150 } 151 152 function upCouchDB { 153 if [ "$useCouchDB" == "true" ]; 154 then 155 downCouchDB 156 echo "Starting couchdb container on port 5984..." 157 export COUCHDB_ADDR=localhost:5984 158 docker run --publish 5984:5984 --detach --name couchdb couchdb:2.3 >/dev/null 159 sleep 5 160 fi 161 } 162 163 function downCouchDB { 164 couch_id=$(docker ps -aq --filter 'ancestor=couchdb:2.3') 165 if [ "$couch_id" != "" ]; then 166 echo "Stopping couchdb container (id: $couch_id)..." 167 docker rm -f $couch_id &>/dev/null 168 fi 169 sleep 5 170 } 171 172 trap downCouchDB EXIT