]> git.proxmox.com Git - pve-cluster.git/blob - data/test/scripts/benchmark-config-get-property.pl
add some dev test scripts for evaluating a few IPC calls hands on
[pve-cluster.git] / data / test / scripts / benchmark-config-get-property.pl
1 #!/usr/bin/perl
2
3 use lib '../../';
4
5 use strict;
6 use warnings;
7
8 #use Data::Dumper;
9 use Time::HiRes qw( gettimeofday tv_interval );
10
11 use PVE::Tools;
12 use PVE::Cluster;
13 use PVE::QemuConfig;
14 use PVE::LXC::Config;
15
16 sub sec_to_unit {
17 my $sec = shift;
18
19 my $unit_index = 0;
20 while ($sec < 1) {
21 $sec *= 1000;
22 $unit_index++;
23 }
24
25 my $unit = @{['s', 'ms', 'us', 'ns', 'ps']}[$unit_index];
26
27 return wantarray ? ($sec, $unit) : "$sec $unit";
28
29 }
30
31 my $results = {};
32
33 sub perf {
34 my ($name, $loops, $code) = @_;
35
36 return if !defined($loops) || $loops <= 0;
37
38 my $loop = 0;
39 eval {
40 my $t0 = [gettimeofday];
41
42 for (my $i = 0; $i<$loops; $i++) {
43 $code->();
44 }
45
46 my $elapsed = tv_interval ($t0, [gettimeofday]);
47
48 my $total = sec_to_unit($elapsed);
49 my $per_loop = $elapsed/$loops;
50 $loop = sec_to_unit($per_loop);
51
52 $results->{$name} = [ $elapsed * 1000, $per_loop * 1000 ];
53
54 print STDERR "elapsed['$name' x $loops]: $total => $loop/loop\n";
55 }; warn $@ if $@;
56
57 return $loop;
58 }
59
60 my $loops = shift // 3;
61 my $vmid = shift // 0;
62 my $prop = shift // 'lock';
63
64 perf('cfg-get-prop', $loops, sub {
65 my $res = PVE::Cluster::get_guest_config_property($prop, $vmid);
66 });
67
68 PVE::Cluster::cfs_update();
69 perf('perl-manual', $loops, sub {
70 my $res = {};
71
72 # modeled after the manager API cluster/resource call
73 my $vmlist = PVE::Cluster::get_vmlist() || {};
74 my $idlist = $vmlist->{ids} || {};
75 foreach my $vmid (keys %$idlist) {
76
77 my $data = $idlist->{$vmid};
78 my $typedir = $data->{type} eq 'qemu' ? 'qemu-server' : 'lxc';
79
80 my $conf = PVE::Cluster::cfs_read_file("nodes/$data->{node}/$typedir/$vmid.conf");
81
82 my $v = $conf->{$prop};
83 $res->{$vmid} = { $prop => $v } if defined($v);
84 }
85 });
86 #PVE::Cluster::get_tasklist('dev5');
87
88 my $a = $results->{'cfg-get-prop'};
89 my $b = $results->{'perl-manual'};
90 printf("$loops\t%.2f\t%.2f\t%.2f\t%.2f\n", $a->[0], $a->[1], $b->[0], $b->[1]);
91
92 #print "res: " . Dumper($res) ."\n";