[SR-Users] Computing a time difference when handling a stateless SIP request
Fred Posner
fred at palner.com
Fri Sep 25 21:19:41 CEST 2020
On Fri, 2020-09-25 at 15:34 +0000, 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 may also want to look at benchmarking:
https://www.kamailio.org/docs/modules/stable/modules/benchmark.html
As for the data formatting, I'm sure you can accomplish what you'd
like with transformations, but that being said, it might also be
beneficial to just mark the info and then use an outside script or
programs to calculate the data.
--fred
More information about the sr-users
mailing list