Are your Perl scripts running slow? Almost certainly there is room for improvement.
I have found several articles dealing with specific problems, but only one dedicated to performance optimalization. It was written by Reuben Stump and it turned out to be an invaluable resource for my later work.
Fetch only what you need
One of the key parts of working with vSphere Perl SDK is obtaining references to so-called Managed Entities — virtual machines, folders or other items in your vSphere inventory. Each entity contains a list of child properties which can be quite extensive. All those properties are what gives vSphere SDK its power, however with great power comes... great data transfer.
Reuben mentions two simple ways of speeding-up your scripts:
- Always limit the property set when obtaining your entities using the property attribute.
- Avoid calling Vim::get_view(s) functions in loop structures.
I'll elaborate a bit more on this topic, however these two should be carved in stone. To see some real life examples, let us create a script, which simply prints out a list of all VMs' names.
#!/usr/bin/perl -w use strict; use warnings; use VMware::VIRuntime; Opts::parse(); Opts::validate(); Util::connect(); my $vms = Vim::find_entity_views(view_type => 'VirtualMachine'); print $_->name . "\n" foreach (@$vms);
The script obtains all properties for all VMs and in my environment it took 4.177 seconds to execute. However, simply by limiting the property set, we can get it down to 2.154 seconds.
my $vms = Vim::find_entity_views( view_type => 'VirtualMachine', properties => ['name'] );
That's great, by modifying a single line of code we've shaved off half of the time!
But two seconds still feel too slow. Is it possible to do even better than that? Yes it is.
Re-use existing sessions
As it turns out, a lot of those two seconds is actually spent by the login action. When running the test, I was using the server + username + password combo to log in to vSphere. In such case, vSphere creates a new session for my script, which can take a considerable amount of time.
$ time ./test.pl --server esxi --username root --password pass
When calling your scripts repeatedly, it is possible to save a lot of time by caching your existing session and reusing it. It also saves some of vSphere's precious resources.
$ /usr/share/doc/vmware-vcli/samples/session/save_session.pl \ --server esxi --username root --password pass --savesessionfile sessfile $ time ./test.pl --sessionfile sessfile
Now the actual script only takes 1.342 seconds!
Optimizing the login process can knock down a full second depending on your environment. In my experience, it is especially helpful when working with busy vCenters, less so for standalone ESXi hosts. By default, the session durability is 30 (vSphere 5.1) or 120 minutes (vSphere 5.5 and newer).
When creating an application which periodically logs in to vCenter, it makes a lot of sense to create a cron task to keep a live session. Alternatively, it is possible to build some caching into your application. I have created a simple Perl module to solve these routine tasks for me and I'll be happy to share it in one of the following blogposts.
Conclusion
- Always measure individual parts of your script, eg. using Perl's Time::HiRes module.
- Avoid fetching unnecessary data.
- Utilize caching as much as you can.
- Keep an open vSphere session to minimize login time.
When used correctly, the Perl SDK should feel much faster than Powershell.

Comments
Post a Comment