Execution times

this block of code executes in an average of 352 ms for each device in list tD on HE and 19 ms on ST. anything i could do to optimize this for HE?

tD.each     {
    ifDebug("${now() - time} ms")
    def lastEvents
    def deviceEventFound = false
    for (def x = 1; x < 3; x++)     {
        def noOfEvents = Math.pow(5, x).toInteger()
        if (hT == _SmartThings)
            lastEvents = it.events(max: noOfEvents).findAll    { it.eventSource == 'DEVICE' && it.date.after(cDT) }
        else
            lastEvents = it.events(max: noOfEvents).findAll    { it.source == 'DEVICE' && it.date.after(cDT) }
        if (lastEvents)     {
            deviceEventFound = true
            break
        }
    }
    if (!deviceEventFound)      dHC = dHC + (dHC ? ', ' : '') + it.displayName;
}

thank you.

Have a look at the timestamp in device current state, vs event history, event history queries are expensive.

ok. question:

  1. the intent was to find when the device last sent an event to the hub. will looking at timestamp in device current state achieve the same? ie does the timestamp get updated when the device successfully last communicates with the hub?
  2. if yes to #1, how do i have a look at the timestamp in device current state? is that an attribute?

thank you.

def lastEvent = singleDevice.currentStates.date.max()

1 Like

that brought it down to 16 ms per device on HE. :slight_smile:

interestingly this was a simple but great example of why distributed systems work well even in the cloud when scale is key.

1 Like

I guess, not like we've spent any time optimizing that specific query.

wasnt expecting that you had and wasnt meant as a critique … just an observation i found interesting in an unexpected context. :slight_smile:

1 Like