This is a utility module.

The module is included with Mule runtime. To use it, you must import it to your DataWeave code, for example, by adding the line import * from dw::util::Timer to the header of your 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): DurationMeasurement<T>

Executes the function and returns an object with the taken time in milliseconds with the result of the 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 result 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): TimeMeasurement<T>

Executes the specified function and returns an object with the start time and end time with the result of the function.

Parameters

Name Description

valueToMeasure

A function to pass to time.

Example

This example passes a wait function (defined in the header), which returns the result in a TimeMeasurement object.

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

Output

1
2
3
4
5
{
  "start": "2018-09-26T18:58:00.887Z",
  "result": "My result",
  "end": "2018-09-26T18:58:00.988Z"
}

toMilliseconds

toMilliseconds(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

Represents a time taken by a function call.

Definition
1
{ time: Number, result: T }

TimeMeasurement

Represents a start/end time measurement.

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