Tuesday, May 27, 2008

Get Corners of a Rectangle, given the center,height and Width

Following code will draw a rectangle on a graphics context, when a valid center, height and width is provided.


int CenterX, CenterY;

public void DrawRect(Graphics g, int Width, int Height)

{

Point[] RectPts;

double Theta;

int HalfWidth, HalfHeight;

double Radius;

RectPts = new Point[4];

HalfWidth = Width / 2;

HalfHeight = Height / 2;

Radius = Math.Sqrt(HalfWidth * HalfWidth + HalfHeight * HalfHeight);

Theta = Math.Acos(HalfWidth / Radius);

RectPts[0] = GetPt(Theta, Radius);

RectPts[1] = GetPt(Math.PI - Theta,Radius);

RectPts[2] = GetPt(Math.PI + Theta,Radius);

RectPts[3] = GetPt(-Theta,Radius);

Pen PenColor = new Pen(Color.Red, 2);

g.DrawPolygon( PenColor, RectPts);

PenColor.Dispose();

}

private Point GetPt(double Offset, double Radius)

{

double tAngle;

Point temp = new Point();

double Rotation = 30;

tAngle = Rotation / 180 * Math.PI + Offset;

temp.X = CenterX + (int)(Radius * Math.Cos(tAngle));

temp.Y = CenterY + (int)(Radius * Math.Sin(tAngle));

return temp;

}

The code idea is copied from some site and converted to C# in a compilable and working format. Hope it helps.


Tuesday, April 15, 2008

How to use CompuWare DevPartner for Memory profiling of .net Windows Services

I have been using the Compuware Devpartner studio for quite some time for profiling our application for memory and performance, and I found it quite useful. Recently we had a memory leak issue in one of our windows service. I tried using the DevPartner profiling for memory on it, but alas I could not profile a service!!

This was a setback and now I was in a fix. Either I have to explore new profiling tools to get my job done or dive into the vast code and find the issue. I decided to spend some time on finding if there is some other way to profile the windows services using DevPartner. I Googled a lot and could not find anything :(

Later out of frustration I looked into the installation directory, and found lots of exe’s and out of curiosity I opened them one by one. To my pleasant surpise I found one exe DPAnalysis.exe which was what I wanted. It was a command line tool for doing all sorts of supported Analysis

  • Performance
  • Coverage
  • Memory
  • Performance expert

Following is the help output from that exe, which explains all the different command line arguments.

Usage:

1) DPAnalysis [a] [b] [c] [d] {e} target [target args]

2) DPAnalysis /config config.xml

a) AnalysisType: Set the run-time analysis type. Performance is default.

/PERF[ORMANCE] Set analysis type to DevPartner Performance Analysis

/COV[ERAGE] Set analysis type to DevPartner Coverage Analysis

/MEM[ORY] Set analysis type to DevPartner Memory Analysis

/EXP[ERT] Set analysis type to DevPartner Performance Expert

b) DataCollection: Enable/Disable data collection for a given target.

DOES NOT LAUNCH the target.

/E[NABLE] Enable data collection for the specified process or service

/D[ISABLE] Disable data collection for the specified process or service

c) OtherOptions:

/O[UTPUT] Specify the session file output directory and/or name with optional extension (.dpprf, .dpcov, .dpmem, or .dppxp)

/W[ORKINGDIR] Specify the process' working directory

/H[OST] Specify target's host machine

/NOWAIT Don't wait for process to exit, just wait for it to start

/N[EWCONSOLE] Run the process in its own command window

d) AnalysisOptions:

/NO_MACH5 Disables excluding time spent on other threads

/NM_METHOD_GRANULARITY Set data collection granularity to method-level (line-level is default)

/EXCLUDE_SYSTEM_DLLS Exclude data collection for system dlls (Perf only)

/NM_ALLOW_INLINING Enable run-time instrumentation of inline methods

/NO_OLEHOOKS Disable collection of COM

/NM_TRACK_SYSTEM_OBJECTS Track system object allocation (Memory only)

e) TargetType: Identify target process or service. MUST BE LAST OPTION.

All arguments after the target name/path are passed directly to the target.

/P[ROCESS] Target is an exe filename (followed by arguments to the process)

/S[ERVICE] Target is a service name (followed by arguments to the service)

2)

/C[ONFIG] Path to configuration file that includes all startup information. Note: no other options can be used with /Config



For profiling the windows service I used the following command”

DPAnalysis /MEM /E /S “my service name”

After running the above using a command prompt, I started the service from service controller, and let the service do its job. I monitored the memory usage and VM Size of service using task manager, and when I was convinced that the memory leak condition is reached, I stopped the service. I was prompted for saving the memory analysis file :), which could be easily opened up in VS2005 (of course because DevPartner was installed)