This utility module contains functions for measuring time.

To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::util::Timer to the header of your DataWeave script.

Functions

currentMilliseconds

currentMilliseconds(): Number

Returns the current time in milliseconds.

Example

This example shows the time in milliseconds when the function executed.

Source
1
2
3
4
5
%dw 2.0
import * from dw::util::Timer
output application/json
---
{ "currentMilliseconds" : currentMilliseconds() }
Output
1
{ "currentMilliseconds": 1532923168900 }

duration

duration<T>(valueToMeasure: () -> T): DurationMeasurement<T>

Executes the input function and returns an object with execution time in milliseconds and result of that function.

Parameters
Name Description

valueToMeasure

A function to pass to duration.

Example

This example passes a wait function (defined in the header), which returns the execution time and result of that function in a DurationMeasurement object.

Source
1
2
3
4
5
%dw 2.0
output application/json
fun myFunction() = dw::Runtime::wait("My result",100)
---
dw::util::Timer::duration(() -> myFunction())
Output
1
2
3
4
{
  "time": 101,
  "result": "My result"
}

time

time<T>(valueToMeasure: () -> T): TimeMeasurement<T>

Executes the input function and returns a TimeMeasurement object that contains the start and end time for the execution of that function, as well the result of the function.

Parameters
Name Description

valueToMeasure

A function to pass to time.

Example

This example passes wait and sum functions (defined in the header), which return their results in TimeMeasurement objects.

1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
fun myFunction() = dw::Runtime::wait("My result",100)
fun myFunction2() = sum([1,2,3,4])
---
{ testing: [
    dw::util::Timer::time(() -> myFunction()),
    dw::util::Timer::time(() -> myFunction2())
  ]
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "testing": [
    {
      "start": "2018-10-05T19:23:01.49Z",
      "result": "My result",
      "end": "2018-10-05T19:23:01.591Z"
    },
    {
      "start": "2018-10-05T19:23:01.591Z",
      "result": 10,
      "end": "2018-10-05T19:23:01.591Z"
    }
  ]
}

toMilliseconds

toMilliseconds(date: DateTime): Number

Returns the representation of a specified date-time in milliseconds.

Parameters
Name Description

date

A DateTime to evaluate.

Example

This example shows a date-time in milliseconds.

Source
1
2
3
4
5
%dw 2.0
import * from dw::util::Timer
output application/json
---
{ "toMilliseconds" : toMilliseconds(|2018-07-23T22:03:04.829Z|) }
Output
1
{ "toMilliseconds": 1532383384829 }

Types

DurationMeasurement

A return type that contains the execution time and result of a function call.

Definition
1
{ time: Number, result: T }

TimeMeasurement

A return type that contains a start time, end time, and result of a function call.

Definition
1
{ start: DateTime, result: T, end: DateTime }