[SR-Users] Computing a time difference when handling a stateless SIP request

Daniel-Constantin Mierla miconda at gmail.com
Tue Sep 29 11:43:34 CEST 2020


On 25.09.20 17:34, Chaigneau, Nicolas wrote:
>>> --snip 
>>> I would like to compute a time difference (with microseconds
>>> precision) when handling a stateless SIP request.
>>> --snip
>> Welcome!
>>
>> Take a look at:
>> https://www.kamailio.org/wiki/cookbooks/5.4.x/pseudovariables#timeval
>>
>> You can get down to the microsecond level.
>
> Thanks!
>
> Looking at src\modules\pv\pv_time.c, it seems TV functions just use "gettimeofday".
> I was looking for a time difference based on a monotonically increasing clock (to avoid possible NTP drifting imprecision).
>
> I've looked at the "benchmark" module which seems to do something like that, but I don't think I can use it for what I need...
>
>
>
> Anyhow, I have another question. Using the TV I've come up with the following.
> This works, except that I don't know how to convert an int to a string padded with zeroes (e.g. 51 => "051").
> Is there a transformation that allows to do that?
>
>
> 	$var(start_time) = $TV(Sn); # allows to cache $TV(s) and $TV(u)
>
> 	(...)
>
> 	$var(end_time) = $TV(Sn);
> 	$var(end_s) = $TV(sn);
> 	$var(end_us) = $TV(un);
> 	$var(diff_us) = $var(end_us) - $TV(u) + (1000000 * ($var(end_s) - $TV(s)));
>
> 	# convert this to ms (integer part) and us (fractional part)
> 	$var(diff_ms) = $var(diff_us) / 1000;
> 	$var(diff_us_f) = $var(diff_us) - (1000 * $var(diff_ms));
>
> 	$var(diff_ms_us) = $var(diff_ms) + "." + $var(diff_us_f); # need to format on 3 digits... can we do the equivalent of sprintf("%03u") somehow ?

You can try with concatenating a variable set to "000" stripped by the
length of milliseconds string with the value of the milliseconds string
-- see the transformations cookbook for strip variant.

Also, useful for complex operations, have in mind that you can combine
use of kamailio cfg scripting language with other embedded interpreters,
such as app_jsdt, app_lua, app_python, ...

On the other hand, if you just want to log how long it takes to execute
config for a request or a reply, then you can get it by tuning the value
of core parameter:

  * https://www.kamailio.org/wiki/cookbooks/5.4.x/core#latency_cfg_log

Cheers,
Daniel

-- 
Daniel-Constantin Mierla -- www.asipto.com
www.twitter.com/miconda -- www.linkedin.com/in/miconda
Funding: https://www.paypal.me/dcmierla




More information about the sr-users mailing list