Results 1 to 9 of 9
  1. #1
    mathes_n is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default 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!

  2. #2
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by mathes_n View Post
    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.
    ....
    Is there some other way of implementation so that i can solve the issue

    Please help me out :)
    What's the optimization goal ?
    You want to decrease CPU utilization ? :)

  3. #3
    mathes_n is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Yep...Want to decrease the CPU Utilization atleast by less than 30%

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  5. #5
    mathes_n is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    yep...if it is 100 then it bcomes only 40%

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  7. #7
    mathes_n is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    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;
    }

  8. #8
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    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.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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

  1. CPU performance getting affected
    By peiceonly in forum Lucene
    Replies: 2
    Last Post: 10-06-2008, 08:24 PM
  2. Performance Plugin
    By bugger in forum Eclipse
    Replies: 0
    Last Post: 01-31-2008, 02:01 PM
  3. taglibs performance issue
    By eva in forum Advanced Java
    Replies: 1
    Last Post: 01-17-2008, 12:44 PM
  4. Performance Of Collections
    By thomasprabu in forum Advanced Java
    Replies: 0
    Last Post: 01-05-2008, 11:17 AM
  5. how to improve the performance of JWS?
    By dinesh kaushik in forum Java Applets
    Replies: 0
    Last Post: 11-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •