Results 1 to 9 of 9
Thread: Performance issue
- 08-29-2008, 02:11 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
Performance issue
Hi,
The piece of code marked bold iterate 2000 times due the result set in table and as result the CPU utilization is hitting 100% for a 2 GB RAM Server with 4 GHZ processor.
public List retrieveCustomersForSearch() throws Exception{
List customers = null;
Session session = null;
CustomerBean customerBean = null;
Object[] resultArray = null;
try {
session = HibernateUtil.currentSession();
String SQLQuery = "SELECT name,customer_id FROM CUSTOMERS order by name";
List tempList = session.createSQLQuery(SQLQuery)
.addScalar("customer_id", Hibernate.LONG)
.addScalar("name", Hibernate.STRING)
.list();
tempList = tempList==null?new ArrayList():tempList;
customers = new ArrayList();
for (int index = 0; tempList!=null && index < tempList.size(); index++) {
resultArray = (Object[]) tempList.get(index);
customerBean = new CustomerBean();
customerBean.setCustomerId(Long.valueOf(resultArra y[0].toString()));
if(resultArray[1]!= null) {
customerBean.setName(resultArray[1].toString());
}else {
customerBean.setName(null);
}
customers.add(customerBean);
}
} catch (HibernateException he) {
log.error("caught: " + he);
throw new PersistenceException(he.getMessage(), he);
} catch (Exception e) {
e.getMessage();
throw new RuntimeException("Unable to retrive Customers from datastore: ", e);
}
return customers;
}
Is there some other way of implementation so that i can solve the issue
Please help me out :)
Thanks in Advance!
- 08-29-2008, 02:46 PM #2
- 09-01-2008, 06:59 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
Yep...Want to decrease the CPU Utilization atleast by less than 30%
- 09-01-2008, 07:05 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you sure that the 2000 iteration cause for this. Did you try it for less number of iterations? May be for 100. What happens? Better to ensure that first.
- 09-01-2008, 08:32 AM #5
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
yep...if it is 100 then it bcomes only 40%
- 09-01-2008, 08:56 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, now it's confirm. But actually even 40% is not worth. That mean rest of the code in your application also effect on this.
Anyway, now think about a best way to breakdown whole process in try block, may be into few methods. Because in a single try block you are trying to execute large number of process. Execute a query, initialize members twice and many more. It really cause to the process.
- 09-01-2008, 01:36 PM #7
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
Thanks for your suggestion but even after splitting it gives the same result
My splitted code looks like
public List retrieveCustomersForSearch() throws Exception{
List customers = null;
//List tempList = null;
Session session = null;
CustomerBean customerBean = null;
Object[] resultArray = null;
try {
session = HibernateUtil.currentSession();
/*customers = session.createSQLQuery("SELECT name,customer_id FROM CUSTOMERS order by name")
.addEntity("customers", com.boeing.tsas.castle.model.CustomerBean.class).l ist();*/
String SQLQuery = "SELECT name,customer_id FROM CUSTOMERS order by name";
List tempList = session.createSQLQuery(SQLQuery)
.addScalar("customer_id", Hibernate.LONG)
.addScalar("name", Hibernate.STRING)
.list();
tempList = tempList==null?new ArrayList():tempList;
customers = customerBeanForm(tempList);
} catch (HibernateException he) {
log.error("caught: " + he);
throw new PersistenceException(he.getMessage(), he);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
throw new RuntimeException("Unable to retrive Customers from datastore: ", e);
}
return customers;
}
private List customerBeanForm(List tempList){
List customers = new ArrayList();
//Session session = null;
CustomerBean customerBean = null;
int size;
if (tempList != null && tempList.size() >0) {
size = tempList.size();
Object[] resultArray = null;
for (int index = 0; index < size; index++) {
//System.out.println("Inside before obj array ::"+new java.text.SimpleDateFormat("hh:mm:ss:SSS").format( new Date()));
resultArray = (Object[]) tempList.get(index);
//System.out.println("Inside After obj array ::"+new java.text.SimpleDateFormat("hh:mm:ss:SSS").format( new Date()));
customerBean = new CustomerBean();
customerBean.setCustomerId(Long.valueOf(resultArra y[0].toString()));
if(resultArray[1]!= null) {
customerBean.setName(resultArray[1].toString());
}else {
customerBean.setName(null);
}
customers.add(customerBean);
//System.out.println("last line in for ::"+new java.text.SimpleDateFormat("hh:mm:ss:SSS").format( new Date()));
}
}
return customers;
}
- 09-01-2008, 04:40 PM #8
Not sure that CPU utilization could be decreased at all in this scenario. Code loads processor heavily without any yields, so...
How much time does it get to perform 2000 cycles, by the way ?
I'd understand the goal "decrease execution time from 20 secs to 5 secs", but decreasing CPU utilization itself...Well, usage of Thread.sleep(10) will give some relief to CPU but not sure that this is your real intent.
- 09-02-2008, 05:11 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you find that how many time your tempList initialized within the try block. I mean, may in the process it can set to null and initialize again and again. Like that try all possibilities.
And also pay attention on what ProjectKaiser says about threads and stuff.
Similar Threads
-
CPU performance getting affected
By peiceonly in forum LuceneReplies: 2Last Post: 10-06-2008, 08:24 PM -
Performance Plugin
By bugger in forum EclipseReplies: 0Last Post: 01-31-2008, 02:01 PM -
taglibs performance issue
By eva in forum Advanced JavaReplies: 1Last Post: 01-17-2008, 12:44 PM -
Performance Of Collections
By thomasprabu in forum Advanced JavaReplies: 0Last Post: 01-05-2008, 11:17 AM -
how to improve the performance of JWS?
By dinesh kaushik in forum Java AppletsReplies: 0Last Post: 11-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks