perf-test.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/perl -w
  2. # Klimbim Software collection, A bag full of things
  3. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # 2020 by Johannes 'Banana' Keßler
  18. # some improved simple page load check script.
  19. # multiple URL and how ofthen they are accessed
  20. use warnings;
  21. use strict;
  22. use LWP::UserAgent;
  23. use HTTP::Request;
  24. use HTTP::Cookies;
  25. use utf8;
  26. use Time::HiRes qw/ time sleep /;
  27. my @urlsToCheck = (
  28. 'https://www.some.tld'
  29. 'https://www.some.tld/with/path',
  30. 'https://www.some-other.tld'
  31. );
  32. my @timesToCheck = (1..10);
  33. my $localtime = localtime;
  34. my ($day,$month,$date,$hour,$year) = split /\s+/,scalar localtime;
  35. my $request_headers = [
  36. 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/999.99 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36',
  37. 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  38. 'Accept-Charset' => 'iso-8859-1,*,utf-8',
  39. 'Accept-Language' => 'en-US',
  40. 'accept-encoding' => 'gzip, deflate, br',
  41. 'Cache-Control' => 'no-cache',
  42. ];
  43. #
  44. #$ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
  45. my $cookies = HTTP::Cookies->new();
  46. # set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )
  47. $cookies->set_cookie(0,'name', 'value','/','www.some.tld');
  48. print "\n+" .('-' x 90) . "+\n";
  49. print "|", " Time: $hour",' ' x 75,"|\n";
  50. print "|", ' URL',' ' x 70,'TIME',' ' x 12,"|\n";
  51. print "+" .('-' x 90) . "+\n";
  52. foreach my $url(@urlsToCheck) {
  53. for (@timesToCheck) {
  54. check_url($url);
  55. }
  56. }
  57. sub check_url {
  58. my ($target) = @_;
  59. my $ua = LWP::UserAgent->new;
  60. $ua->cookie_jar($cookies);
  61. my $req = HTTP::Request->new(GET => $target, $request_headers);
  62. my $start = time;
  63. my $res = $ua->request($req);
  64. if ($res->is_success) {
  65. my $time = time;
  66. $time = ($time - $start);
  67. my $displayUrl = substr($target, -70);
  68. my $out = sprintf "%-74s %.4f sec \n", $displayUrl, $time;
  69. print $out;
  70. }
  71. }