How to check CPU usage

bonsai
Mega Sage

※Paris version

 

I'm running the following script on a job schedule.

var grInc = new GlideRecord('incident');
var schedule_count = 0;

grInc.query();
while (grInc.next()) {
    var sched = new ScheduleOnce();
    sched.script = '(' + scripte + ')();';
    sched.label = "test_schedule";
    sched.schedule1();
    schedule_count += 1;


    if (schedule_count >= 4) {
        gs.sleep(3000);//Wait for the record to be reflected in the "sys_trigger" table
        var grTri_count = rec_count();
        gs.info("schedule count:" + grTri_count);
        while (grTri_count == 4) {
            gs.sleep(1000);//Check the "sys_trigger" table every second
            grTri_count = rec_count();
        }

        schedule_count =grTri_count;
    }

}

//Returns the number of running "test_schedule" schedules that exist in the "sys_trigger" table
function rec_count() {
    var grTri = new GlideRecord('sys_trigger');
    grTri.addQuery("name", "test_schedule");
    grTri.addQuery("state", 1); //"1" is the running status
    grTri.query();
    return grTri.getRowCount();
}

//Wait 1 minute and send a message
function scripte(s) {

gs.sleep(60000);
gs.info("Test!!!");

}

 

Since I check the contents of the table every second in the following part of the script
I suspected that CPU usage was rising.

   if (schedule_count >= 4) {
        gs.sleep(3000);//Wait for the record to be reflected in the "sys_trigger" table
        var grTri_count = rec_count();
        gs.info("schedule count:" + grTri_count);
        while (grTri_count == 4) {
            gs.sleep(1000);//Check the "sys_trigger" table every second
            grTri_count = rec_count();
        }

        schedule_count =grTri_count;
    }

 

Does the meaning of "1.14" displayed in "System Overview" on the "Service Now Performance" screen mean CPU usage?
Since the value of MAX is "1.5", should "1.5" be considered 100%?

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, regardless of the method used, I would personally avoid any scripted function that blocks or delays running of a script; Also continually querying system tables like sys_trigger could result in degraded platform performance.

Can you clarify the business drivers\requirements behind this functionality?
What are you trying to achieve\deliver?

 

I have implemented concurrent execution of scheduled scripts.
I want to adjust the number of concurrent executions, so I am checking the running schedule of the "sys_trigger" table.

Hi, your instance will pick up and process sys_triggers, assigning these to a node\node worker via underpinning SNC functionality, and as your trigger may be queued to any Node Worker it may be processed after other records/triggers, as a result I don't believe that creation of sys_trigger sconcurrently would have any impact on sequential processing of the resulting triggers.

If the intention is to load test the environment somehow? then I think that the nature of your code effectively negates any results it provides, as it would directly impacting normal operational process.

If you spent some time explaining your intentions clearly, IE why you believe you need concurrent processing and how this will impact your objectives the forum could be in a better position to review and advise.

thank you for your answer.
My true purpose is to run the scripts at the same time.
Simultaneous execution is performed to perform processing within a limited time.

In addition, it became necessary to adjust the number of concurrent executions due to the number of concurrent executions of transactions.
Finally, the following script seems to solve it.

 

var grInc = new GlideRecord('incident');
var Concurrency = 4;
grInc.query();
var schedule_count = 0;
var Done_record = "";
while (grInc.next()) {
    var sched = new ScheduleOnce();
    Done_record = Done_record + "," + grInc.getUniqueValue().toString() + ",";
    sched.script = '(' + scripte + ')("' + grInc.getUniqueValue().toString() + '","' + Done_record + '","' + Concurrency + '");';
    sched.label = "test_schedule_" + grInc.getUniqueValue();
    sched.schedule1();
    schedule_count += 1;
    if (schedule_count == 5) {
        break;
    }

}

function scripte(grinc_sysid, done_sysid, Concurrency) {
    try {

        function schedule_Recursive(end_flg) {
            var grInc = new GlideRecord('incident');
            grInc.query();
            while (grInc.next()) {
                var check_done_sysid = done_sysid.indexOf(grInc.getUniqueValue().toString());
                if (check_done_sysid == "-1") {
                    gs.sleep(5000);
                    var get_rec_name_result = get_rec_name();
                    var check_result_rec_name = get_rec_name_result.indexOf(grInc.getUniqueValue().toString());
                    if (check_result_rec_name == "-1") {

                        done_sysid = done_sysid + "," + grinc_sysid;

                        var Done_record = done_sysid + "," + grInc.getUniqueValue().toString() + "," + get_rec_name_result;

                        Done_record = Done_record.split('test_schedule_').join(",");
                        Done_record = Done_record.split('test_schedule').join(",");
                        Done_record = Done_record.split('testschedule').join(",");
                        Done_record = Done_record.split('_').join(",");
                        var Done_record_split = Done_record.split(',');
                        var Done_record_filter = Done_record_split.filter(function(x, i, self) {
                            return self.indexOf(x) === i;
                        });
                        Done_record = Done_record_filter.join(",").toString();

                        var sched = new ScheduleOnce();
                        sched.script = '(' + scripte + ')("' + grInc.getUniqueValue().toString() + '","' + Done_record + '");';
						
                        sched.label = "test_schedule_" + Done_record;
                        sched.schedule1();
                        break;
                    }
                }
            }
        }
        gs.info("start" + grinc_sysid);

        gs.sleep(60000);

        schedule_Recursive(true);

        gs.info("end" + grinc_sysid);
    } catch (e) {

        gs.info("error" + e);
    }

    function get_rec_name() {
        var grTri = new GlideRecord('sys_trigger');
        grTri.addQuery("name", "STARTSWITH", "test_schedule");
        grTri.addQuery("state", 1); //"1" is the running status
        grTri.query();
        var grTri_name = "";

        while (grTri.next()) {
            grTri_name = grTri_name + grTri.getDisplayValue("name").toString();
        }
        grTri_name = grTri_name.split('test_schedule_').join(",");
        grTri_name = grTri_name.split('test_schedule').join(",");
        grTri_name = grTri_name.split('testschedule').join(",");
        grTri_name = grTri_name.split('_').join(",");
        var grTri_split = grTri_name.split(',');
        var grTri_split2 = grTri_split.filter(function(x, i, self) {
            return self.indexOf(x) === i;
        });

        return grTri_split2.join(",").toString();

    }

}