]>
git.ipfire.org Git - thirdparty/squid.git/blob - scripts/cache-compare.pl
3 ## Copyright (C) 1996-2020 The Squid Software Foundation and contributors
5 ## Squid software is distributed under GPLv2+ license and includes
6 ## contributions from numerous individuals and organizations.
7 ## Please see the COPYING and CONTRIBUTORS files for details.
12 # Duane Wessels, Dec 1995
14 # A simple perl script to compare how long it takes to fetch an object
15 # from a number of different caches.
17 # stdin is a list of URLs. Set the @getfrom array to a list of caches
18 # to fetch each URL from. Include 'SOURCE' in @getfrom to fetch from
19 # the source host also. For each URL, print the byte count, elapsed
20 # time and average data rate. At the end print out some averages.
22 # NOTE: uses the Perl function syscall() to implement gettimeofday(2).
23 # Assumes that gettimeofday is syscall #116 on the system
24 # (see /usr/include/sys/syscall.h).
27 # Should probably cache the gethostbyname() calls.
29 @getfrom = ('SOURCE', 'localhost:3128', 'bo:3128');
31 require 'sys/socket.ph';
32 $gettimeofday = 1128; # cheating, should use require syscall.ph
38 foreach $k (@getfrom) {
39 printf "%30.30s:\t", $k;
41 ($b_sec,$b_usec) = &gettimeofday
;
42 $n = &get_from_source
($url);
43 ($e_sec,$e_usec) = &gettimeofday
;
45 ($host,$port) = split (':', $k);
46 ($b_sec,$b_usec) = &gettimeofday
;
47 $n = &get_from_cache
($host,$port,$url);
48 ($e_sec,$e_usec) = &gettimeofday
;
51 $d = ($e_sec - $b_sec) * 1000000 + ($e_usec - $b_usec);
54 printf "%8.1f b/s (%7d bytes, %7.3f sec)\n",
63 print "AVERAGE b/s rates:\n";
64 foreach $k (@getfrom) {
65 printf "%30.30s:\t%8.1f b/s (Alt: %8.1f b/s)\n",
67 $bps_sum{$k} / $bps_n{$k},
68 $bytes_sum{$k} / $sec_sum{$k};
76 unless ($url =~ m!([a-z]+)://([^/]+)(.*)$!) {
77 printf "get_from_source: bad URL\n";
83 unless ($proto eq 'http') {
84 printf "get_from_source: I only do HTTP\n";
88 if ($host =~ /([^:]+):(\d+)/) {
92 return 0 unless ($SOCK = &client_socket
($host,$port));
93 print $SOCK "GET $url_path HTTP/1.0\r\nAccept */*\r\n\r\n";
94 $bytes += $n while (($n = read(SOCK
,$_,4096)) > 0);
100 local($host,$port,$url) = @_;
102 return 0 unless ($SOCK = &client_socket
($host,$port));
103 print $SOCK "GET $url HTTP/1.0\r\nAccept */*\r\n\r\n";
104 $bytes += $n while (($n = read(SOCK
,$_,4096)) > 0);
110 local ($host, $port) = @_;
111 local ($sockaddr) = 'S n a4 x8';
112 local ($name, $aliases, $proto) = getprotobyname('tcp');
113 local ($connected) = 0;
115 # Lookup addresses for remote hostname
117 local($w,$x,$y,$z,@thataddrs) = gethostbyname($host);
118 unless (@thataddrs) {
119 printf "Unknown Host: $host\n";
123 # bind local socket to INADDR_ANY
125 local ($thissock) = pack($sockaddr, &AF_INET
, 0, "\0\0\0\0");
126 unless (socket (SOCK
, &AF_INET
, &SOCK_STREAM
, $proto)) {
127 printf "socket: $!\n";
130 unless (bind (SOCK
, $thissock)) {
137 foreach $thataddr (@thataddrs) {
138 local ($that) = pack($sockaddr, &AF_INET
, $port, $thataddr);
139 if (connect (SOCK
, $that)) {
144 unless ($connected) {
145 printf "$host:$port: $!\n";
149 # Set socket to flush-after-write and return it
151 select (SOCK
); $| = 1;
157 $tvp="\0\0\0\0\0\0\0\0";
158 syscall($gettimeofday, $tvp, $tz);
159 return unpack('ll', $tvp);