📄 stressgenerator.java
字号:
/*------------------------------------------------------------------------------Name: StressGenerator.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.contrib.dbwriter;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Random;import java.util.logging.Level;import java.util.logging.Logger;import java.util.logging.LogManager;import java.util.prefs.Preferences;import org.xmlBlaster.contrib.I_Info;import org.xmlBlaster.contrib.db.I_DbPool;import org.xmlBlaster.contrib.dbwatcher.Info;/** * Example code to run DbWriter as a standalone application. * <p> * You can edit this file and change the configuration settings. * </p> * <p> * Simple usage example: * </p> * <p> * <tt>java org.xmlBlaster.contrib.dbwriter.Example -db.password secret</tt> * </p> * <p> * <tt>java -Djava.util.logging.config.file=testlog.properties org.xmlBlaster.contrib.dbwriter.Example -db.password secret</tt> * </p> * @author Marcel Ruff */public class StressGenerator /*extends Thread*/ { private static Logger log = Logger.getLogger(StressGenerator.class.getName()); private I_DbPool dbPool; private I_Info info; private Random random; private int nmax = 1000; private long sleep = 0L; private int compareNmax = 0; private int compareCount = 0; private long seed; private List operations = new ArrayList(); private boolean commitCheck; private boolean exceptionOccured; public StressGenerator(I_Info info) throws Exception { this.info = info; ClassLoader cl = this.getClass().getClassLoader(); this.dbPool = (I_DbPool)this.info.getObject("db.pool"); if (this.dbPool == null) { String dbPoolClass = this.info.get("dbPool.class", "org.xmlBlaster.contrib.db.DbPool"); if (dbPoolClass.length() > 0) { this.dbPool = (I_DbPool)cl.loadClass(dbPoolClass).newInstance(); this.dbPool.init(info); if (log.isLoggable(Level.FINE)) log.fine(dbPoolClass + " created and initialized"); } else throw new IllegalArgumentException("Couldn't initialize I_DbPool, please configure 'dbPool.class' to provide a valid JDBC access."); this.info.putObject("db.pool", this.dbPool); } // comment the next line out if you always want the same pseudo-random sequence. this.nmax = this.info.getInt("stress.nmax", 1000); this.sleep = this.info.getLong("stress.sleep", 0L); this.compareNmax = this.info.getInt("stress.compareNmax", 0); this.seed = this.info.getLong("stress.seed", 0L); this.commitCheck = this.info.getBoolean("stress.commitCheck", true); if (this.seed < 0L) this.seed = System.currentTimeMillis(); this.random = new Random(this.seed); System.out.println("compareNmax: " + this.compareNmax); } public static void usage() { System.err.println("\n\n"); System.err.println("'stress.nmax' (1000) maximum number of tests/updates"); System.err.println("'stress.sleep' (0) time in ms to sleep between each update"); System.err.println("'stress.compareNmax' (0) number of updates after which to make a compare of replicated\n"); System.err.println(" items (compared toward source tables) (makes only sense if "); System.err.println(" 'stress.commitCheck' is set to 'false'"); System.err.println("'stress.seed' (0) seed number to use to gereate a pseudo-random sequence. If negative"); System.err.println(" then the current time is taken"); System.err.println("'stress.commitCheck' (true) if true, then the 'replitems' table is erased after each check."); System.err.println(" and a check is done on every commit (or rollback). You will not be"); System.err.println(" able to use DbWatcher when this option is choosen."); System.err.println("\n\n"); } private int getUniqueId() { return this.random.nextInt(10); } private String getName() { return "myName" + this.random.nextInt(10); } private String getSurname() { return "mySurname" + this.random.nextInt(1000); } private String getEmail() { return "email" + this.random.nextInt(1000) + ".com"; } private String getPhoto() { return "photo" + this.random.nextInt(1000); } private String getTable() { int val = this.random.nextInt(2); if (val == 0) return "repltest"; else return "repltest2"; } private String getWhere() { String ret = " WHERE uniqueId=" + getUniqueId() + " AND name='" + getName() + "'"; return ret; } private int update(Connection conn, String sql) throws SQLException { Statement st = conn.createStatement(); System.out.println(this.compareCount + "/" + this.compareNmax + ": '" + sql + "'"); try { return st.executeUpdate(sql); } catch (SQLException ex) { this.exceptionOccured = true; throw ex; } finally { if (st != null) st.close(); } } private int doInsert(Connection conn) throws SQLException { String sql = "INSERT INTO " + getTable() + " VALUES (" + getUniqueId() + ",'" + getName() + "','" + getSurname() + "','" + getEmail() + "','" + getPhoto() + "')"; int ret = update(conn, sql); for (int i=0; i < ret; i++) this.operations.add(sql); if (ret > 1) log.severe("doUpdate: too many inserts have been done, should be max. one but are '" + ret + "'"); return ret; } private int doDelete(Connection conn) throws SQLException { String sql = "DELETE FROM " + getTable() + getWhere(); int ret = update(conn, sql); for (int i=0; i < ret; i++) this.operations.add(sql); if (ret > 1) log.severe("doDelete: too many deletions have been done, should be max. one but are '" + ret + "'"); return ret; } private int doUpdate(Connection conn) throws SQLException { String sql = "UPDATE " + getTable() + " SET email = '" + getEmail() + "', surname = '" + getSurname() + "', photo = '" + getPhoto() + "'" + getWhere(); int ret = update(conn, sql); for (int i=0; i < ret; i++) this.operations.add(sql); if (ret > 1) log.severe("doUpdate: too many updates have been done, should be max. one but are '" + ret + "'"); return ret; } private void assertEquals(String msg, String val1, String val2) throws Exception { if (val1 == null && val2 == null) return; if (val1 == null) throw new Exception(msg + " should be null but is '" + val2 + "'"); if (val2 == null) throw new Exception(msg + " should be '" + val1 + "' but is null"); if (!val2.equals(val1)) { throw new Exception(msg + " should be '" + val1 + "' but is '" + val2 + "'"); } } private void assertEquals(String msg, int val1, int val2) throws Exception { if (val2 != val1) { throw new Exception(msg + " should be '" + val1 + "' but is '" + val2 + "'"); } } private void checkConsistency(Connection conn, String tableName, long timeToSleep) throws Exception { conn.commit(); System.out.print("COMPARING (sleep) ... '" + timeToSleep + "' "); Thread.sleep(timeToSleep); Statement st1 = null; Statement st2 = null; int count = 0; try { String sql1 = "SELECT * from " + tableName; String sql2 = "SELECT * from " + tableName + "repl"; st1 = conn.createStatement(); ResultSet rs1 = st1.executeQuery(sql1); st2 = conn.createStatement(); ResultSet rs2 = st2.executeQuery(sql2); while (true) { boolean cont1 = rs1.next(); boolean cont2 = rs2.next(); if (cont1 != cont2) throw new Exception("checkConsistency of '" + tableName + "' failed because different length in result set cont1='" + cont1 + "' cont2='" + cont2 + "'"); if (!cont1) break; count++; assertEquals(tableName + ": entry '" + count + "' uniqueId ", rs1.getInt(1), rs2.getInt(1)); assertEquals(tableName + ": entry '" + count + "' name ", rs1.getString(2), rs2.getString(2)); assertEquals(tableName + ": entry '" + count + "' surname ", rs1.getString(3), rs2.getString(3)); assertEquals(tableName + ": entry '" + count + "' mail ", rs1.getString(4), rs2.getString(4));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -