MySQL官方網站下載:
1、MySQL-server-5.5.28-1.rhel4.i386.rpm
2、java驅動 mysql-connector-java-5.1.5-bin.jar
測試服務器爲Dell E5410 的Debian linux 2.6,配置爲:
1、4核,2.33GHz
2、內存3G
3、SATA硬盤2T
web服務器:tomcat5.5
打壓工具:Apache Bench
監控工具:mysqladmin
測試思路:
1、使用自定義數據庫連接池,最大連接數500;
2、每個請求做1000次插入;
自定義連接池代碼:DBConnectionManager
public class DBConnectionManager {
static private DBConnectionManager instance; // 唯一實例
static private int clients;
private Vector drivers = new Vector();
private PrintWriter log;
private Hashtable pools = new Hashtable();
/**
* 返回唯一實例.如果是第一次調用此方法,則創建實例
*
* @return DBConnectionManager 唯一實例
*/
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}
/**
* 建構函數私有以防止其它對象創建本類實例
*/
private DBConnectionManager() {
init();
}
/**
* 將連接對象返回給由名字指定的連接池
*
* @param name
* 在屬性文件中定義的連接池名字
* @param con
* 連接對象
*/
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
pool.freeConnection(con);
}
}
/**
* 獲得一個可用的(空閒的)連接.如果沒有可用連接,且已有連接數小於最大連接數 限制,則創建並返回新連接
*
* @param name
* 在屬性文件中定義的連接池名字
* @return Connection 可用連接或null
*/
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}
/**
* 關閉所有連接,撤銷驅動程序的註冊
*/
public synchronized void release() {
// 等待直到最後一個客戶程序調用
if (--clients != 0) {
return;
}
Enumeration allPools = pools.elements();
while (allPools.hasMoreElements()) {
DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
log("撤銷JDBC驅動程序 " + driver.getClass().getName() + "的註冊");
} catch (SQLException e) {
log(e, "無法撤銷下列JDBC驅動程序的註冊: " + driver.getClass().getName());
}
}
}
/**
* 根據指定屬性創建連接池實例.
*
* @param props
* 連接池屬性
*/
private void createPools(Properties props) {
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
if (name.endsWith(".url")) {
String poolName = name.substring(0, name.lastIndexOf("."));
String url = props.getProperty(poolName + ".url");
if (url == null) {
log("沒有爲連接池" + poolName + "指定URL");
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max;
try {
max = Integer.valueOf(maxconn).intValue();
} catch (Exception e) {
log("錯誤的最大連接數限制: " + maxconn + " .連接池: " + poolName);
max = 0;
}
DBConnectionPool pool = new DBConnectionPool(poolName, url,
user, password, max);
pools.put(poolName, pool);
log("成功創建連接池" + poolName);
}
}
}
/**
* 讀取屬性完成初始化
*/
private void init() {
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
} catch (Exception e) {
System.err.println("不能讀取屬性文件. "
+ "請確保db.properties在CLASSPATH指定的路徑中");
return;
}
String logFile = dbProps.getProperty("logfile",
"DBConnectionManager.log");
try {
log = new PrintWriter(new FileWriter(logFile, true), true);
} catch (IOException e) {
System.err.println("無法打開日誌文件: " + logFile);
log = new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
}
private void loadDrivers(Properties props) {
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements()) {
String driverClassName = st.nextToken().trim();
try {
Driver driver = (Driver) Class.forName(driverClassName)
.newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
log("成功註冊JDBC驅動程序" + driverClassName);
} catch (Exception e) {
log("無法註冊JDBC驅動程序: " + driverClassName + ", 錯誤: " + e);
}
}
}
/**
* 將文本信息寫入日誌文件
*/
private void log(String msg) {
log.println(new Date() + ": " + msg);
}
/**
* 將文本信息與異常寫入日誌文件
*/
private void log(Throwable e, String msg) {
log.println(new Date() + ": " + msg);
e.printStackTrace(log);
}
/**
* 此內部類定義了一個連接池.它能夠根據要求創建新連接,直到預定的最 大連接數爲止.在返回連接給客戶程序之前,它能夠驗證連接的有效性.
*/
class DBConnectionPool {
private int checkedOut;
private Vector freeConnections = new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user;
public DBConnectionPool(String name, String URL, String user,
String password, int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}
/**
* 將不再使用的連接返回給連接池
*
* @param con
* 客戶程序釋放的連接
*/
public synchronized void freeConnection(Connection con) {
// 將指定連接加入到向量末尾
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
/**
* 從連接池獲得一個可用連接.如沒有空閒的連接且當前連接數小於最大連接 數限制,則創建新連接.如原來登記爲可用的連接不再有效,則從向量刪除之,
* 然後遞歸調用自己以嘗試新的可用連接.
*/
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
// 獲取向量中第一個可用連接
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log("從連接池" + name + "刪除一個無效連接");
// 遞歸調用自己,嘗試再次獲取可用連接
con = getConnection();
}
} catch (SQLException e) {
log("從連接池" + name + "刪除一個無效連接");
// 遞歸調用自己,嘗試再次獲取可用連接
con = getConnection();
}
} else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
return con;
}
/**
* 關閉所有連接
*/
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("關閉連接池" + name + "中的一個連接");
} catch (SQLException e) {
log(e, "無法關閉連接池" + name + "中的連接");
}
}
freeConnections.removeAllElements();
}
/**
* 創建新的連接
*/
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
} else {
con = DriverManager.getConnection(URL, user, password);
}
log("連接池" + name + "創建一個新的連接");
} catch (SQLException e) {
log(e, "無法創建下列URL的連接: " + URL);
return null;
}
return con;
}
}
}
測試代碼 InsertMysqlServlet:
public class InsertMysqlServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Integer insertNum = Integer.valueOf(req.getParameter("insertNum")
.toString());
resp.setContentType("text/html;charset=UTF-8");
resp.setHeader("Cache-Control", "no-cache");
String INSERT_SQL = "INSERT INTO test (id,name) values(10000,'This is for Mysql insert.')";
PreparedStatement pstmt = null;
DBConnectionManager m = DBConnectionManager.getInstance();
Connection conn = null;
try {
conn = m.getConnection("devel");
System.out.println(conn);
pstmt = conn.prepareStatement(INSERT_SQL);
for (int i = 0; i < insertNum; i++) {
pstmt.addBatch();
}
pstmt.executeBatch();
pstmt.close();
resp.getWriter().write(conn + ": Insert Mysql successed!");
} catch (Exception e) {
e.printStackTrace();
} finally {
m.freeConnection("devel", conn);
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
開始壓力測試,開400個線程併發,共插入100萬條記錄:
/usr/sbin/ab -n 1000 -c 400 http://192.168.175.130:8080/labWeb/insertMysql.do?insertNum=1000
監控: /usr/bin/mysqladmin -uroot -proot extended-status -r -i1|grep -i Com_insert
可以看到400個併發、4核2.33G、3G內存、SATA硬盤的情況下,插入100萬記錄的QPS在8400左右。