]> git.proxmox.com Git - pve-apiclient.git/blob - examples/perftest1.pl
perftest1.pl: another example
[pve-apiclient.git] / examples / perftest1.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 # NOTE: you need to run this on a PVE host, or modify the source to
7 # provide username/password/hostname from somewhere else.
8
9 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
10
11 use PVE::APIClient::LWP;
12 use PVE::AccessControl;
13 use PVE::INotify;
14 use JSON;
15
16 # normally you use username/password,
17 # but we can simply create a ticket and CRSF token if we are root
18 # running on a pve host
19
20 my $hostname = PVE::INotify::read_file("hostname");
21 my $ticket = PVE::AccessControl::assemble_ticket('root@pam');
22 my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token('root@pam');
23
24 my $wcount = 10;
25 my $qcount = 100;
26
27 sub get_local_cert_fingerprint { my ($node) = @_; my $cert_path =
28 "/etc/pve/nodes/$node/pve-ssl.pem"; my $custom_cert_path =
29 "/etc/pve/nodes/$node/pveproxy-ssl.pem";
30
31 $cert_path = $custom_cert_path if -f $custom_cert_path;
32
33 my $bio = Net::SSLeay::BIO_new_file($cert_path, 'r'); my $cert =
34 Net::SSLeay::PEM_read_bio_X509($bio); Net::SSLeay::BIO_free($bio);
35
36 my $fp = Net::SSLeay::X509_get_fingerprint($cert, 'sha256'); die
37 "got empty fingerprint" if !defined($fp) || ($fp eq '');
38
39 return $fp; }
40
41 my $local_fingerprint = get_local_cert_fingerprint($hostname);
42
43 sub test_rpc {
44 my ($host) = @_;
45
46 my $conn = PVE::APIClient::LWP->new(
47 #username => 'root@pam',
48 #password => 'yourpassword',
49 ticket => $ticket,
50 csrftoken => $csrftoken,
51 host => $host,
52 # add local hosts cert fingerprint
53 cached_fingerprints => {
54 $local_fingerprint => 1,
55 });
56
57 for (my $i = 0; $i < $qcount; $i++) {
58 eval {
59 my $res = $conn->get("/", {});
60 };
61 if (my $err = $@) {
62 print "ERROR: $err\n";
63 last;
64 }
65 }
66 }
67
68 sub run_tests {
69 my ($host) = @_;
70
71 my $workers;
72
73 my $starttime = [gettimeofday];
74
75 for (my $i = 0; $i < $wcount; $i++) {
76 if (my $pid = fork ()) {
77 $workers->{$pid} = 1;
78 } else {
79 test_rpc($host);
80 exit (0);
81 }
82 }
83
84 # wait for children
85 1 while (wait > 0);
86
87 my $elapsed = int(tv_interval ($starttime) * 1000);
88
89 my $tpq = $elapsed / ($wcount*$qcount);
90
91 print "$host: $tpq ms per query\n";
92 }
93
94 run_tests("localhost"); # test 'pvedaemon'
95
96 run_tests($hostname); # test 'pveproxy'