10
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
Your unique 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 the iteration in ZX1
to redeem the same information in 80,000 lines of the same query have some weight in performance, but I do not know any profiler to be sure of how much is being dedicated to that particular piece of code. I have no security to make time measurement within the code without that measurement does not change the measurement object and its measurement.
For the iteration in
ZX1
, I refer to the whole process, since the initialization withdbSelectArea("ZX1")
up to the teardown of the tie withZX1-> (dbCloseArea())
Another point of doubt I have about performance is about substrings
, in that passage: 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 don’t know profiler to do so, and this time I have no knowledge of how to measure from within the memory code used, called garbage collectors or fragmentation.
So I repeat my question of the title:
How to analyze the performance impact of a code snippet?
The intention here is to avoid doing some unnecessary refactoring in the code, which ultimately has little gain and is difficult to do. Just to change the hotspots of the code snippet.
@Bacco, yes, I couldn’t see how to solve this macro (
&(conv2Json(ZX1->ZX1_CP_PRO, cTabela))
) statically without having to interpret this piece of code... but something tells me there are other bottlenecks that are easier to remove– Jefferson Quesado