VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts)
1 2 Previous Next 24 Replies Last post: Dec 8, 2008 6:01 AM by Engelsman  

really slow performance or bad code? posted: Mar 17, 2008 5:27 AM

Click to view VirtualNoitall's profile Virtuoso 1,856 posts since
Feb 17, 2006

Hello,

I am no programmer so feel free to sharply criticize the following code without holding back -):

foreach ($ds in Get-Datastore -Name datastore*) {
$a = 0
foreach ($vm in get-VM -Datastore $ds){
$a++}
Write-Host $ds.Name " VMs: " $a " Capacity: " $ds.CapacityMB " Free: " $ds.FreeSpaceMB
}

All I want to to is iterate through the datstores and find out the number of VMs, the free space and the capacity for each. The later two are actually available by just querying the datastore but I don't think the first is so I wrote what you see above. I have probably 100 datastore of which roughly half follow the naming scheme that would include them in the query. This seemed pretty straigh forward but the query takes over an hour to run :-(

The long running part is the querying of each included datastore for VMs so I can count them. Does the beta perform poorly or is there a better way to get what I am after?

Thanks!


Re: really slow performance or bad code?

1. Mar 17, 2008 6:00 AM in response to: VirtualNoitall
Click to view LucD's profile Champion vExpert 3,066 posts since
Oct 31, 2005
Since you only want to count the number of VMs you could replace the inner foreach loop by

$a = (Get-vm -Datastore $ds).Length

Re: really slow performance or bad code?

3. Mar 17, 2008 6:37 AM in response to: VirtualNoitall
Click to view LucD's profile Champion vExpert 3,066 posts since
Oct 31, 2005
Out of curiousity, does a Browse Datastore from the VI Vlient also take 1-2 minutes before it displays all folders ?

Re: really slow performance or bad code?

5. Mar 17, 2008 6:50 AM in response to: VirtualNoitall
Click to view LucD's profile Champion vExpert 3,066 posts since
Oct 31, 2005
We don't have that many datastores and guests in the PowerShell test environment but the scripted version "feels" a bit slower compared to the VI Client.
But we're talking seconds not minutes.

Re: really slow performance or bad code?

6. Mar 17, 2008 6:55 AM in response to: LucD
Click to view halr9000's profile Master vExpert 847 posts since
Jun 7, 2007
Guys, let's get some hard stats? It's very easy to wrap your code in this:
{noformat}measure-command { # get-datastore stuff }{noformat}


I've gotta reboot but once I'm back up I'll get some times. Get-VM can be pretty slow as well.






Hal Rottenberg
Co-Host, PowerScripting Podcast (http://powerscripting.net)

Re: really slow performance or bad code?

7. Mar 17, 2008 7:05 AM in response to: halr9000
Click to view LucD's profile Champion vExpert 3,066 posts since
Oct 31, 2005
2.5 to 2.7 secs for each 300Gb datastore with +/- 10 guests per datastore.

Re: really slow performance or bad code?

10. Mar 17, 2008 8:03 AM in response to: VirtualNoitall
Click to view halr9000's profile Master vExpert 847 posts since
Jun 7, 2007
It takes me about 4 seconds to get-datastore. I only have 10. Running Get-VM takes 11 seconds for 186 VMs.




Hal Rottenberg
Co-Host, PowerScripting Podcast (http://powerscripting.net)

Re: really slow performance or bad code?

12. Mar 17, 2008 8:23 AM in response to: halr9000
Click to view halr9000's profile Master vExpert 847 posts since
Jun 7, 2007
The most time-intensive operation by far seems to be the get-vm -datastore $ds. Mine is still running... Ok, the whole script took 2:30 on my VI. Other notes:

  • Querying a DAS datastore on an ESX server with zero VMs took 10.624 seconds. Command performed: 'Get-VM -datastore $ds[0]'
  • The time was very close (11.645 seconds) on another DAS datastore, this one connected to a fully-loaded ESX server.

Hal Rottenberg
Co-Host, PowerScripting Podcast (http://powerscripting.net)

Re: really slow performance or bad code?

13. Mar 17, 2008 8:41 AM in response to: halr9000
Click to view halr9000's profile Master vExpert 847 posts since
Jun 7, 2007
Ooh, cool. The beta code makes much easier something which was pretty difficult before. This version took 29.41 seconds versus 2:30 before. You guys try it. Of course, this begs the question--why do we have to do this?

{noformat}
$ds = Get-Datastore
$output = @()
$ds | ForEach-Object {
	$view = Get-View $_.id
	$process = "" | Select-Object DataStore,FreeSpaceMB,VMCount
	$process.datastore = $view.info.Name
	$process.freespacemb = $view.info.freespace / 1MB
	# and here's the best part
	$process.vmcount = $view.vm.length
	$output += $process
}
Write-Output $output
{noformat}

Hal Rottenberg
Co-Host, PowerScripting Podcast (http://powerscripting.net)

VMware Beta Programs

Want to be Considered for Future Beta Programs?

Learn More

VMware Developer

Download SDKs, APIs, videos,
training, and more in the Developer community.

Learn More

Developer
Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld
Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

Only VMware ... Delivers Nexus 1000V

Ensure consistent, policy-based network capabilities to virtual machines across your data center.

Learn More

Communities