Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add performance tests to ci-cd-yt01 workflow #1595

Merged
merged 30 commits into from
Dec 12, 2024

Conversation

dagfinno
Copy link
Contributor

@dagfinno dagfinno commented Dec 12, 2024

Add performance tests and run these tests in ci-cd-yt01 workflow

Description

Related Issue(s)

#1540

  • #{issue number}

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

@dagfinno dagfinno requested review from a team as code owners December 12, 2024 10:07
Copy link
Contributor

coderabbitai bot commented Dec 12, 2024

📝 Walkthrough

Walkthrough

The changes introduce a new job named run-performance in the CI/CD workflow for executing K6 performance tests. This job is configured to run conditionally and supports parallel execution with a matrix for multiple test files. Additionally, two new optional input parameters are added to the K6 performance test workflow, enhancing token generation capabilities through modifications in the generate_tokens.sh script. New performance testing scripts are also introduced for end-user and service owner functionalities, defining various metrics and thresholds.

Changes

File Path Change Summary
.github/workflows/ci-cd-yt01.yml Added new job run-performance for K6 performance tests with conditional execution and parameters.
.github/workflows/workflow-run-k6-performance.yml Added optional input parameters numberOfTokens and ttl for token generation.
tests/k6/tests/enduser/enduserSearchWithThresholds.js Introduced new performance test for end-user search with defined metrics and thresholds.
tests/k6/tests/scripts/generate_tokens.sh Updated script to include <limit> and <ttl> parameters, enhancing token generation logic.
tests/k6/tests/serviceowner/createDialogWithThresholds.js Added performance test for creating dialogs with defined metrics and thresholds.
tests/k6/tests/serviceowner/serviceOwnerSearchWithThresholds.js Introduced performance test for service owner search with defined metrics and thresholds.

Possibly related PRs

Suggested reviewers

  • arealmaas
  • oskogstad

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (8)
tests/k6/tests/serviceowner/createDialogWithThresholds.js (2)

5-6: Consider increasing virtual users for stress testing

A single virtual user (VU) might not effectively stress test the dialog creation endpoint. Consider implementing a ramping pattern or higher VU count to better simulate real-world load.

-    vus: 1,
+    stages: [
+        { duration: '10s', target: 1 },  // Warm-up
+        { duration: '10s', target: 5 },  // Ramp-up load
+        { duration: '10s', target: 5 },  // Stay at peak
+    ],

1-1: Consider using absolute imports for better maintainability

Using relative imports can be fragile if file structure changes. Consider using absolute imports from a base path.

-import { default as run } from "./performance/create-dialog.js";
+import { default as run } from "@tests/k6/tests/serviceowner/performance/create-dialog.js";
tests/k6/tests/enduser/enduserSearchWithThresholds.js (1)

3-25: Consider extracting common configuration to a shared module

The options configuration is largely similar across all three test files. Consider creating a shared configuration module to:

  1. Standardize common settings (summaryTrendStats, duration, VUs)
  2. Provide factory functions for threshold generation
  3. Ensure consistent performance criteria across similar endpoints

Example shared configuration:

// tests/k6/config/common.js
export const baseOptions = {
    summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(95)', 'p(99)', 'p(99.5)', 'p(99.9)', 'count'],
    duration: "30s"
};

export const createThresholds = (endpoints, { p95 = 300, p99 = 500 } = {}) => {
    return endpoints.reduce((acc, endpoint) => ({
        ...acc,
        [`http_req_duration{name:${endpoint}}`]: [`p(95)<${p95}`, `p(99)<${p99}`],
        [`http_reqs{name:${endpoint}}`]: []
    }), {});
};
.github/workflows/workflow-run-k6-performance.yml (1)

56-59: Consider adding error handling for token generation

The script execution should be checked for errors before proceeding with the K6 test.

-        ./tests/k6/tests/scripts/generate_tokens.sh ./tests/k6/tests/performancetest_data ${{ inputs.tokens }} ${{ inputs.numberOfTokens }} ${{ inputs.ttl }}
+        if ! ./tests/k6/tests/scripts/generate_tokens.sh ./tests/k6/tests/performancetest_data ${{ inputs.tokens }} ${{ inputs.numberOfTokens }} ${{ inputs.ttl }}; then
+          echo "Token generation failed"
+          exit 1
+        fi
tests/k6/tests/scripts/generate_tokens.sh (2)

63-68: Verify token limit implementation consistency

The token limit implementation is duplicated in both enterprise and personal token generation loops. Consider extracting this logic into a function to ensure consistency and maintainability.

+check_token_limit() {
+    local generated=$1
+    local limit=$2
+    if [ $limit -gt 0 ] && [ $generated -ge $limit ]; then
+        return 1
+    fi
+    return 0
+}

-        if [ $limit -gt 0 ] && [ $generated -ge $limit ]; then
-            break
-        fi
+        if ! check_token_limit "$generated" "$limit"; then
+            break
+        fi

Also applies to: 91-96


69-69: Consider adding retry mechanism for token generation

The token generation URLs are called without retry logic. Network issues could cause intermittent failures.

+retry_curl() {
+    local url=$1
+    local auth=$2
+    local max_attempts=3
+    local attempt=1
+    
+    while [ $attempt -le $max_attempts ]; do
+        response=$(curl -s -f "$url" -u "$auth")
+        if [ $? -eq 0 ]; then
+            echo "$response"
+            return 0
+        fi
+        echo "Attempt $attempt failed. Retrying..." >&2
+        attempt=$((attempt + 1))
+        sleep 1
+    done
+    return 1
+}

-        token=$(curl -s -f $url -u "$tokengenuser:$tokengenpasswd" )
+        token=$(retry_curl "$url" "$tokengenuser:$tokengenpasswd")

Also applies to: 97-97

.github/workflows/ci-cd-yt01.yml (2)

156-162: Consider parameterizing the test matrix

The test files are hardcoded in the matrix. Consider moving them to a configuration file for better maintainability.

+    # Create a new file: .github/config/performance-tests.json
+    {
+      "tests": [
+        "tests/k6/tests/serviceowner/serviceOwnerSearchWithThresholds.js",
+        "tests/k6/tests/serviceowner/createDialogWithThresholds.js",
+        "tests/k6/tests/enduser/enduserSearchWithThresholds.js"
+      ]
+    }

-        files: 
-          - tests/k6/tests/serviceowner/serviceOwnerSearchWithThresholds.js
-          - tests/k6/tests/serviceowner/createDialogWithThresholds.js
-          - tests/k6/tests/enduser/enduserSearchWithThresholds.js
+        files: ${{ fromJson(readFile('.github/config/performance-tests.json')).tests }}
🧰 Tools
🪛 yamllint (1.35.1)

[error] 159-159: trailing spaces

(trailing-spaces)


148-148: Remove commented-out code

Remove the commented-out needs line as it's no longer needed.

-    #needs: [deploy-apps, check-for-changes]
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bb4433b and d07f76c.

📒 Files selected for processing (6)
  • .github/workflows/ci-cd-yt01.yml (1 hunks)
  • .github/workflows/workflow-run-k6-performance.yml (2 hunks)
  • tests/k6/tests/enduser/enduserSearchWithThresholds.js (1 hunks)
  • tests/k6/tests/scripts/generate_tokens.sh (6 hunks)
  • tests/k6/tests/serviceowner/createDialogWithThresholds.js (1 hunks)
  • tests/k6/tests/serviceowner/serviceOwnerSearchWithThresholds.js (1 hunks)
🧰 Additional context used
📓 Learnings (1)
tests/k6/tests/enduser/enduserSearchWithThresholds.js (1)
Learnt from: dagfinno
PR: digdir/dialogporten#1373
File: tests/k6/tests/enduser/performance/simple-search.js:11-20
Timestamp: 2024-11-12T05:32:45.311Z
Learning: In `tests/k6/tests/performancetest_common/common.js`, the function `getDefaultThresholds` may intentionally create empty threshold arrays to make labels visible in k6 reports.
🪛 yamllint (1.35.1)
.github/workflows/workflow-run-k6-performance.yml

[error] 27-27: trailing spaces

(trailing-spaces)


[error] 31-31: trailing spaces

(trailing-spaces)

.github/workflows/ci-cd-yt01.yml

[error] 155-155: trailing spaces

(trailing-spaces)


[error] 159-159: trailing spaces

(trailing-spaces)

🔇 Additional comments (7)
tests/k6/tests/serviceowner/serviceOwnerSearchWithThresholds.js (2)

5-6: Consider increasing virtual users for stress testing

Similar to createDialogWithThresholds.js, a single virtual user might not effectively stress test these endpoints.


8-13: Consider aligning performance thresholds across similar endpoints

The thresholds (p95<100ms, p99<300ms) are more stringent than those in createDialogWithThresholds.js (p95<300ms, p99<500ms). Consider standardizing these thresholds unless there's a specific reason for the difference.

tests/k6/tests/enduser/enduserSearchWithThresholds.js (1)

5-6: Consider increasing virtual users for stress testing

Similar to other test files, a single virtual user might not effectively stress test these endpoints.

.github/workflows/workflow-run-k6-performance.yml (1)

24-31: LGTM: New input parameters are well-defined

The new input parameters numberOfTokens and ttl are properly configured with appropriate defaults and types.

🧰 Tools
🪛 yamllint (1.35.1)

[error] 27-27: trailing spaces

(trailing-spaces)


[error] 31-31: trailing spaces

(trailing-spaces)

tests/k6/tests/scripts/generate_tokens.sh (1)

14-16: LGTM: Usage information updated correctly

The usage information clearly describes the new parameters and includes a helpful example.

.github/workflows/ci-cd-yt01.yml (2)

146-147: LGTM: Proper job dependencies and conditions

The job correctly depends on both infrastructure and application deployments, with appropriate conditions for execution.


166-168: Verify performance test parameters

The initial values for VUs (1) and duration (30s) seem conservative. Ensure these values are sufficient for meaningful performance testing.

✅ Verification successful

Performance test parameters align with existing test configurations

The current VU (1) and duration (30s) values in the CI/CD workflow match the configurations used across multiple performance test files in the codebase. These tests have well-defined thresholds:

  • HTTP request duration p95 < 100-300ms
  • HTTP request duration p99 < 300-500ms
  • Failed requests rate < 1%

The parameters are consistent with the established performance testing strategy in the codebase and appear sufficient for meaningful testing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the test files contain any specific thresholds or requirements

echo "Checking performance test thresholds..."
for test_file in $(fd -e js . tests/k6/tests/ | grep "Thresholds.js$"); do
  echo "=== $test_file ==="
  rg "threshold|target|vus|duration" "$test_file" -A 2
done

Length of output: 3301

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (5)
tests/k6/tests/enduser/performance/enduser-search.js (2)

11-18: LGTM! Consider documenting threshold naming conventions.

The addition of the 'enduser' prefix to threshold names improves metric categorization and namespace isolation. The naming convention is consistent across all operations.

