Merge "Add branch name and remove triggers in metal3 container image build job"
[infra/cicd.git] / jjb / onap / cps / prepare-performance-tests-data.sh
index 1a840a880e30303dad447a4c9f7e75dd0d4e22a1..3205eb3cbd0a765e09f23e977c9ab79490335331 100644 (file)
@@ -1,5 +1,5 @@
 #!/bin/bash
-
+sudo apt-get install bc
 #############################################################################################################################
 ################################################ F U N C T I O N S ##########################################################
 #############################################################################################################################
@@ -21,195 +21,21 @@ read_datatreees_pattern="^.*Read datatrees using openroadm root.*"
 update_datanodes_pattern="^.*Update 100 data nodes.*"
 batch_delete_pattern="^.*Batch delete 100 whole lists.*"
 
-all_patterns=("$creating_bookstore_pattern" "$read_datatreees_pattern" "$update_datanodes_pattern" "$batch_delete_pattern")
 new_data=""
-
-latestBuild=$(getLatestBuild)
-availableBuilds=()
-consoleText=""
 JENKINS_JOB_URL="https://jenkins.nordix.org/job/onap-cps-master-performance-test-java"
 
-# Create and add header to the file
-createAndAddHeader() {
-    file="$1"
-    header="$2"
-    if [[ ! -f "$file" ]]; then
-        echo "Creating $file"
-        touch "$file"
-        echo "Build,$header" > $file
-    else
-        echo "$file already exists"
-    fi
-}
-
 # Get latest build
 getLatestBuild() {
     latestBuild=$(curl -s "${JENKINS_JOB_URL}/lastBuild/api/json" | jq -r '.number')
     echo "latest build is $latestBuild"
 }
 
-getConsoleText() {
-    buildToRead=$1
-    consoleURL="${JENKINS_JOB_URL}/${buildToRead}/consoleText"
-    consoleText=$(curl -s "$consoleURL")
-}
-
-getAndRecordDataResults() {
-    consoleText=$1
-    patternToMatch=$2
-    dataFile=$3
-    buildNumber=$4
-    new_data=""
-    matched_line=""
-    limit_value=""
-    took_value=""
-
-    # Get and calculate data for plot
-    if matched_line=$(echo "$consoleText" | grep -o -P "$patternToMatch"); then
-       echo "Matched line: $matched_line"
-       limit_value=$(echo "$matched_line" | grep -o -P 'limit\s*\K\d+(,\d+)?' | tr -cd '[:digit:]')
-        echo $limit_value
-       took_value=$(echo "$matched_line" | grep -o -P 'took\s*\K\d+(,\d+)?' | tr -cd '[:digit:]')
-        echo $took_value
-       percentage=$(($took_value * 100 / $limit_value))
-
-       echo "Limit value: $limit_value"
-       echo "Took value: $took_value"
-       echo "Percentage: $percentage"
-
-    if [ -z "$new_data" ]; then
-       new_data="$percentage"
-       else
-       new_data+=",$percentage"
-       fi
-       echo "New data: $new_data"
-
-       else
-       echo "No match found."
-       fi
-
-    # Record result
-    lastLine=$(tail -n 1 "$dataFile")
-    newLine="$buildNumber,$new_data"
-    if [ -z "$new_data" ]; then
-        echo "No new data"
-    elif [ $newLine == $lastLine ]; then
-       echo "Data already exists"
-    else
-        echo "$buildNumber,$new_data" >> $dataFile
-        echo "New data added"
-    fi
-}
-
-buildStaticReport() {
-    dataFile="$1"  # Get the input file name from the function parameter
-    chartTitle="$2"
-
-    sudo apt-get update
-    sudo apt-get install -y gnuplot
-
-    # Create a temporary Gnuplot script
-    cat <<EOT > gnuplot_script.gp
-    set datafile separator ","
-    set terminal pngcairo size 1500,600
-    set output "${chartTitle}.png"
-
-    # Set X-axis label
-    set xlabel "Build"
-
-    # Read titles from the first line of the CSV
-    plot '$dataFile' using (column(0)+1):2:xtic(1) with linespoints title "Y values"
-EOT
-
-    # Run the Gnuplot script
-    gnuplot gnuplot_script.gp
-
-    # Remove the temporary script
-    rm gnuplot_script.gp
-}
-
-
-buildHTMLReport() {
-
-    chartTitle1="$1"
-    chartFileName1="$2"   # Get the chart file name as the first parameter
-
-    chartTitle2="$3"
-    chartFileName2="$4"
-
-    chartTitle3="$5"
-    chartFileName3="$6"
-
-    chartTitle4="$7"
-    chartFileName4="$8"
-
-    reportTitle="$9"     # Get the report title as the second parameter
-
-cat <<EOT > "index.html"
-<!DOCTYPE html>
-<html>
-<head>
-  <title>$reportTitle</title>
-</head>
-<body>
-    <h1 style="text-align: center;">$reportTitle</h1>
-    <table align="center">
-        <tr> <!-- First Row -->
-            <td align="center">
-                <figcaption>"$chartTitle1"</figcaption>
-                <img src="$chartFileName1" alt="Image 1" width="750" height="300">
-            </td>
-            <td align="center" style="padding: 10px;">
-                <figcaption>"$chartTitle2"</figcaption>
-                <img src="$chartFileName2" alt="Image 2" width="750" height="300">
-            </td>
-        </tr>
-        <tr> <!-- Second Row -->
-            <td align="center" style="padding: 10px;">
-                <figcaption>"$chartTitle3"</figcaption>
-                <img src="$chartFileName3" alt="Image 3" width="750" height="300">
-            </td>
-            <td align="center" style="padding: 10px;">
-                <figcaption>"$chartTitle4"</figcaption>
-                <img src="$chartFileName4" alt="Image 4" width="750" height="300">
-            </td>
-        </tr>
-    </table>
-</body>
-</html>
-EOT
-}
-
-#!/bin/bash
-
-#############################################################################################################################
-################################################ F U N C T I O N S ##########################################################
-#############################################################################################################################
-
-create_data="create_performance_data.txt"
-read_data="read_performance_data.txt"
-update_data="update_performance_data.txt"
-delete_data="delete_performance_data.txt"
-
-# Data files headers
-create_data_title_1="Creating openroadm anchors with large data"
-read_data_title_1="Read datatrees using openroadm root"
-update_data_title_1="Update 100 data nodes"
-delete_data_title_1="Batch delete 100 whole lists"
-
-# Text patterns to match in console log
-creating_bookstore_pattern="^.*Creating openroadm anchors with large.*"
-read_datatreees_pattern="^.*Read datatrees using openroadm root.*"
-update_datanodes_pattern="^.*Update 100 data nodes.*"
-batch_delete_pattern="^.*Batch delete 100 whole lists.*"
-
-all_patterns=("$creating_bookstore_pattern" "$read_datatreees_pattern" "$update_datanodes_pattern" "$batch_delete_pattern")
-new_data=""
-
 latestBuild=$(getLatestBuild)
