VMware {code} Community
comiscience
Contributor
Contributor

It takes more than 25 seconds to get virtual machin base information, how can i improve this performance?

Hello everybody and specialists:

I have 5 virtual host in the same datacenter. I need to connect and get the list of ours virtual host first, and then get each host's virtual switchs' basic information and each host's datastores' basic information.

More clairely:

1, I need get the list of host which contains each host's name

2, for each host, we have 3 switches, and for each switche i need to get its portGroup information (name, ip adress, vlan id and so on).

3, for each host, we have several datasotres, and for each datastore i need to get its name, capacity, ip adress and so on.

I've used code like below:

(For connect to the server, i copy code from 'vSphere Web Service SDK 5.1 example' to my project)

        void CreateServiceRef(string svcRefVal)
        {
            // Manage bad certificates our way:
            System.Net.ServicePointManager.CertificatePolicy = new CertPolicy();
            _svcRef = new ManagedObjectReference();
            _svcRef.type = "ServiceInstance";
            // could be ServiceInstance for "HostAgent" and "VPX" for VPXd
            _svcRef.Value = svcRefVal;
        }
        void Connect(string url, string username, string password)
        {
            if (_service != null)
            {
                Disconnect();
            }
            _service = new VimService();
            _service.Url = url;
            _service.CookieContainer = new System.Net.CookieContainer();
            try
            {
                _sic = _service.RetrieveServiceContent(_svcRef);
            }
            catch(Exception e)
            {
                Disconnect();
                MessageBox.Show("Cannot connect : " + e.Message);
                throw e;
            }
            _propCol = _sic.propertyCollector;
            _rootFolder = _sic.rootFolder;
            if (_sic.sessionManager != null)
            {
                _service.Login(_sic.sessionManager, username, password, null);
            }
        }
      

