January
16th 2010
A Geographic Distance Class in C#

Posted under C# & GIS

The .NET Framework is pretty feature rich, helping developers put applications together quickly. A glaring omission, however, is support for anything to do with geography. I recently had a need to deal with distances between points in GPS files, calculated using the haversine formula, and came up with an elegant solution.

I modeled my class, Distance, on the TimeSpan struct, with a few important changes. This should make the programming model as familiar as possible. Just as a TimeSpan converts the same value ( say, 36 hours ) between different formats ( 1.5 days; 2,160 minutes, etc ), a Distance object converts between formats like feet, meters, and miles.

If this isn’t clear, a short code sample should help. The full code can be found at Distance.cs.

Distance d = new Distance();

d.Miles = 1;
Debug.Assert(d.Yards == 1760);
Debug.Assert(d.Feet == 5280);
Debug.Assert(d.Inches == 63360);

Continue Reading »

No Comments »

August
19th 2009
dbo.QueryPlan (Function)

Posted under Performance & SQL

Our last post described how to see what another connection to a SQL Server is doing, with a user-defined function, dbo.InputBuffer. Given a spid, the function returns the SQL text being run on that connection - or the last command received from the connection, depending on which version you decided to use. Along the same lines, it can often be more useful to find the query plan.

Because the execution plan generated by a SQL command changes for many reasons ( schema changes, data volume, etc ), knowing what a spid is doing might not be as important as knowing how the spid is doing it. For example, an ETL might suddenly run for hours, as the result of a table scan … but why? The function below can help a developer or DBA troubleshoot performance and concurrency issues.

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

If Exists (Select * From sys.objects Where object_id = object_id(N'dbo.QueryPlan') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
        Drop Function dbo.QueryPlan
GO

Create Function dbo.QueryPlan(@spid int) Returns xml As Begin
	Declare
		@plan_handle binary(32),
		@code xml

	Select @plan_handle = plan_handle
	From sys.dm_exec_requests
	Where session_id = @spid;

	Select @code = query_plan From sys.dm_exec_query_plan(@plan_handle);

	Return @code
End

Continue Reading »

1 Comment »

August
17th 2009
dbo.InputBuffer (Function)

Posted under SQL

Background

Most people who use SQL Server regularly, know about the wonderful DBCC InputBuffer command. If you’re not a SQL aficionado, you can easily find out what other connections “are doing” when one of them starts causing problems. Checking the input buffer - how commands are received by the server - returns the SQL code that was most recently sent from that connection. Continue Reading »

4 Comments »

February
7th 2009
A Debug Stopwatch (In C#)

Posted under Open Source & Performance

Time is of the utmost importance to almost any performance test, but, in .net, it can require a bit of scaffolding code. A triplet of C# classes can make short work of measuring performance, and of collating and persisting the results, in a handy XML format.

In the wild, this is often done with code similar to the following:

   Stopwatch sw = new Stopwatch();

   sw.Start();
   // Do some work here
   sw.Stop();

   Debug.WriteLine(sw.Elapsed.TotalMilliseconds.ToString();

This is hardly the end of the world, but it’s a bit cumbersome, especially over the course of many tests.  Perhaps a more natural construction would be:

   using (DebugStopwatch countIndexOf = new DebugStopwatch("IndexOf")) {
      // Do some work here
   }

… with the results being harvested later.  The literal text “IndexOf” can be replaced with any label that will be meaningful when you analyze the timing data.  Persistence of test results is accomplished through the magic of static classes. Continue Reading »

No Comments »

December
9th 2008
Normalizer of Web Pages, Qualifier of URLs

Posted under Open Source & Web

Relative paths look like /images/filename.jpeg, explaining the relationship between the current location and the resource.  Fully qualified paths are complete addresses, and look like http://domain.com/images/filename.jpg.

Sometimes you need to translate between the two.  Think of absolute urls as the third normal form of the web.

The complete solution has 138 lines in two classes, but this includes fetching the content from the internet.  Filling in a host name and path for each relative link is easy. You can download the full implementation

The juicy bits are below: Continue Reading »

1 Comment »

November
30th 2008
Hello world!

Posted under Uncategorized

Another blog devoted to research and development in the software world;  there have been worse ideas, but there have been better ones, too…

The open source will come slowly, but do innovative and very useful things.  General ideas, findings, and snippets of code will also find their way to these pages.  Stay tuned.

C# and Microsoft SQL Server will be the general focus of this blog, although the topics will stray.

No Comments »