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

[Bug]: Performance drop drastically while profiling a Ballerina program #41300

Closed
Nadeeshan96 opened this issue Aug 28, 2023 · 1 comment · Fixed by #41538 or #41554
Closed

[Bug]: Performance drop drastically while profiling a Ballerina program #41300

Nadeeshan96 opened this issue Aug 28, 2023 · 1 comment · Fixed by #41538 or #41554
Assignees
Labels
Area/Profiler Issues related to Ballerina Profiler Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug

Comments

@Nadeeshan96
Copy link
Contributor

Description

$title

Steps to Reproduce

  1. Profile the below program using bal profile.
import ballerina/random;
import ballerina/io;

public function main() returns error? {
    int[] arr = check createRandomIntArray(check float:pow(10,3).cloneWithType(int));
    int[] sortedArr = bubbleSort(arr);
    boolean isSorted = isSortedArray(sortedArr);
    io:println("Is the array sorted? " + isSorted.toString());
}

public isolated function bubbleSort(int[] arr) returns int[] {
    int n = arr.length();
    int temp = 0;
    boolean swapped = false;
    foreach int i in 0...n-2 {
        foreach int j in 1...n-1-i {
            if (arr[j - 1] > arr[j]) {
                temp = arr[j - 1];
                arr[j - 1] = arr[j];
                arr[j] = temp;
                swapped = true;
            }
        }
        if (!swapped) {
            break;
        }
    }
    return arr;
}

isolated function isSortedArray(int[] sortedArr) returns boolean {
    foreach int i in 0..<sortedArr.length() - 1 {
        if (sortedArr[i] > sortedArr[i + 1]) {
            return false;
        }
    }
    return true;
}

isolated function createRandomIntArray(int size) returns int[]|error {
    int[] array = [];
    foreach int i in 0..<size {
        array.push(check random:createIntInRange(0, int:MAX_VALUE));
    }
    return array;
}

To run the program it takes only 1 s.
Profiling it takes > 300 s.

  1. Ballerina service
import ballerina/http;

type Country record {
    string country;
    int population;
    string continent;
    int cases;
    int deaths;
};

type Data record {|
    string country;
    string continent;
    int population;
    decimal caseFatalityRatio;
|};

http:Client diseaseEp = check new ("https://disease.sh/v3");
final Country[] & readonly countries;

function init() returns error? {
    countries = check diseaseEp->/covid\-19/countries;
}

service /covid19/countries on new http:Listener(8080) {
    resource function get summary() returns json {
        Data[] listResult = from var {country, continent, population, cases, deaths} in countries
            where hasSignificantPopulation(population, deaths)
            let decimal caseFatalityRatio = <decimal>deaths / <decimal>cases * 100
            order by caseFatalityRatio descending
            limit 1000
            select {country, continent, population, caseFatalityRatio};
        return getTopSortedValues(listResult);
    }

    isolated resource function get names() returns json {
        return from var {country, population, deaths} in countries
            where hasNonZeroPopulation(population, deaths)
            select {country};
    }
}

isolated function getTopSortedValues(Data[] listResult) returns json[] {
    json[] sortedArray = listResult.sort("ascending", getKey);
    return getTopElements(sortedArray);
}

isolated function getTopElements(json[] sortedArray) returns json[] {
    return from var i in sortedArray
        limit 10
        select i;
}

isolated function hasSignificantPopulation(int population, int deaths) returns boolean {
    return population >= 100 && deaths >= 10;
}

isolated function hasNonZeroPopulation(int population, int deaths) returns boolean {
    return population >= 0 && deaths >= 0;
}

isolated function getKey(record {|string country; string continent; int population; decimal caseFatalityRatio;|} recordVal) returns string {
    return recordVal.country;
}

Normally the throughput is > 400 requests/second for both services. While profiling the service, it drops to around 18 requests/minute. We can use the below jmx file with jmeter to test this.
http-get-request.jmx.zip

Affected Version(s)

2201.8.0-snapshot

OS, DB, other environment details and versions

No response

Related area

-> Other Area

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

Copy link

github-actions bot commented Dec 5, 2023

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Profiler Issues related to Ballerina Profiler Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
Archived in project
2 participants