5

I'm trying to update some information on some printers on a remote server. I need to update stuff like location, comment and port. I have a solution that works, but I find it very slow, and I was wondering if anyone have any idea of why.

I get the printer (and later the port) from the server through WMI with a code like this: (this is testing code)

var test = DateTime.Now;
ManagementScope scope3 = new ManagementScope("\\\\printserver\\root\\cimv2");
scope3.Connect();
SelectQuery q3 = new SelectQuery("select * from Win32_Printer WHERE Name = 'printername'");
ManagementObjectSearcher search3 = new ManagementObjectSearcher(scope3, q3);
var printers3 = search3.Get();
foreach(var p in printers3)
{
    //do stuff with printer here.
}
var test2 = DateTime.Now.Subtract(test).TotalSeconds;

When it is done test2 will contain "33.something" seconds. If I do it without the where clause, it will take almost the same time. Admittedly there are almost 1500 printers on this server, but I feel like I should be able to query for one specific printer in a faster way, and I do not understand why a query with a where clause on the name of the printer takes the same time as a "select all" query.

Any suggestions?

--

Update

As suggested below, I have tried to run the same query multiple times. Still takes the same amount of time. It just feels weird to me, that Windows would need to "touch" every single printer on the system, when I'm searching for a specific one.

Kaspar Kjeldsen
  • 936
  • 1
  • 13
  • 30

1 Answers1

3

Admittedly there are almost 1500 printers on this server

That's the nasty little detail of course. It takes 33 / 1500 = 0.022 seconds per printer. That's a pretty magic number in computers, about how long it takes to open a file on a spindle disk.

There is a simple test you can perform to check if it is indeed the disk that's the bottleneck. Just run your query a second time, right after the slow one. File info will now be cached in the file system cache, it should execute in less than a second. You probably already did this, update your question with what you found out.

Very little you can do about this in software of course. The server needs better hardware to keep you happy. An SSD is very nice, should speed this up by an easy factor of 20. Lots more RAM can help but is not the golden solution. Poking the server more often so it keeps the file data in the cache is a workaround of sorts, not very friendly to the server.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536