---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# OCTimer - Global

# OCTimer - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 5 minutes to read

The OCTimer script include provides methods to track the length of time it takes for a function to complete.
This script include requires the On-Call Scheduling (com.snc.on_call_rotation) plugin to be activated. For more information, refer to the topic [Activate On-Call Scheduling](https://www.servicenow.com/docs/access?context=t_ActivateOnCallScheduling&version=xanadu&pubname=xanadu-it-service-management&ft:locale=en-US).

## OCTimer - log (String name, String entry) {#ariaid-title2}

Logs the entry based on the function name.
{#r_OCTimer-log_S_S__table_kfl_p53_hx__entry__3}

| Name | Type | Description |
|-|-|-|
| name | String | Function name. |
| entry | String | Text that will be part of the output in the log. |
[Table 1. Parameters]

{#r_OCTimer-log_S_S__table_kfl_p53_hx} {#r_OCTimer-log_S_S__table_lfl_p53_hx__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 2. Returns]

{#r_OCTimer-log_S_S__table_lfl_p53_hx}

## OCTimer - millisToTime (Integer millis) {#ariaid-title3}

Takes a millisecond value and returns a formatted duration.
{#r_OCTimer-millisToTime_I__table_ysh_253_hx__entry__3}

| Name | Type | Description |
|-|-|-|
| millis | Integer | Duration in miliseconds. |
[Table 3. Parameters]

{#r_OCTimer-millisToTime_I__table_ysh_253_hx} {#r_OCTimer-millisToTime_I__table_zsh_253_hx__entry__2}

| Type | Description |
|-|-|
| String | A formatted duration. |
[Table 4. Returns]

{#r_OCTimer-millisToTime_I__table_zsh_253_hx}

## OCTimer - result () {#ariaid-title4}

Calculates the time taken for each function registered.
{#r_OCTimer-result__table_wdt_l53_hx__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 5. Parameters]

{#r_OCTimer-result__table_wdt_l53_hx} {#r_OCTimer-result__table_xdt_l53_hx__entry__2}

| Type | Description |
|-|-|
| String | Time taken for each function registered. |
[Table 6. Returns]

{#r_OCTimer-result__table_xdt_l53_hx}  
The following code example benchmarks the time taken by a REST API to fetch IP addresses
from devices in Network Gear and then inserts them into IP Address table. API calls may
require some time to process, so it is advisable to benchmark execution time before
executing them in production.

    var user = "<user name>"; // ServiceNow login user name
    var password = "<password>";  // ServiceNow login password
    var instance = "<instance name>";   // ServiceNow instance name

    // Insert new IP address to IP address table using REST API
    function insertIPAddress(ip_address) {
     var request = new sn_ws.RESTMessageV2();
     request.setEndpoint('https://' + instance + '.service-now.com/api/now/table/cmdb_ci_ip_address');
     request.setHttpMethod('POST');

     request.setBasicAuth(user,password);
     request.setRequestHeader("Accept","application/json");
     request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"ip_address\":\"" + ip_address + "\"}");
     var response = request.execute();
    }

    // Retrieve IP address from devices in Network gear
    function getIPAddresses() {
     var request = new sn_ws.RESTMessageV2();
     request.setEndpoint('https://' + instance + '.service-now.com/api/now/table/cmdb_ci_netgear?sysparm_fields=ip_address');
     request.setHttpMethod('GET');

     request.setBasicAuth(user,password);
     request.setRequestHeader("Accept","application/json");

     var response = request.execute();
     return response.getBody();
    }

    var timer = new OCTimer();             // create OCTimer object

    // get the IP addresses from Network Gear table
    timer.start("getIPAddresses");         // start timer to time fetching IP addresses from Network Gear table
    var ipAddressStr = getIPAddresses();
    timer.stop('getIPAddresses');          // stop timer

    // insert IP addresses to IP Address table
    var ipAddressJson = JSON.parse(ipAddressStr);  // parse result
    var ipAddressList = ipAddressJson.result;      // extract IP addresses to a list
    for (var i=0; i<ipAddressList.length;i++) {    // loop through
     var ip_address = ipAddressList[i]["ip_address"].trim();
     if (ip_address.length > 0) {
      timer.start("insertIPAddress");    // start timer to time inserting IP address to IP Address table
      insertIPAddress(ip_address);       // insert IP address
      timer.stop('insertIPAddress');     // end timer
     }
    }

    var result = timer.result();         // get timer results
    gs.info(result);                     // output timer result

Output:

    getIPAddresses method was invoked 1 time and took 00.288 seconds
    insertIPAddress method was invoked 37 times and took 02.352 seconds

## OCTimer - start (String name) {#ariaid-title5}

Initializes a timer based on the name provided.
{#r_OCTimer-start_S__table_dyg_5t3_hx__entry__3}

| Name | Type | Description |
|-|-|-|
| name | String | Function name. |
[Table 7. Parameters]

{#r_OCTimer-start_S__table_dyg_5t3_hx} {#r_OCTimer-start_S__table_eyg_5t3_hx__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 8. Returns]

{#r_OCTimer-start_S__table_eyg_5t3_hx}  
The following code example benchmarks the time taken by a REST API to fetch IP addresses
from devices in Network Gear and then inserts them into IP Address table. API calls may
require some time to process, so it is advisable to benchmark execution time before
executing them in production.

    var user = "<user name>"; // ServiceNow login user name
    var password = "<password>";  // ServiceNow login password
    var instance = "<instance name>";   // ServiceNow instance name

    // Insert new IP address to IP address table using REST API
    function insertIPAddress(ip_address) {
     var request = new sn_ws.RESTMessageV2();
     request.setEndpoint('https://' + instance + '.service-now.com/api/now/table/cmdb_ci_ip_address');
     request.setHttpMethod('POST');

     request.setBasicAuth(user,password);
     request.setRequestHeader("Accept","application/json");
     request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"ip_address\":\"" + ip_address + "\"}");
     var response = request.execute();
    }

    // Retrieve IP address from devices in Network gear
    function getIPAddresses() {
     var request = new sn_ws.RESTMessageV2();
     request.setEndpoint('https://' + instance + '.service-now.com/api/now/table/cmdb_ci_netgear?sysparm_fields=ip_address');
     request.setHttpMethod('GET');

     request.setBasicAuth(user,password);
     request.setRequestHeader("Accept","application/json");

     var response = request.execute();
     return response.getBody();
    }

    var timer = new OCTimer();             // create OCTimer object

    // get the IP addresses from Network Gear table
    timer.start("getIPAddresses");         // start timer to time fetching IP addresses from Network Gear table
    var ipAddressStr = getIPAddresses();
    timer.stop('getIPAddresses');          // stop timer

    // insert IP addresses to IP Address table
    var ipAddressJson = JSON.parse(ipAddressStr);  // parse result
    var ipAddressList = ipAddressJson.result;      // extract IP addresses to a list
    for (var i=0; i<ipAddressList.length;i++) {    // loop through
     var ip_address = ipAddressList[i]["ip_address"].trim();
     if (ip_address.length > 0) {
      timer.start("insertIPAddress");    // start timer to time inserting IP address to IP Address table
      insertIPAddress(ip_address);       // insert IP address
      timer.stop('insertIPAddress');     // end timer
     }
    }

    var result = timer.result();         // get timer results
    gs.info(result);                     // output timer result

Output:

    getIPAddresses method was invoked 1 time and took 00.288 seconds
    insertIPAddress method was invoked 37 times and took 02.352 seconds

## OCTimer - stop (String name) {#ariaid-title6}

Registers the end of the timer for the provided name.
{#r_OCTimer-stop_S__table_drh_153_hx__entry__3}

| Name | Type | Description |
|-|-|-|
| name | String | Function name. |
[Table 9. Parameters]

{#r_OCTimer-stop_S__table_drh_153_hx} {#r_OCTimer-stop_S__table_erh_153_hx__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 10. Returns]

{#r_OCTimer-stop_S__table_erh_153_hx}  
The following code example benchmarks the time taken by a REST API to fetch IP addresses
from devices in Network Gear and then inserts them into IP Address table. API calls may
require some time to process, so it is advisable to benchmark execution time before
executing them in production.

    var user = "<user name>"; // ServiceNow login user name
    var password = "<password>";  // ServiceNow login password
    var instance = "<instance name>";   // ServiceNow instance name

    // Insert new IP address to IP address table using REST API
    function insertIPAddress(ip_address) {
     var request = new sn_ws.RESTMessageV2();
     request.setEndpoint('https://' + instance + '.service-now.com/api/now/table/cmdb_ci_ip_address');
     request.setHttpMethod('POST');

     request.setBasicAuth(user,password);
     request.setRequestHeader("Accept","application/json");
     request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"ip_address\":\"" + ip_address + "\"}");
     var response = request.execute();
    }

    // Retrieve IP address from devices in Network gear
    function getIPAddresses() {
     var request = new sn_ws.RESTMessageV2();
     request.setEndpoint('https://' + instance + '.service-now.com/api/now/table/cmdb_ci_netgear?sysparm_fields=ip_address');
     request.setHttpMethod('GET');

     request.setBasicAuth(user,password);
     request.setRequestHeader("Accept","application/json");

     var response = request.execute();
     return response.getBody();
    }

    var timer = new OCTimer();             // create OCTimer object

    // get the IP addresses from Network Gear table
    timer.start("getIPAddresses");         // start timer to time fetching IP addresses from Network Gear table
    var ipAddressStr = getIPAddresses();
    timer.stop('getIPAddresses');          // stop timer

    // insert IP addresses to IP Address table
    var ipAddressJson = JSON.parse(ipAddressStr);  // parse result
    var ipAddressList = ipAddressJson.result;      // extract IP addresses to a list
    for (var i=0; i<ipAddressList.length;i++) {    // loop through
     var ip_address = ipAddressList[i]["ip_address"].trim();
     if (ip_address.length > 0) {
      timer.start("insertIPAddress");    // start timer to time inserting IP address to IP Address table
      insertIPAddress(ip_address);       // insert IP address
      timer.stop('insertIPAddress');     // end timer
     }
    }

    var result = timer.result();         // get timer results
    gs.info(result);                     // output timer result

Output:

    getIPAddresses method was invoked 1 time and took 00.288 seconds
    insertIPAddress method was invoked 37 times and took 02.352 seconds