-availableBuilds=()
+latestBuildToRecord=""
 consoleText=""
-JENKINS_JOB_URL="https://jenkins.nordix.org/job/onap-cps-master-performance-test-java"
+lastRecordedBuild=""
+latestRecordedBuild=""
+timestampOfLatestRecordedBuild=""
 
 # Create and add header to the file
 createAndAddHeader() {
@@ -224,12 +50,7 @@ createAndAddHeader() {
     fi
 }
 
-# Get latest build
-getLatestBuild() {
-    latestBuild=$(curl -s "${JENKINS_JOB_URL}/lastBuild/api/json" | jq -r '.number')
-    echo "latest build is $latestBuild"
-}
-
+# Get the console text from specific build of the performance job
 getConsoleText() {
     buildToRead=$1
     consoleURL="${JENKINS_JOB_URL}/${buildToRead}/consoleText"
@@ -249,11 +70,12 @@ getAndRecordDataResults() {
     # Get and calculate data for plot
     if matched_line=$(echo "$consoleText" | grep -o -P "$patternToMatch"); then
        echo "Matched line: $matched_line"
-       limit_value=$(echo "$matched_line" | grep -o -P 'limit\s*\K\d+(,\d+)?' | tr -cd '[:digit:]')
+       limit_value=$(echo "$matched_line" | grep -o -P 'limit\s*\K\d+(\.\d+)?' | tr -cd '[:digit:].')
         echo $limit_value
-       took_value=$(echo "$matched_line" | grep -o -P 'took\s*\K\d+(,\d+)?' | tr -cd '[:digit:]')
+       took_value=$(echo "$matched_line" | grep -o -P 'took\s*\K\d+(\.\d+)?' | tr -cd '[:digit:].')
         echo $took_value
-       percentage=$(($took_value * 100 / $limit_value))
+       percentage=$(echo "scale=2; $took_value * 100.00 / $limit_value" | bc)
+
 
        echo "Limit value: $limit_value"
        echo "Took value: $took_value"
@@ -275,14 +97,27 @@ getAndRecordDataResults() {
     newLine="$buildNumber,$new_data"
     if [ -z "$new_data" ]; then
         echo "No new data"
+        lastRecordedBuild=$(echo "$lastLine" | awk -F, '{print $1}')
+        recordLatestRecordedBuild "$lastRecordedBuild"
     elif [ $newLine == $lastLine ]; then
-       echo "Data already exists"
+         echo "Data already exists"
+        recordLatestRecordedBuild "$buildNumber"
     else
         echo "$buildNumber,$new_data" >> $dataFile
         echo "New data added"
+        recordLatestRecordedBuild "$buildNumber"
     fi
 }
 
+recordLatestRecordedBuild() {
+       latestBuildToRecord="$1"
+  timestampOfLatestRecordedBuild=$(curl -s "${JENKINS_JOB_URL}/${latestBuildToRecord}/api/json" | jq -r '.timestamp')
+       formattedTimestampOfLatestRecordedBuild=$(date -d "@$((timestampOfLatestRecordedBuild / 1000))" "+%B %e, %Y at %H:%M")
+    echo "Latest recorded build: $latestBuildToRecord"
+    echo "Date of latest recorded build: $formattedTimestampOfLatestRecordedBuild"
+    latestRecordedBuild=$latestBuildToRecord
+}
+
 buildStaticReport() {
     dataFile="$1"  # Get the input file name from the function parameter
     chartTitle="$2"
@@ -299,8 +134,16 @@ buildStaticReport() {
     # Set X-axis label
     set xlabel "Build"
 
-    # Read titles from the first line of the CSV
-    plot '$dataFile' using (column(0)+1):2:xtic(1) with linespoints title "Y values"
+    # Set Y-axis label
+    set ylabel "Percentage of limit %"
+
+         # Get stats for min and max
+    stats '$dataFile' using 1
+         xmin = STATS_min
+         freq = 5 #frequency
+         xLabel(x) = (int((x - xmin) / freq) % 2 == 0) ? sprintf("%d", x) : ""
+
+       plot '$dataFile' using (column(0)+1):2:xtic(xLabel(column(1))) with linespoints title "limit"
 EOT
 
     # Run the Gnuplot script
@@ -310,11 +153,10 @@ EOT
     rm gnuplot_script.gp
 }
 
-
 buildHTMLReport() {
 
-    chartTitle1="$1"
-    chartFileName1="$2"   # Get the chart file name as the first parameter
+    chartTitle1="$1" # i.e Get the chart file name as the first parameter
+    chartFileName1="$2"
 
     chartTitle2="$3"
     chartFileName2="$4"
@@ -325,7 +167,7 @@ buildHTMLReport() {
     chartTitle4="$7"
     chartFileName4="$8"
 
-    reportTitle="$9"     # Get the report title as the second parameter
+    reportTitle="$9"     # i.e Get the report title as the ninth parameter
 
 cat <<EOT > "index.html"
 <!DOCTYPE html>
@@ -335,6 +177,11 @@ cat <<EOT > "index.html"
 </head>
 <body>
     <h1 style="text-align: center;">$reportTitle</h1>
+    <h4>Last updated for performance job build no. $latestRecordedBuild on $formattedTimestampOfLatestRecordedBuild</h4>
+    <p>The performance tests job runs daily at 02:15 UTC, providing performance metrics. The following graphs update at 04:15 UTC.</p>
+    <p>Successful performance tests job build adds new data, but even if a build fails, existing data is retained.</p>
+    <p>Updates occur whenever new successful data is available.</p>
+
     <table align="center">
         <tr> <!-- First Row -->
             <td align="center">
@@ -374,6 +221,7 @@ buildPageReport() {
     </head>
     <body>
         <h1>$reportTitle</h1>
+        <h4>Last updated for performance job build no. $latestRecordedBuild on $formattedTimestampOfLatestRecordedBuild</h4>
         <img src="$chartFileName" alt="Graph Image">
     </body>
     </html>
@@ -414,7 +262,7 @@ cd $WORKSPACE
 getLatestBuild
 if [ -z "$(ls -A)" ]; then
        # Calculate the starting value for the loop
-        startValue=$((latestBuild - 50))
+        startValue=$((latestBuild - 100))
 
         # Start the loop from startValue up to latestBuild
         for ((i=startValue; i<=latestBuild; i++)); do