Thom Lawrence

Entries from September 2006

Perfidy: Performance Testing In NUnit

September 17, 2006 · No Comments

Unsatisfied with calling QueryPerformanceCounter and being done with it, I spent today coming up with a fluent interface for performance testing in NUnit:

using (At.Most(5).Seconds)
{
    // Stuff that should take under 5 seconds
}

using (At.Most(1).Minute.GiveOrTake(10).Seconds)
{
    // Stuff that should take under 70 seconds
}

Using.At.Most(10).Seconds.For(1000).Iterations.Do(delegate()
{
    // Stuff that should take under 10 seconds for 1000 iterations
});

Using.At.Most(5).Seconds.For(20).Threads.Do(delegate()
{
    // Stuff that should take under 5 seconds when run inside
    // 20 parallel threads
});

It’s basically a wrapper around NUnit’s Assert.Less() and I call it Perfidy (which I thought was a good word for code that treacherously refuses to pass its tests). You can grab a very rough version here: Perfidy.zip

I suppose this kind of thing doesn’t really qualify as unit testing, and it’s not deterministic. But if you’re fairly generous with the limits and get your tests up and running early in a project, you at least know vaguely where you’re going to suffer.

I’ve not included any tests for the tests. I wasn’t sure if NUnit gives you a way to say ‘give me a green bar if this test fails’, and to test everything takes a fair coffee break, as there are lots of Thread.Sleep()s around. If anyone spots anything obviously wrong, or knows of another library that does the same sort of thing, I’d love to hear about it. Especially if it has pretty syntax. :)

Categories: C# · Code · Programming
Tagged: , , , ,

NHibernate-friendly Types

September 11, 2006 · No Comments

It’s probably a combination of laziness and plain bad form to try and map too many BCL types to your database, but if you’re interested, I’ve gone through a few important assemblies and compiled a list of classes that:

  • Are visible outside their assembly
  • Are neither abstract nor sealed
  • Are non-generic
  • Have a public or protected parameterless constructor

My assumptions might be wrong here, but I expect this to be enough for NHibernate to be able to save objects of those classes, and more importantly load and proxy them.

This is the list: PersistableTypes.txt. And here’s the code:

class Program
{
        static void Main(string[] args)
        {
            foreach(string assemblyName in GetAssemblyNames())
            {
                Assembly assembly = Assembly.Load(assemblyName);
                Type[] types = assembly.GetTypes();
                foreach (Type t in types)
                {
                    if (IsProxiable(t))
                        Console.WriteLine(t.FullName);
                }
            }
        }

        private static string[] GetAssemblyNames()
        {
            return new string[]
            {
                "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                "System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            };
        }

        private static bool IsProxiable(Type t)
        {
            return !t.IsAbstract && !t.IsSealed && t.IsVisible && !t.IsGenericType
                && HasProxiableInitializer(t);
        }

        private static bool HasProxiableInitializer(Type t)
        {
            return Array.Exists(t.GetConstructors(),
                IsProxiableInitializer);
        }

        private static bool IsProxiableInitializer(ConstructorInfo ctor)
        {
            return ctor.GetParameters().Length == 0 &&
                (ctor.IsPublic || ctor.IsFamily);
        }
    }

Should be some fun stuff in there - I wonder if a Web Form and the complete hierarchy of its controls can be persisted nicely?

Categories: C# · Code · Programming
Tagged: , , ,

ObjectSpaces Xml Documentation

September 11, 2006 · No Comments

Does anyone else have System.Data.ObjectSpaces.xml in their Framework\v2.0.50727 directory?

I’ve never had the ObjectSpaces bits on this machine, so I assume this is just a standard bit of cruft that comes with the framework.

Anyhow, I know Paul Wilson drew a lot of inspiration from ObjectSpaces, and while the documentation’s mostly filler text, I thought I’d attach it here just incase nobody’s noticed this historical footnote hanging around on their machine.

System.Data.ObjectSpaces.xml

Categories: Programming
Tagged: ,

Amazon PhD’s In User Land

September 7, 2006 · No Comments

Hello, sir! So you’d like to buy a book?

Perhaps you’d like a list of Statistically Improbable Phrases (SIPs) to inform your choice:

obj ectdatasource, dui ante, horrible chemical taste, same master page, most active threads, polls module, ascx user control, forums module, articles module, custom base class, active polls, int votes, model design pattern, sitemap file, ect base class, poll box, specified poll, object sender, persistent shopping cart, custom configuration section, membership module, aspx page, designing the database tables, querystring parameter, skin file

Anything take your fancy? No? Can I tempt you with some Capitalized Phrases (CAPs)?

Visual Studio, News Header, The Beer House, Web Parts, Parameter Name, Template Monster, Tools Help Back, Cancel Figure, Property Description, Server Explorer, Server Express, Page Language, Smart Tasks, Edit Profile, Column Name, Error List, Marco Bellinaso, Application Extension, Databound Databound Databound, Ready Figure, Solution Explorer, Edit Delete Select, Register Src, Internet Explorer, Generate Local Resource

I’m sure sir can agree that with a Fog Index of 14.0 and a comfortable 1,350,320 characters, this would be a fine addition to any bookshelf.

Still not interested?

Sir?

Sir?

Sigh. What does that always happen?

Categories: Reading
Tagged: