DEV Community

Alex Martinez
Alex Martinez

Posted on • Edited on

5

Using time() and duration() in DataWeave for performance check

You can use duration() to get the total number of milliseconds it took to execute a function.

Script:

%dw 2.0
output application/json
import duration from dw::util::Timer
fun myFunc() = [1, 2, 3] reduce $+$$
---
duration(() -> myFunc())
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "time": 0,
  "result": 6
}
Enter fullscreen mode Exit fullscreen mode

You can use time() to get an even more detailed result of when the function started and ended. This will give you more insight into the total time it took to execute.

Script:

%dw 2.0
output application/json
import time from dw::util::Timer
fun myFunc() = [1, 2, 3] reduce $+$$
---
time(() -> myFunc())
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "start": "2023-03-21T16:00:21.487377Z",
  "result": 6,
  "end": "2023-03-21T16:00:21.487408Z"
}
Enter fullscreen mode Exit fullscreen mode

This way you can use - to get the total number of time.

Script:

%dw 2.0
output application/json
import time from dw::util::Timer
fun myFunc() = [1, 2, 3] reduce $+$$
var funcResult = time(() -> myFunc())
---
funcResult.end - funcResult.start
Enter fullscreen mode Exit fullscreen mode

Output:

"PT0.000024S"
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay