Why does the first AFListData.UpdateValues call take far longer than subsequent calls?

We're creating a PIthon program (PI AF SDK using Python) that queries time series data from a Web API and writes to AF Attributes using AFListData.UpdateValues.

The program performs a separate call for each of four measurement points, and the first call takes far longer than the second one.

Below is a heavily redacted code sample and a screenshot of the output.

 

Code sample:

import requests
import os
import sys
import clr
import configparser
import time
from datetime import datetime, timedelta
from System.Collections.Generic import List as ListNet
from System import DateTime as datetimeNET

start_time = time.time()

# afobjsmeasidlookup is a Python dictionary<integer, AFAttribute>.
[print(measid, attribute) for measid, attribute in afobjsmeasidlookup.items()]

for measid in afobjsmeasidlookup:
    apiURL = apiURLBase + r"/api/measurements/{}/data?start={}".format(str(measid), dateLookback.strftime("%Y-%m-%d"))
    apiResponse = requests.get(apiURL, headers = headers, verify=sslVerify)
    vals        = apiResponse.json()
    milestones.append((round((time.time() - start_time), 1), " API call completed."))
    # values, tsnet, stats and uoms will be used in AFValues constructor.
    values      = [meas['avg'] for meas in vals]                                  
    tsnet       = [datetimeNET.Parse(str(meas['timestamp'])) for meas in vals]     
    stats       = [AFValueStatus.Good]*len(values)                                 
    for afatt in afobjsmeasidlookup[measid]:
        uoms    = [afatt.DefaultUOM] * len(values)                             
        theseafvals = AFValues(values, tsnet, stats, uoms)
        theseafvals.Attribute = afatt
        AFListData.UpdateValues(theseafvals, AFUpdateOption.Replace)
        milestones.append((round((time.time() - start_time), 1), " {} AFValues written.".format(str(theseafvals.Count))))
        

[print ("{}: {}".format(str(ts), info)) for (ts, info) in milestones]

 

Output from line 32:

 


Output from line 32 in code snippet.png
 

A look at the afobjsmeasidlookup dictionary:

 


image.png.png
 

 

 

Is the problem related to the initial call having to first establish a connection to the PI Data Archive or AF server?

  • Your suspicion is correct. The first call for connecting to the AF Server (PISystem) or PI Data Archive (PIServer) does consume the most time. Not much you can do about that but the caution that lingers is if you ever had different methods or techniques and wanted to compare performance, remember the order is very important as the first method tested will be the slowest due to initial connection overhead.