]> git.proxmox.com Git - pve-apiclient.git/commitdiff
perftest1.pl: another example
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 28 Dec 2016 10:35:30 +0000 (11:35 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 28 Dec 2016 10:35:30 +0000 (11:35 +0100)
Makefile
debian/changelog
examples/perftest1.pl [new file with mode: 0755]

index 08b1ce4ba77109c2025a16cbdab02bfff37a36b6..79478808196f761b92dc8a79ae30f43993b992eb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ install:
        install -d -m 755 ${DOCDIR}/examples
        install -m 0755 examples/example1.pl ${DOCDIR}/examples
        install -m 0755 examples/example2.pl ${DOCDIR}/examples
+       install -m 0755 examples/perftest1.pl ${DOCDIR}/examples
 
 .PHONY: upload
 upload: ${DEB}
index 81d3dea034d931d4c332460a2278422c3680312c..bd9cb3c966922c31f9f4d0d7f8d827db4534d150 100644 (file)
@@ -1,6 +1,6 @@
 libpve-apiclient-perl (1.0-2) unstable; urgency=medium
 
-  * add simple example code: example1.pl and example2.pl
+  * add simple example code: example1.pl, example2.pl and perftest1.pl
 
  -- Proxmox Support Team <support@proxmox.com>  Wed, 28 Dec 2016 10:46:52 +0100
 
diff --git a/examples/perftest1.pl b/examples/perftest1.pl
new file mode 100755 (executable)
index 0000000..17f1a91
--- /dev/null
@@ -0,0 +1,96 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# NOTE: you need to run this on a PVE host, or modify the source to
+# provide username/password/hostname from somewhere else.
+
+use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
+
+use PVE::APIClient::LWP;
+use PVE::AccessControl;
+use PVE::INotify;
+use JSON;
+
+# normally you use username/password,
+# but we can simply create a ticket and CRSF token if we are root
+# running on a pve host
+
+my $hostname = PVE::INotify::read_file("hostname");
+my $ticket = PVE::AccessControl::assemble_ticket('root@pam');
+my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token('root@pam');
+
+my $wcount = 10;
+my $qcount = 100;
+
+sub get_local_cert_fingerprint { my ($node) = @_; my $cert_path =
+    "/etc/pve/nodes/$node/pve-ssl.pem"; my $custom_cert_path =
+    "/etc/pve/nodes/$node/pveproxy-ssl.pem";
+
+    $cert_path = $custom_cert_path if -f $custom_cert_path;
+
+    my $bio = Net::SSLeay::BIO_new_file($cert_path, 'r'); my $cert =
+    Net::SSLeay::PEM_read_bio_X509($bio); Net::SSLeay::BIO_free($bio);
+
+    my $fp = Net::SSLeay::X509_get_fingerprint($cert, 'sha256'); die
+    "got empty fingerprint" if !defined($fp) || ($fp eq '');
+
+    return $fp; }
+
+my $local_fingerprint = get_local_cert_fingerprint($hostname);
+
+sub test_rpc {
+    my ($host) = @_;
+
+    my $conn = PVE::APIClient::LWP->new(
+       #username => 'root@pam',
+       #password => 'yourpassword',
+       ticket => $ticket,
+       csrftoken => $csrftoken,
+       host => $host,
+       # add local hosts cert fingerprint
+       cached_fingerprints => {
+           $local_fingerprint => 1,
+    });
+
+    for (my $i = 0; $i < $qcount; $i++) {
+       eval {
+           my $res = $conn->get("/", {});
+       };
+       if (my $err = $@) {
+           print "ERROR: $err\n";
+           last;
+       }
+    }
+}
+
+sub run_tests {
+    my ($host) = @_;
+    
+    my $workers;
+
+    my $starttime = [gettimeofday];
+
+    for (my $i = 0; $i < $wcount; $i++) {
+       if (my $pid = fork ()) {
+           $workers->{$pid} = 1;
+       } else {
+           test_rpc($host);
+           exit (0);
+       }
+    }
+
+    # wait for children
+    1 while (wait > 0);
+
+    my $elapsed = int(tv_interval ($starttime) * 1000);
+
+    my $tpq = $elapsed / ($wcount*$qcount);
+
+    print "$host: $tpq ms per query\n";
+}
+
+run_tests("localhost"); # test 'pvedaemon'
+
+run_tests($hostname); # test 'pveproxy'