-latest_build=$(curl -s "${JENKINS_JOB_URL}/lastBuild/api/json" | jq -r '.number')
-echo "latest build is $latest_build"
-
-# Get the latest build status
-latest_build_status=$(curl -s "${JENKINS_JOB_URL}/api/json" | jq -r '.color')
-echo "latest build status is $latest_build_status"
-
-# Construct the URL for getting the console text
-CONSOLE_URL="${JENKINS_JOB_URL}/${latest_build}/consoleText"
-
-# Get the console text
-console_text=$(curl -s "$CONSOLE_URL")
-
-echo "-----------------------------------------------------------------------------------"
-echo "-----------------------------------------------------------------------------------"
-
-# 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=""
-
-# Extract numerical values as data results
-for pattern in "${all_patterns[@]}"; do
- if matched_line=$(echo "$console_text" | grep -o -P "$pattern"); then
- echo "Matched line: $matched_line"
- limit_value=$(echo "$matched_line" | grep -o -P 'limit\s*\K\d+(,\d+)?' | tr -cd '[:digit:]')
- took_value=$(echo "$matched_line" | grep -o -P 'took\s*\K\d+(,\d+)?' | tr -cd '[:digit:]')
- 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
+
+latestBuildToRecord=""
+consoleText=""
+latestRecordedBuild=""
+timestampOfLatestRecordedBuild=""
+
+# Get latest-completed build number from the jenkins job
+# The number has not been plotted on the graphs yet
+getLastCompletedBuildNumber() {
+ curl -s "${JENKINS_JOB_URL}/lastCompletedBuild/buildNumber"
+}
+
+# Get the last build number from local workspace
+# The number has already been plotted on the graphs
+getLastRecordedBuildNumber() {
+ cd "$WORKSPACE"
+ local file_name="Delete root node.txt"
+
+ # Check if the file exists
+ if [ -f "$file_name" ]; then
+ # Get the last line from the file
+ local last_line=$(tail -n 1 "$file_name")
+ local left_side=$(echo "$last_line" | cut -d ',' -f 1)
+ echo "$left_side"
+ else
+ echo "0"
+ fi
+}
+
+# Get all builds numbers
+getAllBuildNumbers() {
+ curl -s "${JENKINS_JOB_URL}/api/json?tree=allBuilds\[id\]" | jq -r '.allBuilds[].id' | sort -n
+}
+
+# Get the console text from specific build of the performance job
+getConsoleText() {
+ buildToRead=$1
+ consoleURL="${JENKINS_JOB_URL}/${buildToRead}/consoleText"
+ consoleText=$(curl -s "$consoleURL")
+}
+
+# Get and record the percentage (performance job result) for each test name for a build number
+getAndRecordPerformanceJobResultForBuild() {
+ buildNumber="$1"
+ getConsoleText "$buildNumber"
+ # Loop through each text name
+ for cps_stable_test_name in "${cps_stable_test_names[@]}"; do
+ getAndRecordDataResults "$consoleText" "$cps_stable_test_name" "$cps_stable_test_name.txt" "$buildNumber"
+ done
+ for ncmp_stable_test_name in "${ncmp_stable_test_names[@]}"; do
+ getAndRecordDataResults "$consoleText" "$ncmp_stable_test_name" "$ncmp_stable_test_name.txt" "$buildNumber"
+ done
+ for cps_unstable_test_name in "${cps_unstable_test_names[@]}"; do
+ getAndRecordDataResults "$consoleText" "$cps_unstable_test_name" "$cps_unstable_test_name.txt" "$buildNumber"
+ done
+ for ncmp_unstable_test_name in "${ncmp_unstable_test_names[@]}"; do
+ getAndRecordDataResults "$consoleText" "$ncmp_unstable_test_name" "$ncmp_unstable_test_name.txt" "$buildNumber"
+ done
+}
+
+# Calculate the percentage value for a specific test and append into test data file with build number
+getAndRecordDataResults() {
+ consoleText=$1
+ patternToMatch=$2
+ dataFile=$3
+ buildNumber=$4
+ new_data=""
+ matched_line=""
+ limit_value=""
+ took_value=""
+
+ # Get and calculate percentage for the graph
+ if matched_line=$(echo "$consoleText" | grep "$patternToMatch"); then
+ limit_value=$(echo "$matched_line" | grep -o -P 'limit\s*\K\d+(\.\d+)?' | tr -cd '[:digit:].')
+ took_value=$(echo "$matched_line" | grep -o -P 'took\s*\K\d+(\.\d+)?' | tr -cd '[:digit:].')
+ percentage=$(echo "scale=2; $took_value * 100.00 / $limit_value" | bc)
+ new_data="$percentage"
+ fi
+
+ # Record result into related test data file
+ touch "$dataFile"
+ lastLine=$(tail -n 1 "$dataFile")
+ newLine="$buildNumber,$new_data"
+ if [ -z "$new_data" ]; then
+ # No data found for this build probably the build failed
+ echo "$buildNumber,0" >>"$dataFile"
+ recordLatestRecordedBuild "$buildNumber"
+ elif [ "$newLine" == "$lastLine" ]; then
+ # Data already exists
+ recordLatestRecordedBuild "$buildNumber"
+ else
+ # New data added into the file
+ echo "$buildNumber,$new_data" >>"$dataFile"
+ recordLatestRecordedBuild "$buildNumber"
+ fi
+}
+
+# Save the latest recorded build number with date and time
+recordLatestRecordedBuild() {
+ latestBuildToRecord="$1"
+ timestampOfLatestRecordedBuild=$(curl -s "${JENKINS_JOB_URL}/${latestBuildToRecord}/api/json?tree=timestamp" | jq -r '.timestamp')
+ formattedTimestampOfLatestRecordedBuild=$(date -d "@$((timestampOfLatestRecordedBuild / 1000))" "+%B %e, %Y at %H:%M")
+ latestRecordedBuild=$latestBuildToRecord
+}
+
+# Plot the image (graph) in png format
+buildPlotImage() {
+ dataFile="$1" # Get the input file name from the function parameter
+ chartFileName="$2"
+
+ # Create a temporary Gnuplot script
+ cat <<EOT >gnuplot_script.gp
+set datafile separator ","
+set terminal pngcairo size 1500,600
+set output "${chartFileName}"
+set xlabel "Build"
+set ylabel "Percentage of limit %"
+set yrange [0 < * < 80 : 120 < *]
+set xtics rotate
+plot '$dataFile' using (column(0)):2:xtic(sprintf("%d", column(1))) with linespoints title "measured", \
+ 100 with lines linestyle 2 title "100% limit"
+EOT
+
+ # Run the temporary Gnuplot script
+ gnuplot gnuplot_script.gp
+
+ # Remove the temporary Gnuplot script
+ rm gnuplot_script.gp
+}
+
+# Builds category html file
+buildCategoryHtmlReport() {
+ # use indirect expansion to get all elements of the array
+ categoryName=("${!1}")
+ reportTitle="$2"
+ outputFile="$3"
+ cat <<EOT >"$outputFile"
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>$reportTitle</title>
+ </head>
+ <body>
+ <h1 style="text-align: center;">$reportTitle</h1>
+ <h4>Last updated for performance job build no. $latestRecordedBuild on $formattedTimestampOfLatestRecordedBuild</h4>
+ <table align="center">
+EOT
+# Loop through the test names to generate HTML rows
+for test_name_in_category in "${categoryName[@]}"; do
+ cat <<EOF >>"$outputFile"
+ <tr> <!-- Row for $test_name_in_category -->
+ <td align="center" style="padding: 10px;">
+ <figcaption>"$test_name_in_category"</figcaption>
+ <img src="$test_name_in_category.png" width="750" height="300">
+ </td>
+ </tr>
+EOF