Shan Peiris
Shan Peiris

Reputation: 183

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

Upvotes: 3

Views: 9531

Answers (4)

AbdullahTariq
AbdullahTariq

Reputation: 93

You can also use performance.now(). This link has some comparisons between console.time and performance.now.

Upvotes: 2

robertklep
robertklep

Reputation: 203514

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');
    });
}

Upvotes: 4

daan.desmedt
daan.desmedt

Reputation: 3820

As an alternative to the above by @Vivz, I use the console.time functions to measure durations (ms).

/* start time ‘test’ */
console.time('test');

/* stop time ‘test’ */
console.timeEnd('test');

/* stop time output ms */
test: 8254.471ms

Upvotes: 1

Vivz
Vivz

Reputation: 6630

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.

Upvotes: 2

Related Questions