After adding more tags (up to 44 now) I saw the execution time ~900 ms.
I took your advice and edited the script, but it only brought it down to ~600 ms, which does seem very slow.
dataLogger = system.tag.readBlocking(["[default]DataLogger"])[0].value
if dataLogger:
#create empty lists for holding tag properties
value_paths = []
logging_paths = []
opcPath_paths = []
#filter headloss
value_paths.append("[default]Tag1")
value_paths.append("[default]Tag2")
value_paths.append("[default]Tag3")
#.
#. more tags
#.
for path in value_paths:
logging_paths.append(path+".loggingEnabled")
opcPath_paths.append(path+".OpcItemPath")
tagRead_paths = value_paths + logging_paths + opcPath_paths
tagsRead = system.tag.readBlocking(tagRead_paths)
for value_index,path in enumerate(value_paths):
logging_index = value_index + len(value_paths) #offset logging index from start
opc_index = logging_index + len(logging_paths) #offset opcPath index from start
tagHistory.logTagChange(tagsRead[logging_index].value, 0, 0, tagsRead[value_index].value, False, tagsRead[opc_index].value)
Turns out the slowness is in the tagHistory.logTagChange()
call. Without it, the new code runs in 2ms.
I had another system.tag.readBlocking()
inside that function, so I had 45 total calls to it. I thought this would’ve been the issue, but removing it did nothing to help.
See any other places for optimization?
Here’s tagHistory.logTagChange()
:
def logTagChange(loggingEnabled, historicalDeadband, minSampleRate, currentValue, initialChange, OpcPath):
if ((1 == loggingEnabled) and (not initialChange)):
#trim OPC path for storing in database
PlcTag = OpcPath.replace("ns=1;s=[PLC]","")
#get latest DB info
lastDBvalue = system.db.runNamedQuery("My_Project","Last_Tag_Value",{"tag_name":PlcTag})
if ( not ((0 == lastDBvalue.rowCount))):
lastVal = lastDBvalue.getValueAt(0,'val')
lastTime = lastDBvalue.getValueAt(0,'t_stamp')
deltaT = system.date.secondsBetween(lastTime,system.date.now())
#large enough change in value to log the change
if ((0 == lastDBvalue.rowCount) or (abs(currentValue - lastVal) >= historicalDeadband) or ((minSampleRate > 0) and (deltaT >= minSampleRate))):
system.db.runNamedQuery("My_Project", "Store_Tag_Change", {"tag_name":PlcTag,"val":currentValue})
Last_Tag_Value
Query:
SELECT TOP 1 val, t_stamp FROM [dbo].[Data] where tag_id=:tag_name order by t_stamp desc
Store_Tag_Change
Query:
INSERT INTO [dbo].[Data] ([tag_id],[val])
VALUES (:tag_name,:val)
There might be a way to write a different query to insert all of the rows at once, rather than inserting them one at a time with the for loop, but I’m not super familiar with SQL.