论坛首页 Java企业应用论坛

性能比较:SLSB+Hibernate & SLSB+CMP

浏览 17779 次
该帖已经被评为精华帖
作者 正文
   发表时间:2003-09-12  
我测了一下, 把Hibernate放在Ejb Container中的性能。我还不知道如何贴图,所以把数据贴出来。没有使用JTS, 也把CMP在weblogic中的数目改为了1, Weblogic系统默认为1000.

从图形的趋势来看,数据量越大,Hibernate优势越明显。不知测的对不对?


***Create***

Record    100 300 500 700 900

Entity Bean 248 762 1336 1700 2317
Hibernate    210 496 854 1347 1537



***Query***

Record      100 300 500 700 900

Entity Bean    27 70 103 140 177
Hibernate      25 51 71 99 124
   发表时间:2003-09-12  
用附件上传功能来贴图,请给出详细的硬件软件配置和环境,最好能够有测试代码下载,可以放在附件里面。
0 请登录后投票
   发表时间:2003-09-13  
CPU: Intel PIII 650
Memory: 192M
Hard Disk: IBM-DJSA-210,  10G
Operate System: Windows XP
Application Server: Weblogic 7.0
JDK: JDK1.4
Database: Microsoft SQL Server 2000
Persistence Framewok: Hibernate 2.0
JDBC Driver: Official JDBC Driver For Microsoft SQL Server

Client side

package com.bruce;

import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;

public class TestSesTestClient
extends Object {
private TestSesHome testSesHome = null;

//Construct the EJB test client
public TestSesTestClient(); {
initialize();;
}

public void initialize(); {
try {
  Context context = new InitialContext();;
  Object ref = context.lookup("TestSes");;
  testSesHome = (TestSesHome); PortableRemoteObject.narrow(ref, TestSesHome.class);;
}
catch (Exception e); {
  e.printStackTrace();;
}
}

public TestSesHome getHome(); {
return testSesHome;
}

//Main method

public static void main(String[] args); {
TestSesTestClient client = new TestSesTestClient();;
try { 
  TestSesHome testSesHome = (TestSesHome); client.getHome();;
  /*
  testSesHome.create();.testHibernateCreate(1);;
  testSesHome.create();.testHibernateQuery();;
  testSesHome.create();.testHibernateUpdate();;
  testSesHome.create();.testHibernateDelete();;
  
  testSesHome.create();.testEjbCreate(1);;
  testSesHome.create();.testEjbQuery();; 
  testSesHome.create();.testEjbUpdate();; 
  testSesHome.create();.testEjbDelete();;
  */
   
   
   long s = 0;
   long xx = testSesHome.create();.testEjbCreate(100);;
    for(int i=1;i<11;i++);{
  	 long t = testSesHome.create();.testEjbUpdate();;
	    if(i!=1);{
	      s = s + t;
	    }
	  }
	  System.out.println("Average time is " + s/9);;
}
catch (Exception e); {
  e.printStackTrace();;
}
}
}




Server Side

package com.bruce;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import org.apache.log4j.Logger;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.transaction.UserTransaction;



public class TestSesBean
    implements SessionBean {
  private final static Logger log = Logger.getLogger(TestSesBean.class);;
  Configuration conf = null;
  SessionFactory sessions = null;
  Session session = null;
  SessionContext sessionContext;
  
  public void ejbCreate(); throws CreateException {
    log.info("TestSesBean is created");;
  }

  public void ejbRemove(); {
    log.info("TestSesBean is removed");;
  }

  public void ejbActivate(); {
     log.info("TestSesBean is actived");;
  }

  public void ejbPassivate(); {
     log.info("TestSesBean is passivated");;
  }

  public void setSessionContext(SessionContext sessionContext); {
    this.sessionContext = sessionContext;
  }

  public long testEjbCreate(int count); {
  	 long t0 = 0;
    long t1 = 0;
    
    try {
      System.out.println("Start Creating Records with Entity Bean...");;
      t0 = System.currentTimeMillis();;
      Context context = new InitialContext();;
      Object object = context.lookup("java:comp/env/ejb/Products");;
      ProductsHome productsHome = (ProductsHome);
          javax.rmi.PortableRemoteObject.narrow(object, ProductsHome.class);;
      for (int i = 0; i < count; i++); {
       productsHome.create(String.valueOf(System.currentTimeMillis(); + i+"EJB"););;
      }
      t1 = System.currentTimeMillis();;
      System.out.println("*****Finished Creating Records with EJB: " +
                         (t1 - t0); + "ms");;
     }
    catch (Exception e); {
      e.printStackTrace();;
    }
    return (t1 - t0);;
 }

  
  public long testEjbQuery(); {
  	 long t0 = 0;
    long t1 = 0;
    
    try {
      System.out.println("***Start Reading Records with Entity Bean...");;
      t0 = System.currentTimeMillis();;
      Context context = new InitialContext();;
      Object object = context.lookup("java:comp/env/ejb/Products");;
      ProductsHome productsHome = (ProductsHome);
          javax.rmi.PortableRemoteObject.narrow(object, ProductsHome.class);;
      
      Iterator i = productsHome.findAll();.iterator();;
						while (i.hasNext();); {
							Products pro = (Products); javax.rmi.PortableRemoteObject.narrow(i.next();, Products.class);;
							//System.out.println(pro.getProductId(););;
						}
      t1 = System.currentTimeMillis();;
      System.out.println("*****Finished Reading Records with EJB: " +
                         (t1 - t0); + "ms");;
     }
    catch (Exception e); {
      e.printStackTrace();;
    }
    return (t1 - t0);;
 }

public long testEjbDelete(); {
  	 long t0 = 0;
    long t1 = 0;
    
    try {
      System.out.println("***Start Deleting Records with Entity Bean...");;
      t0 = System.currentTimeMillis();;
      Context context = new InitialContext();;
      Object object = context.lookup("java:comp/env/ejb/Products");;
      ProductsHome productsHome = (ProductsHome);
          javax.rmi.PortableRemoteObject.narrow(object, ProductsHome.class);;
      
      Iterator i = productsHome.findAll();.iterator();;
						while (i.hasNext();); {
							Products pro = (Products); javax.rmi.PortableRemoteObject.narrow(i.next();, Products.class);;
							pro.remove();;
						}
      t1 = System.currentTimeMillis();;
      System.out.println("*****Finished Deletinging Records with EJB: " +
                         (t1 - t0); + "ms");;
     }
    catch (Exception e); {
      e.printStackTrace();;
    }
    return (t1 - t0);;
 }

 public long testEjbUpdate(); {
  	 long t0 = 0;
    long t1 = 0;
    
    try {
      System.out.println("***Start Updating Records with Entity Bean...");;
      t0 = System.currentTimeMillis();;
      Context context = new InitialContext();;
      Object object = context.lookup("java:comp/env/ejb/Products");;
      ProductsHome productsHome = (ProductsHome);
          javax.rmi.PortableRemoteObject.narrow(object, ProductsHome.class);;
      
      Iterator i = productsHome.findAll();.iterator();;
						while (i.hasNext();); {
							Products pro = (Products); javax.rmi.PortableRemoteObject.narrow(i.next();, Products.class);;
							pro.setName("testUpdateByEntity");;
						}
      t1 = System.currentTimeMillis();;
      System.out.println("*****Finished Deletinging Records with EJB: " +
                         (t1 - t0); + "ms");;
     }
    catch (Exception e); {
      e.printStackTrace();;
    }
    return (t1 - t0);;
 }


  public long testHibernateCreate(int count);{
    long t0 = 0;
    long t1 = 0;
    try{
      session = HibernateSession.openSession();;
           
      Product pro = null;
      System.out.println("Start Creating Records with Hibernate...");;
      t0 = System.currentTimeMillis();;
      for (int i = 0; i < count; i++); {
        pro = new Product();;
        pro.setProductId(String.valueOf(System.currentTimeMillis(); + i););;
        session.save(pro);;
      }
      session.flush();;
      t1 = System.currentTimeMillis();;
      System.out.println("*****Finished Creating Records with Hibernate: " +
                         (t1 - t0); + "ms");;
      
      //session.connection();.commit();;
   }catch (MappingException me); {
      System.out.println("MappingException " + me.getMessage(););;
    }
    catch (HibernateException he); {
       System.out.println("HibernateException " + he.getMessage(););;
    }
    catch (Exception e); {
      System.out.println("Exception " + e.getMessage(););;
    }
    finally {
      try {
        HibernateSession.closeSession();;
      }
      catch (HibernateException he); {
        System.out.println("HibernateException " + he.getMessage(););;
      }
    }
    return (t1 - t0);;
  
 }
 
 public long testHibernateQuery();{
    long t0 = 0;
    long t1 = 0;
    try{
      session = HibernateSession.openSession();;
           
      Product pro = null;
      System.out.println("Start Reading Records with Hibernate...");;
      t0 = System.currentTimeMillis();;
      Query q = session.createQuery("select p from Product as p");;
      List l = q.list();;
      for (int i=0; i< l.size();; i++); {
        pro = (Product); l.get(i);;
      }  
      t1 = System.currentTimeMillis();;
      System.out.println("*******Finished Reading Records with Hibernate: " +
                         (t1 - t0); + "ms");;
    }catch (MappingException me); {
      System.out.println("MappingException " + me.getMessage(););;
    }
    catch (HibernateException he); {
       System.out.println("HibernateException " + he.getMessage(););;
    }
    catch (Exception e); {
      System.out.println("Exception " + e.getMessage(););;
    }
    finally {
      try {
        HibernateSession.closeSession();;
      }
      catch (HibernateException he); {
        System.out.println("HibernateException " + he.getMessage(););;
      }
    }
    return (t1 - t0);;
 }
 
 
 public long testHibernateDelete();{
    long t0 = 0;
    long t1 = 0;
    try{
      session = HibernateSession.openSession();;
           
      Product pro = null;
      System.out.println("Start Deleting Records with Hibernate...");;
      t0 = System.currentTimeMillis();;
      session.delete("from Product");;
      session.flush();;  
      t1 = System.currentTimeMillis();;
      System.out.println("*******Finished Deleting Records with Hibernate: " +
                         (t1 - t0); + "ms");;
    }catch (MappingException me); {
      System.out.println("MappingException " + me.getMessage(););;
    }
    catch (HibernateException he); {
       System.out.println("HibernateException " + he.getMessage(););;
    }
    catch (Exception e); {
      System.out.println("Exception " + e.getMessage(););;
    }
    finally {
      try {
        HibernateSession.closeSession();;
      }
      catch (HibernateException he); {
        System.out.println("HibernateException " + he.getMessage(););;
      }
    }
    return (t1 - t0);;
 }
 
 public long testHibernateUpdate();{
    long t0 = 0;
    long t1 = 0;
    try{
      session = HibernateSession.openSession();;
           
      Product pro = null;
      System.out.println("Start Updating Records with Hibernate...");;
      t0 = System.currentTimeMillis();;
      Query q = session.createQuery("select p from Product as p");;
      List l = q.list();;
      for (int i=0; i< l.size();; i++); {
        pro = (Product); l.get(i);;
        pro.setName("testUpdateByHibernate");;
      }  
      t1 = System.currentTimeMillis();;
      System.out.println("*******Finished updating Records with Hibernate: " +
                         (t1 - t0); + "ms");;
    }catch (MappingException me); {
      System.out.println("MappingException " + me.getMessage(););;
    }
    catch (HibernateException he); {
       System.out.println("HibernateException " + he.getMessage(););;
    }
    catch (Exception e); {
      System.out.println("Exception " + e.getMessage(););;
    }
    finally {
      try {
        HibernateSession.closeSession();;
      }
      catch (HibernateException he); {
        System.out.println("HibernateException " + he.getMessage(););;
      }
    }
    return (t1 - t0);;
 }
 
 
  public void testConnection(); {
    Connection conn = null;
    PreparedStatement pstmt = null;
    String s = "";
    try {
      conn = getConnection();;
      pstmt = conn.prepareStatement(
          "select name from products where productId = ?");;
      pstmt.setString(1, "1");;
      ResultSet rs = pstmt.executeQuery();;
      while (rs.next();); {
        s = rs.getString("name");;
        System.out.println(s);;
      }
    }
    catch (Exception e); {
      e.printStackTrace();;
    }
  } //end

  public Connection getConnection(); throws Exception {
    try {
      Context ctx = new InitialContext();;
      javax.sql.DataSource ds =
          (javax.sql.DataSource); ctx.lookup("ejbPool");;
      return ds.getConnection();;
    }
    catch (Exception e); {
      System.err.println("Could not locate datasource!  Reason:");;
      e.printStackTrace();;
      throw e;
    }
  }

}


0 请登录后投票
   发表时间:2003-09-25  
robbin 写道
用附件上传功能来贴图,请给出详细的硬件软件配置和环境,最好能够有测试代码下载,可以放在附件里面。

是啊,看的眼花缭乱,代码挺多的,能把测试代码上传一下么,大家都来试试!
0 请登录后投票
   发表时间:2004-03-07  
如果用localhome 和local ejb object ejb的表现应该会好一点,不知bruce能不能把local对象的测试也做一下啊。
另外,多线程并发情况下,两种架构是否会有比较大的差别?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics