Thursday, January 31, 2008

Fusion - .net tool (binds refernces/probing)

fusion (fuslogvw.exe - default location c:\program files\microsoft visual studio 8\SDK\\bin ) does the job of finding assembly references at a fixed number of location.

Probing looks for references in the current dir as well as the dir/
eg: lets say current dir is Pro. so searching for foo.dll, fusion looks in Pro and Pro/foo directories for the dll.


to view the search log we need to enable a registry key -
goto vs command prompt
reg add HKLM\Software\Microsoft\Fusion /v EnableLog /t REG_DWORD /d 1
--adds a registry key 'EnableLog' under fusion(codename of the assembly loader component of the CLR) of type DWORD


You can create a simple program which refers a dll and then move the refered dll to some other location(other then dir\)

after enabling fusion log u'll get message that define the search process otherwise u'll just get a .net exception message.

references -

Wednesday, January 16, 2008

Performance Counter

PerformanceCounterCategory class can be used to create key and type-value inside it.
It creates it under "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" in the regedit.

PerformanceCounterCategory.Exists - weather the specified custom counter exists.
PerformanceCounterCategory.Create - creates a new custom counter.

Eg:

if (!PerformanceCounterCategory.Exists("MySingleCategory"))
{
PerformanceCounterCategory.Create ("MySingleCategory",
"My New Perf Category Description", "MyCounter",
"My New Perf Counter Desc");
}
else
{
Console.WriteLine("Counter already exists");
}


Another class 'CounterCreationDataCollection' can be used to add more then one custom counter. Create multiple instance of counter using 'CounterCreationData'. Add them to the collection and create the performanceCounterCategory using this collection.


Eg:

CounterCreationDataCollection col =
new CounterCreationDataCollection();

// Create two custom counter objects.
CounterCreationData counter1 = new CounterCreationData();
counter1.CounterName = "Counter1";
counter1.CounterHelp = "Custom counter 1";
counter1.CounterType = PerformanceCounterType.NumberOfItemsHEX32;

CounterCreationData counter2 = new CounterCreationData();

// Set the properties of the 'CounterCreationData' object.
counter2.CounterName = "Counter2";
counter2.CounterHelp = "Custom counter 2";
counter2.CounterType = PerformanceCounterType.NumberOfItemsHEX32;

// Add custom counter objects to CounterCreationDataCollection.
col.Add(counter1);
col.Add(counter2);

// Bind the counters to a PerformanceCounterCategory
// Check if the category already exists or not.
if(!PerformanceCounterCategory.Exists("MyMultipleCategory"))
{
PerformanceCounterCategory category =
PerformanceCounterCategory.Create("MyMultipleCategory",
" My New Perf Category Description ", col);
}
else
{
Console.WriteLine("Counter already exists");
}


To access a already existing performance counter. Use the 'category name' and 'counter name' to make the performance counter accessible.

Eg:

PerformanceCounter counter = new PerformanceCounter();
counter.CategoryName = "mySingleCategory";
counter.CounterName = "myCounter";
counter.ReadOnly = false;

Best way to find execution time of a part of code

Instead of using DateTime Object to find out the execution time (which gives only milliseconds of difference) use QueryPerformanceCounter and QueryPerformanceFrequency Win32 API menthods.

QueryPerformanceCounter(), queries the actual value of the high-resolution performance counter at any point. The second function, QueryPerformanceFrequency(), will return the number of counts per second that the high-resolution counter performs. therefore execution time = (start QueryPerformanceCounter - stop QueryPerformanceCounter)/ QueryPerformanceFrequency


using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace Win32
{
internal class HiPerfTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency);

private long startTime, stopTime;
private long freq;

// Constructor

public HiPerfTimer()
{
startTime = 0;
stopTime = 0;

if (QueryPerformanceFrequency(out freq) == false)
{
// high-performance counter not supported

throw new Win32Exception();
}
}

// Start the timer

public void Start()
{
// lets do the waiting threads there work

Thread.Sleep(0);

QueryPerformanceCounter(out startTime);
}

// Stop the timer

public void Stop()
{
QueryPerformanceCounter(out stopTime);
}

// Returns the duration of the timer (in seconds)

public double Duration
{
get
{
return (double)(stopTime - startTime) / (double) freq;
}
}
}
}

This class is very simple to use. Just create an instance of HiPerfTimer, call Start() to start timing and call Stop() to stop timing. To retrieve the elapsed time, just call the Duration() function and you will get the elapsed time.

The following sample should explain that.

HiPerfTimer pt = new HiPerfTimer(); // create a new PerfTimer object

pt.Start(); // start the timer

Console.WriteLine("Test\n"); // the code to be timed

pt.Stop(); // stop the timer

Console.WriteLine("Duration: {0} sec\n",
pt.Duration); // print the duration of the timed code

Monday, January 14, 2008

Response.Write() verse Response.Output.Write()

the difference between Response.Write() and Response.Output.Write() in ASP.NET. Well sir, I'm glad you asked, because it's damned interesting. :) The short answer is that the latter gives you String.Format-style output and the former doesn't. The long answer follows.

In ASP.NET the Response object is of type HttpResponse and when you say Response.Write you're really saying (basically) HttpContext.Current.Response.Write and calling one of the many overloaded Write methods of HttpResponse.

Response.Write then calls .Write() on it's internal TextWriter object:

public void Write(object obj){ this._writer.Write(obj);}

HttpResponse also has a Property called Output that is of type, yes, TextWriter, so:

public TextWriter get_Output(){ return this._writer; }

Which means you can to the Response whatever a TextWriter will let you. Now, TextWriters support a Write() method ala String.Format, so you can do this:

Response.Output.Write("Scott is {0} at {1:d}", "cool",DateTime.Now);

But internally, of course, this this is happening:

public virtual void Write(string format, params object[] arg)
{
this.Write(string.Format(format, arg));
}

thanks to Scott Hanselman's ComputerZen.com