001: // $Id: PerformanceH2.java 7 2007-08-17 19:32:18Z jcamaia $
002:
003: package net.sf.persist.tests.performance;
004:
005: import java.io.BufferedReader;
006: import java.io.BufferedWriter;
007: import java.io.FileWriter;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.InputStreamReader;
011: import java.io.Writer;
012: import java.sql.Connection;
013: import java.sql.PreparedStatement;
014: import java.sql.ResultSet;
015: import java.sql.SQLException;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.List;
019: import java.util.Map;
020:
021: import net.sf.persist.Persist;
022: import net.sf.persist.tests.framework.ConnectionHelper;
023:
024: import com.jamonapi.Monitor;
025: import com.jamonapi.MonitorFactory;
026:
027: public class PerformanceH2 {
028:
029: public static AllTypes buildAllTypes() {
030: AllTypes a = new AllTypes();
031: byte[] binaryCol = new byte[255];
032: for (int i = 0; i < 255; i++)
033: binaryCol[i] = (byte) (i / 2);
034: a.setBinaryCol(binaryCol);
035: a.setBlobCol(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
036: Map obj = new HashMap();
037: obj.put("x", "y");
038: a.setOtherCol(obj);
039: a.setDateCol(new java.sql.Date(System.currentTimeMillis()));
040: a.setTimeCol(new java.sql.Time(System.currentTimeMillis()));
041: a.setTimestampCol(new java.sql.Timestamp(System
042: .currentTimeMillis()));
043: a.setCharCol("hello world char");
044: a.setVarcharCol("hello world varchar");
045: a.setVarcharIgnorecaseCol("hello world varchar ignore case");
046: a.setClobCol("hello world clob");
047: a.setIntCol(12345678);
048: a.setBooleanCol(true);
049: a.setTinyintCol(123);
050: a.setSmallintCol(123);
051: a.setBigintCol(Long.MAX_VALUE / 2);
052: a.setDecimalCol(12345678);
053: a.setDoubleCol(1234.5678);
054: a.setRealCol(1234.56f);
055: return a;
056: }
057:
058: public static void main(String[] args) throws SQLException,
059: IOException {
060:
061: String reportPath = args[0];
062:
063: String properties = "net/sf/persist/tests/performance/h2.properties";
064:
065: // create a connection
066: Connection connection = ConnectionHelper
067: .getConnection(properties);
068: connection.setAutoCommit(true);
069: connection
070: .createStatement()
071: .execute(
072: "create table if not exists all_types ( int_col int, boolean_col boolean, tinyint_col tinyint, smallint_col smallint, bigint_col bigint, decimal_col decimal, double_col double, real_col real, time_col time, date_col date, timestamp_col timestamp, binary_col binary, blob_col blob, other_col other, uuid_col uuid, varchar_col varchar, varchar_ignorecase_col varchar_ignorecase, char_col char, clob_col clob, id int auto_increment )");
073: connection.createStatement().execute("delete from all_types");
074:
075: // persist
076: Persist persist = new Persist(connection);
077:
078: int inserts = 5000;
079: int reads = 10000;
080: int readLists = 5;
081:
082: AllTypes a = buildAllTypes();
083:
084: // insert
085:
086: System.out.print("insert");
087:
088: String sql = "insert into ALL_TYPES(INT_COL,BOOLEAN_COL,TINYINT_COL,SMALLINT_COL,BIGINT_COL,DECIMAL_COL,"
089: + "DOUBLE_COL,REAL_COL,TIME_COL,DATE_COL,TIMESTAMP_COL,BINARY_COL,BLOB_COL,OTHER_COL,UUID_COL,"
090: + "VARCHAR_COL,VARCHAR_IGNORECASE_COL,CHAR_COL,CLOB_COL)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
091:
092: for (int i = 1; i <= inserts; i++) {
093:
094: if (i % 10 == 0)
095: System.out.print(" " + i);
096: if (i % 250 == 0)
097: System.out.println();
098:
099: // jdbc
100: Monitor mon = MonitorFactory.start("[insert] jdbc");
101: PreparedStatement stmt = connection.prepareStatement(sql);
102: stmt.setLong(1, a.getIntCol());
103: stmt.setBoolean(2, a.getBooleanCol());
104: stmt.setInt(3, a.getTinyintCol());
105: stmt.setInt(4, a.getSmallintCol());
106: stmt.setLong(5, a.getBigintCol());
107: stmt.setLong(6, a.getDecimalCol());
108: stmt.setDouble(7, a.getDoubleCol());
109: stmt.setFloat(8, a.getRealCol());
110: stmt.setTime(9, a.getTimeCol());
111: stmt.setDate(10, a.getDateCol());
112: stmt.setTimestamp(11, a.getTimestampCol());
113: stmt.setBytes(12, a.getBinaryCol());
114: stmt.setBytes(13, a.getBlobCol());
115: stmt.setObject(14, a.getOtherCol());
116: stmt.setBytes(15, a.getUuidCol());
117: stmt.setString(16, a.getVarcharCol());
118: stmt.setString(17, a.getVarcharIgnorecaseCol());
119: stmt.setString(18, a.getCharCol());
120: stmt.setString(19, a.getClobCol());
121: stmt.executeUpdate();
122: stmt.close();
123: mon.stop();
124:
125: // persist
126: mon = MonitorFactory.start("[insert] persist");
127: persist.insert(a);
128: mon.stop();
129:
130: }
131: System.out.println();
132:
133: // find a single id to be used by select by primary key
134: long id = persist.read(int.class,
135: "select min(id) from all_types");
136:
137: // read
138:
139: System.out.print("read");
140:
141: a = new AllTypes();
142:
143: for (int i = 1; i <= reads; i++) {
144: if (i % 50 == 0)
145: System.out.print(" " + i);
146: if (i % 1000 == 0)
147: System.out.println();
148:
149: // jdbc
150: Monitor mon = MonitorFactory.start("[read single] jdbc");
151: PreparedStatement stmt = connection
152: .prepareStatement("select * from all_types where id=?");
153: stmt.setLong(1, id);
154: ResultSet rs = stmt.executeQuery();
155: rs.next();
156: a.setIntCol(rs.getLong(1));
157: a.setBooleanCol(rs.getBoolean(2));
158: a.setTinyintCol(rs.getInt(3));
159: a.setSmallintCol(rs.getInt(4));
160: a.setBigintCol(rs.getLong(5));
161: a.setDecimalCol(rs.getLong(6));
162: a.setDoubleCol(rs.getDouble(7));
163: a.setRealCol(rs.getFloat(8));
164: a.setTimeCol(rs.getTime(9));
165: a.setDateCol(rs.getDate(10));
166: a.setTimestampCol(rs.getTimestamp(11));
167: a.setBinaryCol(rs.getBytes(12));
168: a.setBlobCol(rs.getBytes(13));
169: a.setOtherCol(rs.getObject(14));
170: a.setUuidCol(rs.getBytes(15));
171: a.setVarcharCol(rs.getString(16));
172: a.setVarcharIgnorecaseCol(rs.getString(17));
173: a.setCharCol(rs.getString(18));
174: a.setClobCol(rs.getString(19));
175: stmt.close();
176: mon.stop();
177:
178: mon = MonitorFactory.start("[read single] persist");
179: a = persist.readByPrimaryKey(AllTypes.class, id);
180: mon.stop();
181:
182: mon = MonitorFactory.start("[read single] persist map");
183: /*Map<String, Object> m =*/persist.readMap(
184: "select * from all_types where id=?", id);
185: mon.stop();
186: }
187: System.out.println();
188:
189: System.out.print("read list");
190: for (int i = 1; i <= readLists; i++) {
191: if (i % 1 == 0)
192: System.out.print(" " + i);
193: if (i % 20 == 0)
194: System.out.println();
195:
196: List al = new ArrayList();
197: Monitor mon = MonitorFactory.start("[read list] jdbc");
198: PreparedStatement stmt = connection
199: .prepareStatement("select * from all_types");
200: ResultSet rs = stmt.executeQuery();
201: while (rs.next()) {
202: a.setIntCol(rs.getLong(1));
203: a.setBooleanCol(rs.getBoolean(2));
204: a.setTinyintCol(rs.getInt(3));
205: a.setSmallintCol(rs.getInt(4));
206: a.setBigintCol(rs.getLong(5));
207: a.setDecimalCol(rs.getLong(6));
208: a.setDoubleCol(rs.getDouble(7));
209: a.setRealCol(rs.getFloat(8));
210: a.setTimeCol(rs.getTime(9));
211: a.setDateCol(rs.getDate(10));
212: a.setTimestampCol(rs.getTimestamp(11));
213: a.setBinaryCol(rs.getBytes(12));
214: a.setBlobCol(rs.getBytes(13));
215: a.setOtherCol(rs.getObject(14));
216: a.setUuidCol(rs.getBytes(15));
217: a.setVarcharCol(rs.getString(16));
218: a.setVarcharIgnorecaseCol(rs.getString(17));
219: a.setCharCol(rs.getString(18));
220: a.setClobCol(rs.getString(19));
221: al.add(a);
222: }
223: stmt.close();
224: mon.stop();
225:
226: mon = MonitorFactory.start("[read list] persist");
227: /*List<AllTypes> list =*/persist.readList(AllTypes.class);
228: mon.stop();
229:
230: mon = MonitorFactory.start("[read list] persist map");
231: /*List<Map<String,Object>> map =*/persist
232: .readMapList("select * from all_types");
233: mon.stop();
234: }
235: System.out.println();
236:
237: System.out.println();
238: System.out.println("finished");
239:
240: // read sort table javascript
241: InputStream is = Thread
242: .currentThread()
243: .getContextClassLoader()
244: .getResourceAsStream(
245: "net/sf/persist/tests/performance/sorttable.js");
246: BufferedReader r = new BufferedReader(new InputStreamReader(is));
247: StringBuffer sorttable = new StringBuffer();
248: String line;
249: while ((line = r.readLine()) != null) {
250: sorttable.append(line + "\n");
251: }
252:
253: // create report html
254: System.out.println("writing report to " + reportPath);
255: String report = MonitorFactory.getReport();
256: Writer w = new BufferedWriter(new FileWriter(reportPath));
257: w
258: .write("<html><head><style>*{font-family:tahoma; font-size:8pt} td{padding:3px}</style>");
259: w.write("<script>\n" + sorttable + "</script>");
260: w.write("</head><body onload=\"initTable('report-table')\">");
261: w.write(report.replace("<table", "<table id=\"report-table\""));
262: w.write("</body></html>");
263: w.close();
264:
265: }
266:
267: }
|