Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-82754

Performance improvement at listing courses using a String Builder to efficiently build the final string placeholders.

XMLWordPrintable

    • MOODLE_405_STABLE
    • MOODLE_500_STABLE
    • MDL-82754-main
    • Hide

      Pre-test

      1. The testing will use the Firefox browser.
      2. Generate 100 courses
        1. Download the CLI script maketestcourse82754.php and put the file to MOODELDIR/admin/tool/generator/cli/maketestcourse82754.php
        2. Open your terminal and set the current working directory to your Moodle directory
        3. Run the below command:

          php admin/tool/generator/cli/maketestcourse82754.php --shortname=s --size=S --amount=100

      Testing

      1. Do not apply the patch yet.
      2. Login as admin
      3. Navigate to My Courses page
      4. Scroll down the page and change the Show dropdown to max, 96.
      5. Open the Web developer tools
      6. Click the Performance tab
      7. Click Start recording
      8. Reload the page and wait until the page is finished to load.
      9. Click the Capture recording button
      10. VERIFY there is new tab opened
      11. At the tracks column (the first column), click "Localhost" if you are running your Moodle with localhost as host. if not, search the host you are using.
      12. Search "treatStringsInContent" in the Filter stack
      13. Click the "treatStringsInContent" in the Call tree tab window
      14. Note down the Traced running time.
      15. Apply the patch and repeat steps 2 - 14
      16. Compare the Traced running time before and after the patch
      17. VERIFY the Traced running time faster after the patch is applied.

      Please refer to the image below for a better understanding of steps 11 - 14.

       

       

      Show
      Pre-test The testing will use the Firefox browser. Generate 100 courses Download the CLI script maketestcourse82754.php and put the file to MOODELDIR/admin/tool/generator/cli/maketestcourse82754.php Open your terminal and set the current working directory to your Moodle directory Run the below command: php admin/tool/generator/cli/maketestcourse82754.php --shortname=s --size=S --amount= 100 Testing Do not apply the patch yet. Login as admin Navigate to My Courses page Scroll down the page and change the Show dropdown to max, 96. Open the Web developer tools Click the Performance tab Click Start recording Reload the page and wait until the page is finished to load. Click the Capture recording button VERIFY there is new tab opened At the tracks column (the first column), click "Localhost" if you are running your Moodle with localhost as host. if not, search the host you are using. Search "treatStringsInContent" in the Filter stack Click the "treatStringsInContent" in the Call tree tab window Note down the Traced running time. Apply the patch and repeat steps 2 - 14 Compare the Traced running time before and after the patch VERIFY the Traced running time faster after the patch is applied. Please refer to the image below for a better understanding of steps 11 - 14.    
    • Hide

      Code verified against automated checks.

      Checked MDL-82754 using repository: https://github.com/meirzamoodle/moodle.git

      More information about this report

      Built on: Mon 21 Oct 2024 06:19:55 AM UTC

      Show
      Code verified against automated checks. Checked MDL-82754 using repository: https://github.com/meirzamoodle/moodle.git main (0 errors / 0 warnings) [branch: MDL-82754-main | CI Job ] More information about this report Built on: Mon 21 Oct 2024 06:19:55 AM UTC
    • 6
    • Team Hedgehog 2024 Sprint 3.2, Team Hedgehog 2024 Sprint 3.3, Team Hedgehog 2024 Review 4, Team Hedgehog 2024 Sprint 4.1, Team Hedgehog 2024 Sprint 4.2, Team Hedgehog 2024 Sprint 4.3

        On the home page, whether on the dashboard, home, myhome or whatever is configured in your Moodle that lists all of a teacher's courses. If the number of these courses is very large and teachers prefer the list as "all", there is a major performance problem in the "treatStringsInContent()" method, which replaces placeholders in a string with their respective translations, that is, replacing placeholders found in a string.
        Previously, they used String.replace() to replace placeholders, but the complexity increased with the number of strings to be replaced. Now, they manually go through the string and replace placeholders as they find them.
        However, currently there is this major problem of multiple string concatenations, which are costly in performance.
        Therefore, I am proposing a solution using a String Builder through an array, which will reduce the amount of concatenation within the while loop, causing a significant increase in performance.
        In a local test with more than 200 courses listed, the performance difference was more than 50 seconds.

      Step by step to simulate the problem:

      • Log in to your Moodle with a teacher who has the maximum number of courses possible, preferably more than 100 courses that can be listed, and a page that lists all the courses of this teacher. After loading, if there is pagination, choose the "all" option as default, to list all the courses.
      • In dev tools, go to performance -> start recording -> refresh the page that lists the courses, and after loading all the courses with their images, stop recording. Then, analyze the performance in:
      • Call Tree -> timer triggered -> execute microtasks -> ProcessRenderedContent -> treatStringsInContent through this, it is possible to note the execution time of the treatStringsInContent method.* Step by step to simulate the solution:
          Afterwards, it is possible to change the code in this method and implement the proposed improvement, applying a String Builder with an array in the treatStringsInContent method. After that, replicate the testing process mentioned above and see that the time is much shorter and performance is better.- FILE:  renderer.js
      • METHOD: treatStringsInContent()
      • PATH:  /moodle/lib/amd/src/local/templates/renderer.js
      • New code with modification:  Attached in this improvement in: solution.js

      Note: The attached file only contains the code of the method to be improved, not the entire renderer.js file.
      Att, Jean Marcos Sotoriva.
      Rio Grande do Sul, Brasil.

        1. maketestcourse82754.php
          4 kB
          Meirza
        2. MDL-82754_performance_profiler.png
          109 kB
          Meirza
        3. MDL-82754.png
          782 kB
          Ron Carl Alfon Yu
        4. original.js
          2 kB
          JeanSotoriva
        5. solution.js
          2 kB
          JeanSotoriva

            meirza.arson@moodle.com Meirza
            jeansotoriva JeanSotoriva
            Stevani Andolo Stevani Andolo
            Jun Pataleta Jun Pataleta
            Ron Carl Alfon Yu Ron Carl Alfon Yu
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 days, 1 hour, 34 minutes
                2d 1h 34m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.