Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

measure time taken for API call

I want to get the time taken for an API call.

i have a web application with angularJS front end and nodejs back end which has end to end connection.

route in back-end

app.get('/api/eventUploadRead', function(req, res) {
         filesReadUploads.find(function(err, events) {                
              if (err)
                  res.send(err);
              res.json(events);
          });
      });

service in front end which call the api

getUploadRead : function() {
        return $http.get('/api/eventUploadRead');
    },

controller

getEventsOfUploadedFile();
 $scope.events;
    function getEventsOfUploadedFile()
    {
        Event.getUploadRead()
        .success(function(event){
        $scope.events=event;
        })
            .error(function (error) {
                $scope.status = 'Unable to load event data: ';
        });
    }

what i want to do exactly is get the time spent until the data passed to the controller from the API call.t the reason to do this is,i want to measure the time change with the decrease of data load.i want to decrease data load and measure the time taken to load accordingly.

can anyone help me this?if not clear please let me know.

Thanks

like image 649
Shan Peiris Avatar asked Dec 10 '25 12:12

Shan Peiris


2 Answers

You can use console.time()/console.timeEnd() to time the request:

function getEventsOfUploadedFile() {
  console.time('getUploadRead');
  Event.getUploadRead()
    .success(function(event) {
      $scope.events = event;
    })
    .error(function(error) {
      $scope.status = "Unable to load event data: ";
    })
    .finally(function() {
      console.timeEnd('getUploadRead');
    });
}
like image 152
robertklep Avatar answered Dec 12 '25 03:12

robertklep


You can start a timer before and after service call to calculate the time taken.

  var start = new Date().getTime();
    getEventsOfUploadedFile();
     $scope.events;
        function getEventsOfUploadedFile()
        {
            Event.getUploadRead()
            .success(function(event){
              var end = new Date().getTime(); 
            $scope.events=event;
            })
                .error(function (error) {
                    $scope.status = 'Unable to load event data: ';
            });
        }
    total_time = end - start;

You can also use the the chrome developer tools to analyse the time of service calls.

like image 43
Vivz Avatar answered Dec 12 '25 05:12

Vivz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!