3 Replies · Latest reply on Dec 8, 2017 12:15 AM by sergiu.panaite

    is there any way to test the performance of my codes

      Say for example:
      Servicenow - in many cases gives best practices like: use glideaggregate instead of getRowCount() API. So in this case, can i test the performance difference by changing getRowCount() in some 10 scripts with glideaggregate?

        • Re: is there any way to test the performance of my codes
          sergiu.panaite

          GlideAggregate does a direct SQL "SELECT COUNT()" statement where GlideRecord does a "SELECT * FROM" and then an internal loop to count the number of records. If the right indexes are present, the SELECT COUNT()" operation should be much faster especially for large resultsets.

          Best Regards,
          Sergiu

          Visit my community blog:
          https://community.servicenow.com/people/sergiu.panaite/blog

          or personal blog:
          http://bitsofcode.io

          Have a performance issue? Challenge me!

            • Re: is there any way to test the performance of my codes
              Shawn Dowler

              If I have code that is already doing a GlideRecord query() for other reasons, is getRowCount() still slow? Or is the query() the slow part and the getRowCount() just returning a property of the GlideRecord object?

                • Re: is there any way to test the performance of my codes
                  sergiu.panaite

                  getRowCount() is pretty fast for small datasets, for large datasets it can become slow. It's easy to use for testing, but in production, if you GlideRecord needs to return lots of rows, might not be as fast as you expect. Call it only if really needed.

                  A GlideRecord call doesn't trigger a raw SQL count(*) (therefore the GlideAggregate) call so row count is calculated by looping once through the resultset.

                   

                  One way of testing this:

                  - as admin run the GlideRecord call in background scripts. Any SQL triggered by that call, if longer than 100ms, will be shown on the output screen.

                   

                  An example of a simple script:

                   

                  var gr = new GlideRecord('sys_metadata');
                  gs.print('Before doing the call: ' + gs.nowDateTime());
                  gr.query();
                  gs.print('After call is done: ' + gs.nowDateTime());
                  gs.print(gr.getRowCount());
                  gs.print('Getting the rowcount: ' + gs.nowDateTime());
                  

                   

                  I get:

                   

                  [0:00:00.796] Script completed in scope global: script
                  
                  

                  *** Script: Before doing the call: 12-08-2017 00:14:17
                  *** Script: After call is done: 12-08-2017 00:14:18
                  *** Script: 218245
                  *** Script: Getting the rowcount: 12-08-2017 00:14:18
                  

                   

                  So, total response time is almost 800ms, but the query itself is most probably that one that takes most of time. I don't really have a much larger dataset (a few millions) so this is not as conclusive as some might expect.

                  Best Regards,
                  Sergiu

                  Visit my community blog:
                  https://community.servicenow.com/people/sergiu.panaite/blog

                  or personal blog:
                  http://bitsofcode.io

                  Have a performance issue? Challenge me!