github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/tools/LTE/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`
    50    writeResults
    51  }
    52  
    53  function writeResults {
    54    outputDir=`getOuputDir`
    55    echo "Test Output Start:"
    56    echo "$RAW_OUTPUT"
    57    echo "Test Output Finish"
    58    while read -r line; do
    59      echo $line >> $outputDir/$RAW_OUTPUT_FILE
    60      if [[ $line =~ $benchmarkLineRegex ]]; then
    61        resultsDataLine="`extractParamValues`, `nanosToSec ${BASH_REMATCH[1]}`"
    62        for d in $RESULTANT_DIRS; do
    63          dirSizeKBs=`du -sk $d | cut -f1`
    64          resultsDataLine="$resultsDataLine, `kbsToMbs $dirSizeKBs`"
    65        done
    66        echo $resultsDataLine >> $outputDir/$RESULTS_FILE
    67      fi
    68    done <<< "$RAW_OUTPUT"
    69  }
    70  
    71  function runTestSetup {
    72    outputDir=`getOuputDir`
    73    for d in ${TESTS_SETUP_DONE[@]}
    74      do
    75        if [ $d == $outputDir ]
    76        then
    77          return
    78        fi
    79      done
    80    createOutputDir
    81    writeResultsFileHeader
    82    TESTS_SETUP_DONE+=($outputDir)
    83  }
    84  
    85  function writeResultsFileHeader {
    86    outputDir=`getOuputDir`
    87    echo "" >> $outputDir/$RESULTS_FILE
    88    echo "# `date`" >> $outputDir/$RESULTS_FILE
    89    headerLine="# `extractParamNames`, Time_Spent(s)"
    90    for d in $RESULTANT_DIRS; do
    91      headerLine="$headerLine, Size_$(basename $d)(mb)"
    92    done
    93    echo "$headerLine" >> $outputDir/$RESULTS_FILE
    94  }
    95  
    96  function extractParamNames {
    97    echo $TEST_PARAMS | sed "s/$testParamRegex/\1/g"
    98  }
    99  
   100  function extractParamValues {
   101    echo $TEST_PARAMS | sed "s/$testParamRegex/\2/g"
   102  }
   103  
   104  function getOuputDir {
   105    pkgName=$(basename $PKG_NAME)
   106    outputDir="$OUTPUT_DIR_ROOT/$pkgName/$FUNCTION_NAME"
   107    if [ ! -z "$OUTPUT_DIR" ]; then
   108      outputDir="$OUTPUT_DIR_ROOT/$pkgName/$OUTPUT_DIR"
   109    fi
   110    echo $outputDir
   111  }
   112  
   113  function createOutputDir {
   114    outputDir=`getOuputDir`
   115    if [ ! -d "$outputDir" ]; then
   116      mkdir -p $outputDir
   117    else
   118      echo "INFO: outputDIR [$outputDir] already exists. Output will be appended to existing file"
   119    fi
   120  }
   121  
   122  function clearOSCache {
   123    platform=`uname`
   124    if [[ $platform == 'Darwin' ]]; then
   125      echo "Clearing os cache"
   126      sudo purge
   127    else
   128      echo "WARNING: Platform [$platform] is not supported for clearing os cache."
   129    fi
   130  }
   131  
   132  function nanosToSec {
   133    nanos=$1
   134    echo $(awk "BEGIN {printf \"%.2f\", ${nanos}/1000000000}")
   135  }
   136  
   137  function kbsToMbs {
   138    kbs=$1
   139    echo $(awk "BEGIN {printf \"%.2f\", ${kbs}/1024}")
   140  }