Friday 24 February 2012

IsolatedStorage performance tests on WP7

The documentation on IsolatedStorage seems to be incomplete on the subject of when you should open and close the ApplicationStore, particularly since the only the public static members are thread safe.

It would appear that the best performance might be to open the IsolatedStorage using IsolatedStorageFile.GetUserStoreForApplication() once for the application, and share the instance, but what operations are allowed across multiple threads if you do?  Well, that isn’t clear.

So, the safest thing to do is to open one every time you need it, and dispose of it when finished.

I’ve already verified that each time you call GetUserStoreForApplication, a different object is returned, and built a harness that does this:

isf = IsolatedStorageFile.GetUserStoreForApplication();

int v = System.Environment.TickCount;

for (int i = 0;i<1000;i++)
{

CheckFolderBare("Shared");
}

int v2 = System.Environment.TickCount - v;
v = System.Environment.TickCount;

for (int i= 0;i<1000;i++)
{
CheckFolderSlow("Shared");
}
int v3 = System.Environment.TickCount - v;

MessageBox.Show(string.Format("Fast: {0}ms\nSlow {1}ms", v2, v3));


My Implementations of CheckFolderBare and CheckFolderSlow are this:


private bool CheckFolderBare(string s)
{
return isf.DirectoryExists(s);
}

private bool CheckFolderSlow(string s)
{
using (var isf2 = IsolatedStorageFile.GetUserStoreForApplication())
{
return isf2.DirectoryExists(s);
}
}

 

Not surprisingly, my initial suspicions were correct.  It isn’t particularly cheap to create and dispose of the file (taking about 1.3ms).  Timings were:

Fast: 2787ms

Slow: 4086ms

 

How about enumerating the folders?  To do this, I create a folder in IsolatedStorage named Logs, and created in that folder 7 additional folders, and then ran these functions 1000 times:

private string[] FoldersSlow()
{
using (var isf2 = IsolatedStorageFile.GetUserStoreForApplication())
{
return isf2.GetDirectoryNames("Logs/*.*");
}
}

private string[] FoldersFast()
{
return isf.GetDirectoryNames("Logs/*.*");
}



Results differed by about 2.5ms:


Fast: 7619ms
Slow: 9100ms


So, to make things more interesting, I decided to create and delete a folder on each operation, so on odd calls, I remove the folder Logs/Bogus and on even calls I created it.


Surprisingly, I ended up with times differing by 1.3ms, and in some cases, not showing any difference at all.


Fast: 16406ms
Slow: 17542ms


Verdict?


On balance, I’d say there is no real performance penalty by opening and closing the IsolatedStorage in a realistic usage scenario.

No comments:

Post a Comment