I have the following code in ADVPL:
Static Function linhaJson(cTabela, cChave, lVerificaExclusao)
local cTipo
local xResult
local cJson := "{"
dbSelectArea("ZX1")
ZX1->(dbSetOrder(1))
ZX1->(dbGoTop())
ZX1->(dbSeek(cChave))
While !(ZX1->(EOF())) .AND. cChave == ZX1->(ZX1_FILIAL + ZX1_COD)
If ZX1->ZX1_TIPO == 'B'
xResult := &(conv2Json(ZX1->ZX1_CP_PRO, cTabela))
cTipo := ValType(xResult)//ValType(ZX1->ZX1_CP_PRO)
If cTipo == 'C'
if AllTrim(xResult) != "NULL"
cJson += '"' + AllTrim(Lower(ZX1->ZX1_CP_GEO)) + '":"' + ESCENVST(SUBS(AllTrim(xResult),1,ZX1->ZX1_TAM)) + '",'
EndIf
ElseIf cTipo == 'N'
cJson += '"' + AllTrim(Lower(ZX1->ZX1_CP_GEO)) + '":"' + AllTrim(STR(&(cTabela + "->"+ZX1->ZX1_CP_PRO))) + '",'
ElseIf cTipo == 'D'
cJson += '"' + AllTrim(Lower(ZX1->ZX1_CP_GEO)) + '":"' + AllTrim(DtoS(&(cTabela + "->"+ZX1->ZX1_CP_PRO))) + '",'
EndIf
ElseIf ZX1->ZX1_TIPO == 'S'
cJson += '"' + AllTrim(Lower(ZX1->ZX1_CP_GEO)) + '":"' + AllTrim(ZX1->ZX1_CP_PRO) + '",'
ElseIf ZX1->ZX1_TIPO == 'V'
cJson += '"' + AllTrim(Lower(ZX1->ZX1_CP_GEO)) + '":"",'
Else //ZX1->ZX1_TIPO == 'N'
cJson += ""
EndIf
ZX1->(dbSkip())
EndDo
ZX1->(dbCloseArea())
cJson := SUBS(cJson, 1, len(cJson) - 1)
If lVerificaExclusao //Se for excluso
cJson += ',"data_delete":"' + getCurrentDate() + '" '
EndIf
cJson += "}"
Return cJson
Its only obligation is to mine data from a query in Protheus and turn it into a JSON for me to consume on my server. It runs for every line of every mining I do.
I believe that the iteration in ZX1
to retrieve the same information in 80,000 rows of the same query has some weight in the performance, however I do not know any profiler to be sure of how much is being dedicated to this particular code snippet. I do not have the security to measure time within the code without this measurement does not change the measurement object and its measurement.
By iteration in
ZX1
, I mean the whole process, from the initialization withdbSelectArea("ZX1")
to the teardown of the loop withZX1-> (dbCloseArea())
Another point of doubt I have about performance is about substrings
, in this section: cJson := SUBS(cJson, 1, len(cJson) - 1)
. This could generate some memory fragmentation allocated to the strings more than I'm already fragmenting. Again, I do not know profiler to do so, and this time I am not aware of how to measure from memory used code, called garbage collector or fragmentation.
So, I reiterate my title question:
How to analyze the performance impact of a code snippet?
The intention here is to avoid doing any unnecessary refactoring in the code, which in the end has little gain and is difficult to do. Just to change the hotspots of the code snippet.