VMware Cloud Community
Najtsob
Enthusiast
Enthusiast

C# and VI.net toolkit, slow FindEntityView and Connect

Hi!

This is part of my code and "client.Connect(...)" takes about 6 seconds to execute on local network. Is this normal ?

client = new VimClient();
client.Connect(host, CommunicationProtocol.Https, 443);
UserSession session = client.Login(username, password);
if (session == null)
    throw new Exception("Error connecting to VC " + host);

Also "client.FindEntityView(...)" takes obut 10 seconds when called first time. Subsequent calls takes just about 0,8 second. Is this normal too ?

NameValueCollection filter = new NameValueCollection();
filter.Add("Summary.vm", VMId);

VirtualMachine vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, filter, null);

I'm using C# and .net 3.5sp1. Is there any way to speed up things ?

Thank you for your help.

0 Kudos
12 Replies
harkamal
Expert
Expert

1. Connection

- are you handling the ssl certificate exception, that might slow down the connection for few seconds. There's an example in sdk examples on handling the ssl certificate.

2. Find EntityView

- One way to expedite FindEntityView would be to tell it the starting point from where to start its search, otherwise it searches everything until it finds the target

Najtsob
Enthusiast
Enthusiast

I looked into sample code and there I spoted big difference betwen sample code and my code.

_service = new VimService();
_service.Url = url;
_service.CookieContainer = new System.Net.CookieContainer();

_sic = _service.RetrieveServiceContent(_svcRef);
_propCol = _sic.propertyCollector;
_rootFolder = _sic.rootFolder;

if (_sic.sessionManager != null) {
    _service.Login(_sic.sessionManager, username, password, null);
}

client = new VimClient();
client.Connect(host, CommunicationProtocol.Https, 443);
UserSession session = client.Login(username, password);
if (session == null)
    throw new Exception("Error connecting to VC " + host);


So I'm using wrong/other SDK ?

Thank you for your help. You are life saver Smiley Happy

0 Kudos
harkamal
Expert
Expert

Okay, now i recall all horrors from the past.

You have seen the sdk and code, which is not easy to work with. Especially an amateur like me could not work through it when i started 2 years back.

Luckily, it got to see Carter's presentation on .Net WRAPPER dll that they supplied as part of PowerCLI, it has got all sdk functionality wrapped around for visual studio development. I have been using it since then until now.

http://www.slideshare.net/vmwarecarter/using-vi-toolkit-for-windows-from-net

Have a full view of the presentation, and you will know more.

Cheers

Najtsob
Enthusiast
Enthusiast

I downloaded "VMware-vSphere-WS-SDK-4.1.0-257238.zip" and there are docs and samples just for web service based SDK.

Are there any simillar docs and samples for using VMware.Vim asembly ?

Best regards.

0 Kudos
LucD
Leadership
Leadership

Did you already have a look at the documents that are installed when you install PowerCLI ?

There is a CHM file, called vSphere SDK for .Net API Reference, and a PDF file, called vSphere SDK for .NET Developer’s Guide.

They are not very extensive and don't have a lot of samples but they give a good starting point.

And then there are Carter's slides, reference see above, that give valuable information.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
harkamal
Expert
Expert

Have u seen the presentation? I guess it's a jump start.

Sent from my iPhone

0 Kudos
ykalchev
VMware Employee
VMware Employee

Hi,

About speed up of the calls .. Do you run the code in the Visual Studio?

These delays usually are caused when you run code in debug mode. VS loads vSphere web service proxy assemblies (VimService.dll and VimService.Serialializers.dll ) with the debug information on first RetrieveProperties call and this cause the significant difference in the execution time.

Regards,

Yasen Kalchev

PowerCLI Dev team

Yasen Kalchev, vSM Dev Team
0 Kudos
Najtsob
Enthusiast
Enthusiast

Yes I looked at thoose files and they are great for start.

Now I want learn more about good practices and need to know if functions are thread safe, etc.

And about original question:

"FindEntityView(...)" function still takes about 10 seconds when called  first time. As sugested I added starting point from where to start its search.


What are your experiances about speed of this funcion ?

0 Kudos
LucD
Leadership
Leadership

Did you Yasen's reply ? Are you running in debug mode ?

I'm afraid that there isn't much , besides the earlier resources mentioned in this thread.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Najtsob
Enthusiast
Enthusiast

Sorry for late response.

Yes I saw ykalchev's response and I'm running standalone exe built in release mode. I still want to know how fast are these functions in your examples.

0 Kudos
Najtsob
Enthusiast
Enthusiast

Below is my sample code. First time when connecting to VC it takes about 6s and second time it takes under a second.

Searching for resource pool takes about 5 sec and then about 0,5s for VM's from this pool. In second run takes about 0,5s to find each VM without resource pool as starting point.

Why is second run so much faster and what do I need to cache if I always want this speed ?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using VMware.Vim;

using System.Collections.Specialized;

namespace VcTest

{

    class Program

    {

        public static VimClient client;

        static void Main(string[] args)

        {

            string host = "hostname";

            string user = "username";

            string pass = "password";

            string resourcePool = "resource pool name";

            connectToVc(host, user, pass);

            ResourcePool rp = getRP(resourcePool);

            string[] VMs = { "vm-24657", "vm-24705", "vm-24682", "vm-21925", "vm-26977" }; //five random selected vm's from resourcePool

            foreach (string VMid in VMs)

            {

                getVM(VMid, rp.MoRef);

            }

            client.Disconnect();

            client = null;

            Console.WriteLine("Round two \n");

            System.Threading.Thread.Sleep(5000);

            connectToVc(host, user, pass);

            foreach (string VMid in VMs)

            {

                getVM(VMid, null);

            }

            client.Disconnect();

            Console.Read();

        }

        public static void connectToVc(string host, string user, string pass)

        {

            Console.WriteLine("Connecting...");

            client = new VimClient();           

            DateTime start = DateTime.Now;

            ServiceContent sc = client.Connect(host, CommunicationProtocol.Https, 443);

            UserSession session = client.Login(user, pass);

            DateTime stop = DateTime.Now;

            Console.WriteLine("Time elapsed: " + (stop - start).TotalMilliseconds);

        }

        public static void getVM(string VMId, ManagedObjectReference searchStart)

        {

            NameValueCollection filter = new NameValueCollection();

            filter.Add("Summary.vm", VMId);

            DateTime start = DateTime.Now;

            VirtualMachine vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), searchStart, filter, null);

            DateTime stop = DateTime.Now;

            Console.WriteLine("Searching for VM, name: " + vm.Name + " id: " + VMId + " took " + (stop - start).TotalMilliseconds + "ms\n");

        }

        public static ResourcePool getRP(string RPname)

        {

            NameValueCollection RPfilter = new NameValueCollection();

            RPfilter.Add("name", RPname);

            DateTime start = DateTime.Now;

            ResourcePool rp = (ResourcePool)client.FindEntityView(typeof(ResourcePool), null, RPfilter, null);

            DateTime stop = DateTime.Now;

            Console.WriteLine("Searching for Resource Pool, name: " + rp.Name + " took " + (stop - start).TotalMilliseconds + "ms\n");

            return rp;

        }

    }

}

Best regard, Primoz.

0 Kudos