Consider adding a comment explaining the threshold naming convention and its relationship to the K6 reporting structure.

    thresholds: getDefaultThresholds(['http_req_duration', 'http_reqs'],[
+       // Threshold names are prefixed with 'enduser' to categorize metrics in K6 reports
        'enduser get dialog', 
        'enduser get dialog activities',

Line range hint 4-6: Consider enhancing environment variable validation.

While the current boolean parsing is functional, consider adding validation for unexpected values and providing meaningful error messages.

Here's a suggested enhancement:

-const isSingleUserMode = (__ENV.isSingleUserMode ?? 'false') === 'true';
-const traceCalls = (__ENV.traceCalls ?? 'false') === 'true';
+function parseBoolean(value, defaultValue, name) {
+    if (value === undefined) return defaultValue;
+    if (value !== 'true' && value !== 'false') {
+        throw new Error(`Invalid value for ${name}. Expected 'true' or 'false', got '${value}'`);
+    }
+    return value === 'true';
+}
+
+const isSingleUserMode = parseBoolean(__ENV.isSingleUserMode, false, 'isSingleUserMode');
+const traceCalls = parseBoolean(__ENV.traceCalls, false, 'traceCalls');
.github/workflows/ci-cd-yt01.yml (3)

147-148: Remove commented code.

The commented line #needs: [deploy-apps, check-for-changes] should be removed as it appears to be a remnant of previous configuration.


156-163: Clean up trailing spaces.

Remove trailing spaces from the following lines:

  • Line 159: files:
  • Line 163: fail-fast: false
🧰 Tools
🪛 yamllint (1.35.1)

[error] 159-159: trailing spaces

(trailing-spaces)


[error] 163-163: trailing spaces

(trailing-spaces)


156-162: Consider test execution strategy.

The current configuration runs tests sequentially (max-parallel: 1) which is good for accurate performance measurements. However, consider:

  1. Adding comments to explain why sequential execution is necessary
  2. Documenting the expected duration for the complete test suite
  3. Considering test prioritization if the suite grows larger
🧰 Tools
🪛 yamllint (1.35.1)

[error] 159-159: trailing spaces

(trailing-spaces)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d07f76c and 2176ea3.

📒 Files selected for processing (3)
  • .github/workflows/ci-cd-yt01.yml (1 hunks)
  • tests/k6/tests/enduser/enduserSearchWithThresholds.js (1 hunks)
  • tests/k6/tests/enduser/performance/enduser-search.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/k6/tests/enduser/enduserSearchWithThresholds.js
🧰 Additional context used
📓 Learnings (1)
tests/k6/tests/enduser/performance/enduser-search.js (1)
Learnt from: dagfinno
PR: digdir/dialogporten#1373
File: tests/k6/tests/enduser/performance/simple-search.js:11-20
Timestamp: 2024-11-12T05:32:45.311Z
Learning: In `tests/k6/tests/performancetest_common/common.js`, the function `getDefaultThresholds` may intentionally create empty threshold arrays to make labels visible in k6 reports.
🪛 yamllint (1.35.1)
.github/workflows/ci-cd-yt01.yml

[error] 155-155: trailing spaces

(trailing-spaces)


[error] 159-159: trailing spaces

(trailing-spaces)


[error] 163-163: trailing spaces

(trailing-spaces)

🔇 Additional comments (3)
tests/k6/tests/enduser/performance/enduser-search.js (1)

Line range hint 4-6: Verify CI/CD workflow integration.

Ensure that the environment variables and test data setup are properly configured in the ci-cd-yt01 workflow.

Also applies to: 22-33

✅ Verification successful

Environment variables properly configured in CI/CD workflow

The environment variables isSingleUserMode and traceCalls are correctly integrated in the test script and will be passed through the k6 test execution environment. The workflow configuration in ci-cd-yt01.yml properly sets up the k6 performance test execution with necessary secrets and configurations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check workflow configuration for environment variables and test data setup

# Check for workflow file and environment variable configuration
echo "Checking workflow configuration..."
rg -A 5 "isSingleUserMode|traceCalls" .github/workflows/

# Check for test data setup in workflow
echo "Checking test data setup..."
rg -A 10 "endUsersWithTokens" .github/workflows/

Length of output: 251


Script:

#!/bin/bash
# Let's check for workflow files that might be using k6 or performance tests
echo "Checking workflow files for k6 or performance test configurations..."
fd -e yml -e yaml . .github/workflows/ --exec cat {}

# Also check for any k6 related configuration files
echo -e "\nChecking for k6 configuration files..."
fd k6 . --exec cat {}

Length of output: 69490


Script:

#!/bin/bash
# Let's check the workflow that runs k6 performance tests
echo "Checking k6 performance test workflow configuration..."
rg -A 10 "run-performance" .github/workflows/ci-cd-yt01.yml

# Also check the workflow that actually runs the k6 tests
echo -e "\nChecking k6 test execution workflow..."
rg -A 10 "K6_CLOUD_TOKEN|isSingleUserMode|traceCalls" .github/workflows/workflow-run-k6-performance.yml

Length of output: 1531


Script:

#!/bin/bash
# Let's check the actual k6 test execution script
echo "Checking k6 test execution script configuration..."
cat tests/k6/tests/enduser/performance/enduser-search.js

# Also check if there are any environment variable configurations in the test setup
echo -e "\nChecking k6 test setup files..."
fd -e js config tests/k6 --exec cat {}

Length of output: 4732

.github/workflows/ci-cd-yt01.yml (2)

153-154: Verify K6 Cloud configuration.

Ensure that the K6 Cloud token and project ID are properly configured in the repository secrets for the YT01 environment.


143-175: Verify workflow integration.

The performance test job looks well-configured, but let's verify:

  1. Integration with the failure notification job
  2. Proper triggering conditions
  3. Workflow file syntax
✅ Verification successful

Let me gather more information about the workflow integration, particularly focusing on the failure notification setup and job dependencies.


Let me check one final aspect regarding the workflow integration and failure handling.


Performance test job is properly integrated and configured

The verification confirms that:

  1. The job has correct failure handling with if: ${{ always() && !failure() && !cancelled() }} condition
  2. It's properly integrated into the workflow's dependency chain with needs: [deploy-apps, deploy-infra, check-for-changes]
  3. The job's results are included in the Slack failure notification system
  4. The reusable workflow configuration is complete with all required inputs and secrets
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify workflow integration

# Check if the job is properly referenced in the failure notification job
echo "Checking failure notification job dependencies..."
rg "needs.*run-performance" .github/workflows/ci-cd-yt01.yml

# Verify workflow syntax
echo "Validating workflow syntax..."
gh workflow view "ci-cd-yt01.yml"

# Check for any hardcoded environment references
echo "Checking for hardcoded environment references..."
rg -i "yt01" .github/workflows/ci-cd-yt01.yml

Length of output: 1506


Script:

#!/bin/bash
# Check for failure notification job and its dependencies
echo "Checking failure notification configuration..."
rg -A 10 "failure-notification:" .github/workflows/ci-cd-yt01.yml

# Check the reusable workflow for k6 performance tests
echo "Checking k6 performance workflow..."
cat .github/workflows/workflow-run-k6-performance.yml

# Verify if there are any other jobs depending on run-performance
echo "Checking dependencies on run-performance..."
rg "needs:.*run-performance" .github/workflows/

Length of output: 2554


Script:

#!/bin/bash
# Check for any if conditions or job dependencies that might affect the workflow
echo "Checking workflow conditions and dependencies..."
rg -B 2 -A 5 "if: .*failure\(\)" .github/workflows/ci-cd-yt01.yml

# Check for any post-job actions or notifications
echo "Checking post-job configurations..."
rg "post:" .github/workflows/ci-cd-yt01.yml -A 10

# Check the complete job definition to understand the failure handling
echo "Checking complete run-performance job..."
rg -B 5 -A 30 "run-performance:" .github/workflows/ci-cd-yt01.yml

Length of output: 4793

🧰 Tools
🪛 yamllint (1.35.1)

[error] 155-155: trailing spaces

(trailing-spaces)


[error] 159-159: trailing spaces

(trailing-spaces)


[error] 163-163: trailing spaces

(trailing-spaces)

.github/workflows/ci-cd-yt01.yml Show resolved Hide resolved
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
39.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@arealmaas arealmaas merged commit f6f33d8 into main Dec 12, 2024
21 of 22 checks passed
@arealmaas arealmaas deleted the performance/threshold-tests branch December 12, 2024 13:02
@coderabbitai coderabbitai bot mentioned this pull request Dec 12, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants