Calculate TTLB (Time To Last Byte) and TTFB (Time To First Byte)

Can someone please help me providing the logic to calculate TTLB and TTFB in Apigee ?

I see Apigee provides following variables which holds some times to calculate but need help on how to utilize them to achieve TTLB and TTFB values.

client.received.end.time

client.received.end.timestamp

client.received.start.time

client.received.start.timestamp

0 2 3,655
2 REPLIES 2

really appreciate any help on this query please

You need to take care to define perspectives.

Time-to-first-byte and time-to-last-byte can theoretically be measured by a client for a request it sends out. Time-to-first-byte is the time it takes for a client to receive the first byte of a response to a request that it sends. Time to last byte is the time it takes for the client to receive ALL content of the response.

A client that connects to an endpoint is the only party that can reasonably calculate TTFB an TTLB. More to the point, the resource serving the endpoint cannot compute these values. There are many potential hops and buffers and caches between a client and an endpoint, and the endpoint implementation cannot know these, and cannot know what effect these things might have on the client experience. Only the client can compute the TTFB and TTLB, which are meaningful only to that specific client. A corollary: If the endpoint in question is hosted in Apigee Edge, then the context variables defined within Apigee Edge are not useful to calculate these values.

On the other hand, if you want to consider Apigee Edge as the client, and the endpoint in question is the target system, then you can use context variables to compute TTFB and TTLB.

The variables of interest are:

  • target.sent.start.timestamp
  • target.received.start.timestamp
  • target.received.end.timestamp

The first can be considered "start time" of the request. Subtracting it from the second var gives you TTFB. Subtracting it from the 3rd time give you TTLB. You can do these calculations in a JavaScript policy. The code might look like this:

var start, end, delta;
start = context.getVariable('target.sent.start.timestamp');
end = context.getVariable('target.received.end.timestamp');
delta = Math.floor(end - start);
context.setVariable('target_ttlb', delta.toFixed(0));
end = context.getVariable('target.received.start.timestamp');
delta = Math.floor(end - start);
context.setVariable('target_ttfb', delta.toFixed(0));

This policy can run any time after the target response has been received. Eg, target postflow, or later.

Notice these variables all end in .timestamp. These are long quantities. The similarly named variables that end in .time are string quantities. Don't use those for arithmetic.

You could try to compute the similar quantities for the full end-to-end request, from the point of view of the Apigee Edge proxy. This would not be the TTFB or TTLB from the client's perspective, so we cannot really call them TTFB and TTLB. Rather, it would be a calculation from the server's perspective - from the perspective of the proxy. So if we do calculate it, it could not be as meaningful as actual TTFB and TTLB.

Even with those caveats, the attempt is doomed. Consider this code :

// DO NOT USE
var start, end, delta;
start = context.getVariable('client.received.start.timestamp');
end = context.getVariable('system.timestamp');
delta = Math.floor(end - start);
context.setVariable('proxy_ttlb', delta.toFixed(0)); // not really
end = context.getVariable('client.sent.start.timestamp');
delta = Math.floor(end - start);
context.setVariable('proxy_ttfb', delta.toFixed(0));  // ha ha , no way

Suppose you run this policy at the latest possible moment, in the proxy PostFlow. This is still BEFORE the client has received its any of its response (if streaming is not enabled). In other words it is still before the time indicated by "client.sent.start.timestamp". And "system.timestamp" will represent "right now", which isn't "when the client received the last byte". In the proxy postflow, system.timestamp is just... "right now". It has no relation to the time when transmission of data to the client began or ended. Therefore the quantities calculated by this arithmetic don't represent real information. Ideally you want this to run in the PostClientFlow, which is to say AFTER the client has received it's response. but AFAIK, that flow allows only MessageLogging policies, sadly.

This is why I say you can calculate TTFB and TTLB only in the client that is sending the request and receiving the response.

But...

Having said all of this, I think TTFB and TTLB are probably not good things for you to try to calculate. You have, within Apigee Analytics, the response time (latency) of each API Proxy request, along with some elaborated statistics, like TP99 and TP95 over various windows. Use THEM, rather than trying to modify your proxies to calculate relevant data. Unless you are doing something pretty magical, like modifying a weighted load balancing scheme for targets based on the time a target takes to respond, you probably don't need the arithmetic I showed above.