Tuesday, September 8, 2009

Optimizations solve the problems!!! (continue)

After running ANTS profiler, we found the method that causes the problem, this method simply requested by users every one minute to get some data, the implementation of this method was very bad.
The following is the pseudo-code for the method

GetData(startTime,endTime)
{
//Is Data in the cache?
if exist_cache
GetFromCache()
else
//select from database
//update cache
}
In the previous code we found two problems; the first one is that the mechanism of updating the cache was not done regularly, so a lot of users requests go to database, that causes a performance problem as the number of users increases.
The second problem is the way of getting data from the cache itself, the cache was DataSet that contains single table, getting data was done by calling DataTable.Select method that consumes a lot of time.
We solved these problems simply by updating the cache continuously in a separate background thread so whatever the number of users requests you have only one database access, and the second problem was solved by distributing data over tables so we can get data directly from tables without using DataTable.Select method.

Tuesday, August 25, 2009

Optimizations solve the problems!!!

Yes, sometimes when you are going to develop a program or any solution, you put set of assumptions and start building on these assumptions, after finishing development then testing and if everything is ok you assume your application does not need any kind of optimization as it performs well!! till your assumptions broken!! "ooh shit, may it happen!?", sure see the following real case.
One month ago my friends in another company faced a big problem, as they have a real time service that supports around 200 users, the system was working perfectly without any problem till they made promotion for set of customers without testing the maximum capacity their service can support!!!

around 1000 users tried to use the service, at that point the service could not respond to other customers and it was fully utilized!!!

So I joined them and started digging into, to see what is the cause of the problem as they have a lot of components.
We found that they have an old ASP.NET 1.1 web sevice that is being used most of the time from all of the clients, around 80% it was the source of the problem.
We started running ASP.NET performance counter to see the number of requests and queued requests as these are good indicator for the problem, then we started running the profiler to find the method that consumes most of the time!!!

catch me next post

Monday, June 29, 2009

Programming Tips

1. How to execute critical methods even there is no available memory

Sometimes we need to execute critical tasks even the application faces "out of memroy" exception
like (Database rollback, sending messages from server to clients, saving the working environment..etc)

So how we can execute this method even there is no enough memory.

A good solution for this problem is:

During application startup try to allocate enough dummy memory and when the application faces "out of memory", free this memory and exeute the critical mehods.
2. How to safely access windows control from another thead

The golden windows rule says "Do not access control except through control thread owner".
The control thread owner is the thread which creates the control.
so we have to switch from worker thread to owner thread (it is the main thread in most of the cases) to access the control
see the following code snippt in c#


namespace ThreadingSample
{
public partial class MainForm : Form
{

Thread batchthread = null;

public MainForm()
{
InitializeComponent();
}

void UpdateProgress(int progress)
{

if (prgbarProcess.InvokeRequired)
{
prgbarProcess.Invoke((
MethodInvoker)delegate()
{
prgbarProcess.Increment(progress);
});
}

else
{
prgbarProcess.Increment(progress);
}
}

void StartBatchProcessing()
{

for (int i = 0; i <> {
Thread.Sleep(100);
UpdateProgress(i);
}
}
private void btnProcess_Click(object sender, EventArgs e)
{
batchthread = new Thread(new ThreadStart(StartBatchProcessing)); batchthread.IsBackground = true; batchthread.Start();
}
}
}