5 sec delay in service call

Hi!

I'm getting a strange behaviour using Sparkle server. I have a server (Http.sys based) with one URL that clients call. The clients are Dahua ANPR cameras that send JSON objects to the sparkle server. Tje objects contain pictures so are relatively large (approx 1mb).

The problem is that as I can see the camera sends the request and it's only after 4-5 seconds that I se the server reacts to the call. I can't understand why the delay and I'm even not sure that is Sparkle the problematic part. I log the request immediately int he code, but it's still very late.

Is there any way to test where the delay is created? At least regarding Sparkle? I know that could be elsewhere, so please ignore the post if you thing that the problem cannot be in sparke.

The JSON is like this (it's a bit shortened, I removed the Base64 picture):

{
    "Picture": {
        "CutoutPic": {
            "Content": "",
            "PicName": ""
        },
        "NormalPic": {
            "Content": "",
            "PicName": ""
        },
        "Plate": {
            "BoundingBox": [1226, 541, 1372, 579],
            "Confidence": 222,
            "IsExist": true,
            "PlateColor": "Unknown",
            "PlateNumber": "KPTEST",
            "PlateType": "Unknown"
        },
        "SnapInfo": {
            "DefenceCode": "8l1ZdXZjxBrNFl1l",
            "DeviceID": "FORNACE.VHOD",
            "OpenStrobe": false,
            "SnapTime": "2021-08-05 09:48:15"
        },
        "Vehicle": {
            "VehicleSeries": "Unknown"
        },
        "VehiclePic": {
            "Content": "",
            "PicName": ""
        }
    }
}

The service is coded this way:

procedure StartServer;
var
  Module : TAnonymousServerModule;
  resp: string;
  itm: TDahuaItemClass;
  Step: string;
begin
  if Assigned(SparkleServer) then
     Exit;

  {$REGION 'Priprava skupnega modula - ta se pripravi samo 1x'}
  modMain := TmodMain.Create(nil);
  {$ENDREGION}

  SparkleServer := THttpSysServer.Create;

  Module := TAnonymousServerModule.Create(Settings.ServerUri,
  procedure(const C: THttpServerContext)
    begin
      Step := 'Begin';
      resp := TEncoding.ANSI.GetString(C.Request.Content);

      if length(resp)>100  then begin
        fmServer.Log('Got request');
        try
          Step := 'Itm';
          if Settings.LogRaw then begin
            fmServer.Log('RAW: '+resp); // Dev Only - BIG log messages!
          end;

          itm := TDahuaItemClass.FromJsonString(resp);

          fmServer.Log(itm.Picture.Plate.PlateNumber+': Got request for gate '+itm.Picture.SnapInfo.DeviceID);

          // Kontrola, če ima pravico
          Step := 'Whitelist';
          if not modMain.CheckWhiteList(itm.Picture.Plate.PlateNumber) then begin
            fmServer.Log(itm.Picture.Plate.PlateNumber+' Skipping item, not in whitelist');
            if Assigned(itm) then begin
              FreeAndNil(itm);
            end;
            exit;
          end;

          try
            Step := 'Mqtt';
            SendToMqtt(itm);
          except
            on E:Exception do begin
              fmServer.Log(itm.Picture.Plate.PlateNumber+': MQTT error '+E.Message);
            end;
          end;
          // Shranjevanje v DB
          Step := 'ProcessDahuaItem';
          ProcessDahuaItem(itm);
        except
          on E:Exception do begin
            fmServer.Log('Error (step='+Step+'): '+E.Message);
            C.Response.StatusCode := 500;
            C.Response.ContentType := 'text/html';
          end;
        end;

        {$REGION 'Sprosti itm'}
        if Assigned(itm) then try
          Step := 'Free ITM';
          fmServer.Log(itm.Picture.Plate.PlateNumber+': freeing ITM');
          FreeAndNil(itm);
        except
          on E:Exception do begin
            fmServer.Log('Itm.Free error: '+E.Message);
            C.Response.StatusCode := 500;
            C.Response.ContentType := 'text/html';
          end;
        end;
        {$ENDREGION}

      end else if Pos('keepalive', lowercase(resp))>0 then begin
        if not Settings.SkipKeepAlive then
          fmServer.Log('KeepAlive.'); //Samo za debug..
      end else begin
        fmServer.Log('Unknown message: '+resp);
      end;

      C.Response.StatusCode := 200;
      C.Response.ContentType := 'text/html';
    end
  );

  // Uncomment line below to enable CORS in the server
  Module.AddMiddleware(TCorsMiddleware.Create);

  // Uncomment line below to allow compressed responses from server
  Module.AddMiddleware(TCompressMiddleware.Create);

  SparkleServer.Dispatcher.AddModule(Module);

  SparkleServer.Start;
end;

Do I understand that it takes a long time even to reach the first line in anonymous module, this one:

      Step := 'Begin';

If that's the case, then indeed it doesn't look like Sparkle, because the path to reach that part of the code is very light. It could be something inside the processing (like your ANSI.GetString for example), but it's not clear to me exactly if that's the case.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.