Wednesday, April 29, 2009

CODE ANALYSIS USING VS 2008

I was going through CLR Profiler to use as a first step to check memory utilization on every application to make it as a process while delivering projects to clients. Though, there are better code profiler which can let us know more than what CLR Profiler can, I have taken CLR Profiler as it is Microsoft’s and its free. CLR Profiler does profling to know the memory consumption of your application like freed memory, reused memory, and allocated memory of the whole application. Having said, it includes all the .Net object’s memory consumption too, which is too much to have a look at. There is no way you can omit the .Net namespaces and have only the specific namespaces to be proflied. [Note : We can write our own code as the source code for the CLR profiler is available along with the application download. ]

While I was struggling to use it in Vista, I happen to see a new feature called Code Analysis in VS 2008. I was curious to know more about it and visited the Somesegar’s blog (http://blogs.msdn.com/somasegar/archive/2007/10/04/code-analysis-features-in-vs-2008.aspx).

Using Code Analysis feature in VS 2008, we can analyze the code, get the code metrics and also profiler the application to view the memory consumption. Let us see visit each of them.

Code Analysis

There is nothing new about this for people who have used FxCop or StyleCop already. In Visual Studio 2008 Developer Edition and Team Edition, we have a new menu item ‘Analyze’ which will help us in configuring, running and setting the Code analysis to run while code check-in.

Code analysis allows us to check our code against the Design rules, Usage rules, Naming rules etc which are designed to meet the Microsoft code standards and recommendation. We can enable this feature by doing a right click -> Properties -> Code Analysis -> selecting the checkbox ‘Enable Code Analysis on Build’ and choose the specific rules if needed. In case of website, we can choose the menu ‘Website’ in the VS IDE and choose to ‘Code Analysis Configuration’ or choose Analyze -> Code Analysis Configuration. Simillarly, use Website menu, Analyze menu or use the context menu in the solution explorer to Run the Code Analysis on Web site. For libraries, we get to see ‘Run the Code Analysis’ on the context menu (right click on class library in solution explorer).

Code Metrics

This feature in VS 2008 helps us in checking the Code Metrics of our projects / solutions. Please note that this feature will not give us the Code Metrics for website created in C#, Visual Basic projects. But it works fine for libraries.

To do this, choose the ‘Calculate Code Metrics’ from the context menu in the solution explorer by right-clicking on the Solution. The same can be done using the Analyze menu item in the VS 2008 Developer edition / Team edition.

This feature currently supports five different metrics; Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Coupling of Class and Lines of Code. The explanation for each of these are available in the blogs http://blogs.msdn.com/fxcop/archive/2007/02/28/announcing-visual-studio-code-metrics.aspx and http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx

Code Profiling

This reduces the burden of using CLR Profiler to know the memory consumption of the whole application which throws up the heap memory usage of even the .Net objects. The Code profiling in VS 2008 lets us know the Types with Most Instances created, Types with Most Memory allocated, and Function allocating most memory.

To do a code profiling, choose ‘Launch Performance Wizard’ from the ‘Analyze’ menu. Follow the wizard to choose the target application to profile and the profile type (Sampling or Instrumentation). Once the Wizard is finished, we will get to see a Performance Explorer window having the performance session created with Target and Report as the sub items under the session tree.

Choose the properties of the session and set the desired properties to profile for Performance counters. We can now launch the profile and get a performance report. We can click on any of the rows to drill down further to check the function calls (stack trace) with memory consumption. The data view once we click on the row will open up to show memory consumption by Functions, Objects, Call Tree, Caller / Callee, Object Life time etc.

References :

Code Profiler

http://msdn.microsoft.com/en-us/library/ms182372(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms182372.aspx

http://blog.maartenballiauw.be/post/2008/02/Code-performance-analysis-in-Visual-Studio-2008.aspx

Code Metrics

http://blogs.msdn.com/fxcop/archive/2007/02/28/announcing-visual-studio-code-metrics.aspx

http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx

VS 2008 Edition Comparison

http://www.microsoft.com/visualstudio/en-us/products/teamsystem/default.mspx#compare_products)

Wednesday, March 25, 2009

Why no "read only" "write only" automatic properties

We all know how automatic properties feature of .Net 3.5 left us improving our development time. Although we had the snippet facility to create properties earlier, it is too boring to keep creating private variable and a public property using each variable. Automatic property helps us in those situation. But there is a constraint that we cannot create automatic property as"read only" or "write only" property. Let's see why?

Automatic properties are defined as follows.


public int Width

{
get;
set;

}


It might look simillar to declaring a property in the abstract class in .Net 2.0. The difference between them is just the abstract keyword. Moving on, automatic properties create a private variable internally. It may look like

private int width

public int Width

{
get
{
return width;
}
set
{
width = value;
}
}

this look similar to .Net 2.0 way of creating properties.


Automatic property creates a private variable by itself for its use. What it exposes is only the property which is fair enough for us to work on. But again, why no "readonly" or "writeonly"?
If we create a property for readonly purpose, we have to do it as follows

public int Width
{
get;
}

In this case, we dont know the private variable created by the .Net for us as it is created at runtime. So, we cannot set the value of the .Net created variable through our program so that it returns the value when the Width property is referred.


So, the only way is to create a variable by ourselves and use it in our program to return it. That's why readonly and writeonly properties cannot be created automatic. We should create it explicitly as we do it in .Net 2.0 or earlier.

Few insights on partial methods

I am reading the Andrew Troelsen's book "Pro C# 2008 and the .Net 3.5 platform" bit un-regularly. I am finding this book bit interesting as it covers the chapters in a naive way. I came across two new features (Partial Methods and Automatic properties) in .Net 3.5 which are not complex to understand. But there are some constraints in using these features which Andrew left us to think through by ourselves. I thought through them and compiled my understanding as below. This blog will have about the Partial methods and will write on Automatic properties in a day.

Partial Methods - allows only void return type

Partial methods allow us to write the method implementation in a different class (partial class) by keeping just the method definition in a partial class. This feature though has few constraints which you will get to know by reading the above book. One of the constraint is that partial methods allow only void return type. Why is that?

Possibly, because until the partial method is implemented, your disassembled code will not show up the partial method definition. To explain further, please follow the example.

For ex :
namespace MyDotNet35
{
public partial class TestHarness
{
public bool myTestMethod()
{
.....do something ....
myPartialMethod() ; //call the partial method
....do something ....
}

partial void myPartialMethod();
}
}

If you look at the compiled IL of the above class, you will not get to see the myPartialMethod() call inside the myTestMethod until it is implemented in another partial class as below.

namespace MyDotNet35
{
public partial class TestHarness
{
partial void myPartialMethod()
{
//actual implementation of the partial method is here.
}
}
}

If my partial method returns an integer, then its possible for me to write the code above as


namespace MyDotNet35
{
public partial class TestHarness
{
public bool myTestMethod()
{
.....do something ....
int retVal = myPartialMethod() ; //call the partial method
if (retVal == someValue){
....do something ....
}
}

partial void myPartialMethod();
}
}

Imagine what would be the compiled version of IL....?

Yes, the compiler cannot leave the myPartialMethod call in this case (though the implementation is not done yet) as it is required to store the output to a variable and also some validation takes place based on the return value of the partial method.

Now, that's why the partial methods should have only void return type....got it.

Thursday, February 12, 2009

Derby aka JavaDb is the slowest

I am into an interesting experiment on using multiple open source dbs for my current assignment. The requirement is to come up with a data mashup simillar to Apatar (but not as sophisticated as that). Though Apatar is an open source and code is available for public, the need of the hour is to complete the data mashup within a week of time and that too customized and another key thing was Apatar uses Derby db which is very slow.

Now, back to the point, this required me to test the code with various open source databases to show the client about the performance of each database.

I initially tested it with mysql. But then I thought we need an embedded database so that the deployment of this application would be easier. I investigated few sites and got through handful of new open source dbs like SmallSQL, H2, HSQL, Derby, PostGreSQL, axiom etc.

Due to time constraint, I chose to test the app's performance with few databases like Derby, H2, HSQL, mysql (though mysql is not an option for me).

Here is the matrix (showing the execution time in seconds) after I test. My test was
1. To creating 3 tables
2. Inserting 20000 records into them
3. To create indexes
4. Joining them and building expressions and produce the output.

With 2 tables with 20K records
MySQL - 34
Derby - 183
HSQL - 7.67
H2 - 9.45

With 3 tables with 20K records
MySQL - 53
Derby - 604
HSQL - 13.47
H2 - 14.36

To my surprise, HSQL is better than even H2.

JDBC Drivers for Open source DBs

Though I am a .Net guy working on Java always facinates me with some compromise and little complaints. One of the tough thing is to get the drivers of different databases (non-conventional one's) when we need especially on Open source databases. It is not available on the readme files when you download the database jars. Ofcourse, we can figure it out from google, but I thought of putting it in this blog some of the JDBC Drivers with URL so that you need not have to google for each open source drivers and url.

The following are some of the open source database connectivity parameters which I came across when I worked with java recently.

#FOR MYSQL

Url=jdbc:mysql://localhost:3306/
Driver=com.mysql.jdbc.Driver

Please note the mysql can have a server mode and hence the url might differ. In general, when you install mysql in your desktop and use it then the above holds good. The default port is 3306 and if you talented enough to change it then I believe you are equally talented enough to figure out the url :).

#FOR DERBY

URL=jdbc:derby:DBNAME;create=true
Driver=org.apache.derby.jdbc.EmbeddedDriver

The DB is created with the DBName you specify in Derby (aka Javadb) when you say create=true.

#FOR SMALLSQL

URL=jdbc:smallsql:DBNAME
Driver=smallsql.database.SSDriver

#FOR HSQL

URL=jdbc:hsqldb:DBNAME
Driver=org.hsqldb.jdbcDriver

#FOR H2

URL=jdbc:h2:~/test
Driver=org.h2.Driver

Hope this helps to few atleast.