mld has asked for the wisdom of the Perl Monks concerning the following question:

I want to calculate network usage of an application.how this can be acomplished using Perl

Replies are listed 'Best First'.
Re: Network Usage of an application
by almut (Canon) on Apr 12, 2010 at 16:27 UTC

    As already pointed out, it's not exactly clear what you mean, but maybe try something like this:

    #!/usr/bin/perl use GTop (); my $gtop = GTop->new(); my %pre_stats; sub get_netstats { my $init = shift; my $netload = $gtop->netload("eth0"); for (qw(bytes_out bytes_in bytes_total packets_out packets_in packets_total)) { if ($init) { $pre_stats{$_} = $netload->$_(); } else { printf "%-14s: %d\n", $_, $netload->$_() - $pre_stats{$_}; } } } get_netstats("initial"); system "wget", "http://perlmonks.org"; # your app here get_netstats(); __END__ $ ./834243.pl --18:20:17-- http://perlmonks.org/ => `index.html' Resolving perlmonks.org... 66.39.54.27, 209.197.123.153 Connecting to perlmonks.org|66.39.54.27|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 83,769 (82K) [text/html] 100%[================================================================= +==========>] 83,769 118.72K/s 18:20:18 (118.47 KB/s) - `index.html' saved [83769/83769] bytes_out : 3553 bytes_in : 88978 bytes_total : 92531 packets_out : 51 packets_in : 68 packets_total : 119

    Of course, as the traffic info is interface-global (not process-specific), there mustn't be any other significant network traffic going on while you're measuring...

      Thanks. But not able to install the Gtop module in my system. Is this module is applicable for Windows system.In the net It has been said Gtop is used for Unix.

        Chances are it will work on windows if libgtop works on windows, and since libgtop is a gnobe library, chances are it might work on windows. :)

        Alternatively there is Win32::PerfMon/ Win32::Process::Perf which perform a similar function as libgtop.

Re: Network Usage of an application
by Anonymous Monk on Apr 12, 2010 at 12:06 UTC
    What is "network usage"?