(For get hostlist, i copy code from 'vSphere Web Service SDK 5.1 example' to my project)

        List<string> GetContainerContents()

        {

            // Create a Filter Spec to Retrieve Contents for...

            TraversalSpec rpToVm = new TraversalSpec();

            rpToVm.name = "rpToVm";

            rpToVm.type = "ResourcePool";

            rpToVm.path = "vm";

            rpToVm.skip = false;

            // Recurse through all ResourcePools

            TraversalSpec rpToRp = new TraversalSpec();

            rpToRp.name = "rpToRp";

            rpToRp.type = "ResourcePool";

            rpToRp.path = "resourcePool";

            rpToRp.skip = false;

            rpToRp.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec() };

            rpToRp.selectSet[0].name = "rpToRp";

            rpToRp.selectSet[1].name = "rpToVm";

            // Traversal through ResourcePool branch

            TraversalSpec crToRp = new TraversalSpec();

            crToRp.name = "crToRp";

            crToRp.type = "ComputeResource";

            crToRp.path = "resourcePool";

            crToRp.skip = false;

            crToRp.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec() };

            crToRp.selectSet[0].name = "rpToRp";

            crToRp.selectSet[1].name = "rpToVm";

            // Traversal through host branch

            TraversalSpec crToH = new TraversalSpec();

            crToH.name = "crToH";

            crToH.type = "ComputeResource";

            crToH.path = "host";

            crToH.skip = false;

            // Traversal through hostFolder branch

            TraversalSpec dcToHf = new TraversalSpec();

            dcToHf.name = "dcToHf";

            dcToHf.type = "Datacenter";

            dcToHf.path = "hostFolder";

            dcToHf.skip = false;

            dcToHf.selectSet = new SelectionSpec[] { new SelectionSpec() };

            dcToHf.selectSet[0].name = "visitFolders";

            // Traversal through vmFolder branch

            TraversalSpec dcToVmf = new TraversalSpec();

            dcToVmf.name = "dcToVmf";

            dcToVmf.type = "Datacenter";

            dcToVmf.path = "vmFolder";

            dcToVmf.skip = false;

            dcToVmf.selectSet = new SelectionSpec[] { new SelectionSpec() };

            dcToVmf.selectSet[0].name = "visitFolders";

            // Recurse through all Hosts

            TraversalSpec HToVm = new TraversalSpec();

            HToVm.name = "HToVm";

            HToVm.type = "HostSystem";

            HToVm.path = "vm";

            HToVm.skip = false;

            HToVm.selectSet = new SelectionSpec[] { new SelectionSpec() };

            HToVm.selectSet[0].name = "visitFolders";

            // Recurse thriugh the folders

            TraversalSpec visitFolders = new TraversalSpec();

            visitFolders.name = "visitFolders";

            visitFolders.type = "Folder";

            visitFolders.path = "childEntity";

            visitFolders.skip = false;

            visitFolders.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec() };

            visitFolders.selectSet[0].name = "visitFolders";

            visitFolders.selectSet[1].name = "dcToHf";

            visitFolders.selectSet[2].name = "dcToVmf";

            visitFolders.selectSet[3].name = "crToH";

            visitFolders.selectSet[4].name = "crToRp";

            visitFolders.selectSet[5].name = "HToVm";

            visitFolders.selectSet[6].name = "rpToVm";

            SelectionSpec[] selectionSpecs = new SelectionSpec[] { visitFolders, dcToVmf, dcToHf, crToH, crToRp, rpToRp, HToVm, rpToVm };

            PropertySpec[] propspecary = new PropertySpec[] { new PropertySpec() };

            propspecary[0].all = false;

            propspecary[0].allSpecified = true;

            propspecary[0].pathSet = new string[] { "name" };

            propspecary[0].type = "ManagedEntity";

            PropertyFilterSpec spec = new PropertyFilterSpec();

            spec.propSet = propspecary;

            spec.objectSet = new ObjectSpec[] { new ObjectSpec() };

            spec.objectSet[0].obj = _rootFolder;

            spec.objectSet[0].skip = false;

            spec.objectSet[0].selectSet = selectionSpecs;

            // Recursively get all ManagedEntity ManagedObjectReferences

            // and the "name" property for all ManagedEntities retrieved

            ObjectContent[] ocary =

               _service.RetrieveProperties(

                  _propCol, new PropertyFilterSpec[] { spec }

               );

            List<string> listHostSystem = new List<string>();

            //System.IO.File.Create("c:\\nodeinformations.txt");

            //System.IO.File.Exists(@"c:\\nodeinformations.txt");

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\\nodeinformations.txt"))

            {

                // If we get contents back. print them out.

                if (ocary != null)

                {

                    ObjectContent oc = null;

                    ManagedObjectReference mor = null;

                    DynamicProperty[] pcary = null;

                    DynamicProperty pc = null;

                    List<string> listVirtualMachine = new List<string>();

                    for (int oci = 0; oci < ocary.Length; oci++)

                    {

                        oc = ocary[oci];

                        mor = oc.obj;

                        pcary = oc.propSet;

                        Console.WriteLine("Object Type : " + mor.type);

                        file.WriteLine("Object Type : " + mor.type);

                        Console.WriteLine("Reference Value : " + mor.Value);

                        file.WriteLine("Reference Value : " + mor.Value);

                        if (pcary != null)

                        {

                            for (int pci = 0; pci < pcary.Length; pci++)

                            {

                                pc = pcary[pci];

                                Console.WriteLine("   Property Name : " + pc.name);

                                file.WriteLine("   Property Name : " + pc.name);

                                if (pc != null)

                                {

                                    if (!pc.val.GetType().IsArray)

                                    {

                                        Console.WriteLine("   Property Value : " + pc.val);

                                        file.WriteLine("   Property Value : " + pc.val);

                                        if (mor.type == "HostSystem")

                                        {

                                            listHostSystem.Add(pc.val.ToString());

                                        }

                                        else if (mor.type == "VirtualMachine")

                                        {

                                            listVirtualMachine.Add(pc.val.ToString());

                                        }

                                    }

                                    else

                                    {

                                        Array ipcary = (Array)pc.val;

                                        Console.WriteLine("Val : " + pc.val);

                                        file.WriteLine("Val : " + pc.val);

                                        for (int ii = 0; ii < ipcary.Length; ii++)

                                        {

                                            object oval = ipcary.GetValue(ii);

                                            if (oval.GetType().Name.IndexOf("ManagedObjectReference") >= 0)

                                            {

                                                ManagedObjectReference imor = (ManagedObjectReference)oval;

                                                Console.WriteLine("Inner Object Type : " + imor.type);

                                                file.WriteLine("Inner Object Type : " + imor.type);

                                                Console.WriteLine("Inner Reference Value : " + imor.Value);

                                                file.WriteLine("Inner Reference Value : " + imor.Value);

                                            }

                                            else

                                            {

                                                Console.WriteLine("Inner Property Value : " + oval);

                                                file.WriteLine("Inner Property Value : " + oval);

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }

                    Console.WriteLine("");

                    file.WriteLine("");

                    Console.WriteLine("Liste de Host");

                    file.WriteLine("Liste de Host");

                    foreach (var row in listHostSystem)

                    {

                        Console.WriteLine(row.ToString());

                        file.WriteLine(row.ToString());

                    }

                    Console.WriteLine("");

                    Console.WriteLine("Liste de VM Machine");

                    file.WriteLine("Liste de VM Machine");

                    foreach (var row in listVirtualMachine)

                    {

                        Console.WriteLine(row.ToString());

                        file.WriteLine(row.ToString());

                    }

                }

                else

                {

                    Console.WriteLine("No Managed Entities retrieved!");

                }

            }

            return listHostSystem;

        }

0 Kudos
1 Reply
comiscience
Contributor
Contributor

(Below is a part of code for get sotrage informations,  i copied code from 'vSphere Web Service SDK 5.1 example' to my project)

            // Connect to each host with cb of type AppUtil.AppUtil

       public static AppUtil.AppUtil cb = null;

            if (cb != null)

            {

                cb.disConnect();

                cb = null;

            }

            cb = AppUtil.AppUtil.initialize("getOnlyStorageList"

                                            , constructOptions()

                                            , args);

            cb.connect();

            vmUtils = new VMUtils(cb);

            validate();

        private void getStorageInfo(ObservableCollection<HostStorageInfoDataStructure> storageList, string host, string datacenter)
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;
            try
            {
                if (((datacenter != null) && (host != null))
                    || ((datacenter != null) && (host == null)))
                {
                    dcmor
                        = cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", datacenter);
                    if (dcmor == null)
                    {
                        Console.WriteLine("Datacenter not found");
                        return;
                    }
                    hostfoldermor = vmUtils.getHostFolder(dcmor);
                    hostmor = vmUtils.getHost(hostfoldermor, host);
                }
                else if ((datacenter == null) && (host != null))
                {
                    hostmor = vmUtils.getHost(null, host);
                }
                if (hostmor != null)
                {                                              
                    ////////////////////Stockage////////////////////////////////
                    Object cobj = cb.getServiceUtil().GetDynamicProperty(hostmor, "datastore");
                    ManagedObjectReference[] datastores = (ManagedObjectReference[])cobj;
                    for (int i = 0; i < datastores.Length; i++)
                    {
                        VimApi.DatastoreSummary storageSummaryInfo = (VimApi.DatastoreSummary)cb.getServiceUtil().GetObjectProperties(cb.getConnection().ServiceContent.propertyCollector, datastores[i], new String[] { "summary" })[0].propSet[0].val;
                        storageList.Add(new HostStorageInfoDataStructure()
                        {
                            StorageName = storageSummaryInfo.name,
                            Host = host
                        });
                    }
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("ResourceInUseFault"))
                {
                    MessageBox.Show("There are virtual network adapters "
                                + "associated with the virtual switch.");
                    EXOE.Tools.LoggerExoe.Error("There are virtual network adapters "
                                + "associated with the virtual switch.");
                    EXOE.Tools.LoggerExoe.Error(e.Message);
                }
                else if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    MessageBox.Show(cb.getAppName() + " : Failed : virtual switch cannot be found. ");
                    EXOE.Tools.LoggerExoe.Error(cb.getAppName() + " : Failed : virtual switch cannot be found. ");
                    EXOE.Tools.LoggerExoe.Error(e.Message);
                }
                else if (e.Detail.FirstChild.LocalName.Equals("HostConfigFault"))
                {
                    MessageBox.Show(cb.getAppName() + " : Failed : Configuration failures. ");
                    EXOE.Tools.LoggerExoe.Error(cb.getAppName() + " : Failed : Configuration failures. ");
                    EXOE.Tools.LoggerExoe.Error(e.Message);
                }
                else
                {
                    EXOE.Tools.LoggerExoe.Error(e.Message);
                    throw e;
                }
            }
            catch (Exception e)
            {
                EXOE.Tools.LoggerExoe.Error(e.Message);
                throw e;
            }
        }

The total time it costed from connecting to server to getting need information is more than 35 seconds.

How could it be so slow?

And if i do just get each host's Datastore without operation of get host list. It still take more than 20 minute.  And in my test, i have only 5 hosts! And each host has maximum 12 datastore....

Thanks very much!!

0 Kudos