From 14c545de81dc4335a0024365bb7b9d6764e24df5 Mon Sep 17 00:00:00 2001 From: Banana Date: Sun, 16 Feb 2020 19:22:57 +0100 Subject: [PATCH] some simple ulr calling scripts --- perl/mecha.pl | 67 ++++++++++++++++++++++++++++++++++++++++++++ perl/perf-test.pl | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 perl/mecha.pl create mode 100644 perl/perf-test.pl diff --git a/perl/mecha.pl b/perl/mecha.pl new file mode 100644 index 0000000..2b9458d --- /dev/null +++ b/perl/mecha.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl -w + +# 2020 by Johannes 'Banana' Keßler +# some simple URL load check with running +# headless (or not) chrome + +use warnings; +use strict; +use utf8; + +use WWW::Mechanize::Chrome; +use Log::Log4perl qw(:easy); +use Time::HiRes qw/ time sleep /; +use File::Temp 'tempdir'; + +# how often each url will be called +my @timesToCheck = (1..10); +# the urls to be called +my @urlsToCheck = ( + 'https://www.some.tld' + 'https://www.some.tld/with/path', + 'https://www.some-other.tld' +); + +my $mech = WWW::Mechanize::Chrome->new( + launch_exe => '/usr/bin/google-chrome-stable', # path to your chome + incognito => 1, + data_directory => tempdir(CLEANUP => 1 ), + launch_arg => [ + #"--headless", # headless or not + "--disk-cache-dir=/dev/null", + "--aggressive-cache-discard", + "--disable-gpu", + "--deterministic-mode", + "--disk-cache-size=1", + "--no-sandbox" + ], + cookie_jar => {} +); + +$mech->add_header( + 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/999.99 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36', + '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', + 'Accept-Charset' => 'iso-8859-1,*,utf-8', + 'Accept-Language' => 'en-US', + 'accept-encoding' => 'gzip, deflate, br', + 'Cache-Control' => 'no-cache', +); + + +foreach my $url(@urlsToCheck) { + my $displayUrl = substr($url, -70); + + for (@timesToCheck) { + my $start = time; + $mech->get($url.'?'.$start); + if ($mech->success()) { + my $time = time; + $time = ($time - $start); + + my $out = sprintf "%s %.4f sec \n", $displayUrl, $time; + print $out; + } + $mech->sleep( 2 ); + } +} +$mech->close(); \ No newline at end of file diff --git a/perl/perf-test.pl b/perl/perf-test.pl new file mode 100644 index 0000000..8c9c176 --- /dev/null +++ b/perl/perf-test.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl -w + +# 2020 by Johannes 'Banana' Keßler +# some improved simple page load check script. +# multiple URL and how ofthen they are accessed + +use warnings; +use strict; +use LWP::UserAgent; +use HTTP::Request; +use HTTP::Cookies; +use utf8; +use Time::HiRes qw/ time sleep /; + +my @urlsToCheck = ( + 'https://www.some.tld' + 'https://www.some.tld/with/path', + 'https://www.some-other.tld' +); +my @timesToCheck = (1..10); + +my $localtime = localtime; +my ($day,$month,$date,$hour,$year) = split /\s+/,scalar localtime; + +my $request_headers = [ + 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/999.99 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36', + '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', + 'Accept-Charset' => 'iso-8859-1,*,utf-8', + 'Accept-Language' => 'en-US', + 'accept-encoding' => 'gzip, deflate, br', + 'Cache-Control' => 'no-cache', +]; + +# +#$ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt")); +my $cookies = HTTP::Cookies->new(); +# set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest ) +$cookies->set_cookie(0,'name', 'value','/','www.some.tld'); + + +print "\n+" .('-' x 90) . "+\n"; +print "|", " Time: $hour",' ' x 75,"|\n"; +print "|", ' URL',' ' x 70,'TIME',' ' x 12,"|\n"; +print "+" .('-' x 90) . "+\n"; + + +foreach my $url(@urlsToCheck) { + for (@timesToCheck) { + check_url($url); + } +} + +sub check_url { + my ($target) = @_; + + my $ua = LWP::UserAgent->new; + $ua->cookie_jar($cookies); + my $req = HTTP::Request->new(GET => $target, $request_headers); + + my $start = time; + my $res = $ua->request($req); + + if ($res->is_success) { + my $time = time; + $time = ($time - $start); + my $displayUrl = substr($target, -70); + + my $out = sprintf "%-74s %.4f sec \n", $displayUrl, $time; + print $out; + } +} -- 2.39.5