Hi Team,
We have an application for Random Pin Generation. As per business requirement, we have to support 12 concurrent user transactions at a time. Where in each user will attempt to generate 1 lakh Pins during each transaction. During this activity, application server's CPU utilization reached maximum limit.
We identified, random number generation task is what consuming maximum system resources ( 70 %). please advise where we need betterment to fix this issue? Should we need to find solution in IIS side or Application level?
System Configuration
1. 16 GB RAM
2. OCTA Core Processor
3. IIS 7.0
4. Development Environment ( Visual Studio 4.0)
Code
List lstpins = null;
lstpins = new List();
for (int i = 1; i <= qty; )
{
double dlRnd = rnFirst.NextDouble();//with seed
if (dlRnd < 0.0001)
{
dlRnd += 0.012;
}
string strRndFirstDgt = dlRnd.ToString().Substring(2, iFirstPinLng);
//----Second Rnd ------ 0.6 91979 52919 26617
double dlSecondRnd = rnSecond.NextDouble(); //Without Seed. 58535 03461 123
if (dlSecondRnd < 0.0001)
{
dlSecondRnd += 0.012;
}
string strRndSecondLastsixDgt = dlSecondRnd.ToString().Substring(dlSecondRnd.ToString().Length - iSecondPinLng, iSecondPinLng);
string conCan = strRndFirstDgt + strRndSecondLastsixDgt;
long temp;
temp = Convert.ToInt64(conCan) + DateTime.Now.Ticks; //addition to pin+ datetime Ticks
int intRndThird = rnThird.Next(1, 10);//To avoid the prefix value 0's;
string conCanRnd = intRndThird.ToString() + (temp.ToString().Substring(temp.ToString().Length - pinLength, pinLength - 1));
string sessionconcatpins = stInt + conCanRnd.ToString(); // Append Concurent User Hit Count in to PINS
if (!string.IsNullOrEmpty(sessionconcatpins.Trim()))
{
if (!lstpins.Contains(sessionconcatpins))
{
lstpins.Add(sessionconcatpins);
i++;
}
}
if (i % 100 == 0)
{
lngDateTicker = DateTime.Now.Ticks;
strSeedLastNineDgt = lngDateTicker.ToString().Substring(7, 9);
rnFirst = new Random(Convert.ToInt32(strSeedLastNineDgt));
}
}
Thread.Sleep(100);
return lstpins;
Sivasankaran GurusamyPosted Feb 26, 2015, 8:53 AM
Thanks for your support , we tried with your suggestion , The CPU utilization getting less ..(5%)
VulpesPosted Feb 17, 2015, 5:03 PM
This will be both CPU intensive and slow. Worse still the garbage collector will be kicking in periodically to clean up the previously used strings.
What you could try and do is to replace those sub-string operations with a method which extracts digits numerically i.e. without using strings. This should be much quicker to execute and won't require garbage collection.
Here's one I've just written for the purpose (not necessarily the best way of doing this):
The output is:
Notice that the second last extraction is 1 rather than 01 because the method returns a long rather than a string. Not sure whether this will be a problem or not in your application?