001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.bench;
007:
008: import java.io.FileWriter;
009: import java.io.InputStream;
010: import java.io.PrintWriter;
011: import java.sql.Connection;
012: import java.sql.DriverManager;
013: import java.sql.PreparedStatement;
014: import java.sql.ResultSet;
015: import java.sql.Statement;
016: import java.util.ArrayList;
017: import java.util.Properties;
018:
019: import org.h2.test.TestBase;
020: import org.h2.util.IOUtils;
021: import org.h2.util.JdbcUtils;
022:
023: /**
024: * The main controller class of the benchmark application.
025: * To run the benchmark, call the main method of this class.
026: */
027: public class TestPerformance {
028:
029: boolean collect;
030: boolean log;
031:
032: public static void main(String[] args) throws Exception {
033: new TestPerformance().test(args);
034: }
035:
036: private Connection getResultConnection() throws Exception {
037: Class.forName("org.h2.Driver");
038: return DriverManager.getConnection("jdbc:h2:data/results");
039: }
040:
041: private void openResults(boolean init) throws Exception {
042: Connection conn = null;
043: Statement stat = null;
044: try {
045: conn = getResultConnection();
046: stat = conn.createStatement();
047: if (init) {
048: stat.execute("DROP TABLE IF EXISTS RESULTS");
049: }
050: stat
051: .execute("CREATE TABLE IF NOT EXISTS RESULTS(TESTID INT, TEST VARCHAR, "
052: + "UNIT VARCHAR, DBID INT, DB VARCHAR, RESULT VARCHAR)");
053: } finally {
054: JdbcUtils.closeSilently(stat);
055: JdbcUtils.closeSilently(conn);
056: }
057: }
058:
059: private void test(String[] args) throws Exception {
060: boolean init = false;
061: int dbId = -1;
062: String out = "benchmark.html";
063: for (int i = 0; i < args.length; i++) {
064: if (args[i].equals("-db")) {
065: dbId = Integer.parseInt(args[++i]);
066: } else if (args[i].equals("-init")) {
067: init = true;
068: } else if (args[i].equals("-out")) {
069: out = args[++i];
070: } else if (args[i].equals("-log")) {
071: log = Boolean.valueOf(args[++i]).booleanValue();
072: }
073: }
074: openResults(init);
075:
076: Properties prop = new Properties();
077: InputStream in = getClass().getResourceAsStream(
078: "test.properties");
079: prop.load(in);
080: in.close();
081: int size = Integer.parseInt(prop.getProperty("size"));
082: ArrayList dbs = new ArrayList();
083: for (int i = 0; i < 100; i++) {
084: if (dbId != -1 && i != dbId) {
085: continue;
086: }
087: String dbString = prop.getProperty("db" + i);
088: if (dbString != null) {
089: Database db = Database.parse(this , i, dbString);
090: if (db != null) {
091: db.setTranslations(prop);
092: dbs.add(db);
093: }
094: }
095: }
096: ArrayList tests = new ArrayList();
097: for (int i = 0; i < 100; i++) {
098: String testString = prop.getProperty("test" + i);
099: if (testString != null) {
100: Bench bench = (Bench) Class.forName(testString)
101: .newInstance();
102: tests.add(bench);
103: }
104: }
105: testAll(dbs, tests, size);
106: collect = false;
107: if (dbs.size() == 0) {
108: return;
109: }
110: ArrayList results = ((Database) dbs.get(0)).getResults();
111: Connection conn = null;
112: PreparedStatement prep = null;
113: Statement stat = null;
114: PrintWriter writer = null;
115: try {
116: conn = getResultConnection();
117: stat = conn.createStatement();
118: prep = conn
119: .prepareStatement("INSERT INTO RESULTS(TESTID, TEST, UNIT, DBID, DB, RESULT) VALUES(?, ?, ?, ?, ?, ?)");
120: for (int i = 0; i < results.size(); i++) {
121: Object[] res = (Object[]) results.get(i);
122: prep.setInt(1, i);
123: prep.setString(2, res[0].toString());
124: prep.setString(3, res[1].toString());
125: for (int j = 0; j < dbs.size(); j++) {
126: Database db = (Database) dbs.get(j);
127: prep.setInt(4, db.getId());
128: prep.setString(5, db.getName());
129: ArrayList r = db.getResults();
130: Object[] v = (Object[]) r.get(i);
131: prep.setString(6, v[2].toString());
132: prep.execute();
133: }
134: }
135:
136: writer = new PrintWriter(new FileWriter(out));
137: ResultSet rs = stat
138: .executeQuery("CALL '<table><tr><th>Test Case</th><th>Unit</th>' "
139: + "|| SELECT GROUP_CONCAT('<th>' || DB || '</th>' ORDER BY DBID SEPARATOR '') FROM "
140: + "(SELECT DISTINCT DBID, DB FROM RESULTS)"
141: + "|| '</tr>' || CHAR(10) "
142: + "|| SELECT GROUP_CONCAT('<tr><td>' || TEST || '</td><td>' || UNIT || '</td>' || ( "
143: + "SELECT GROUP_CONCAT('<td>' || RESULT || '</td>' ORDER BY DBID SEPARATOR '') FROM RESULTS R2 WHERE "
144: + "R2.TESTID = R1.TESTID) || '</tr>' ORDER BY TESTID SEPARATOR CHAR(10)) FROM "
145: + "(SELECT DISTINCT TESTID, TEST, UNIT FROM RESULTS) R1"
146: + "|| '</table>'");
147: rs.next();
148: String result = rs.getString(1);
149: writer.println(result);
150: } finally {
151: JdbcUtils.closeSilently(prep);
152: JdbcUtils.closeSilently(stat);
153: JdbcUtils.closeSilently(conn);
154: IOUtils.closeSilently(writer);
155: }
156:
157: // ResultSet rsDbs = conn.createStatement().executeQuery(
158: // "SELECT DB RESULTS GROUP BY DBID, DB ORDER BY DBID");
159: // while(rsDbs.next()) {
160: // writer.println("<th>" + rsDbs.getString(1) + "</th>");
161: // }
162: // ResultSet rs = conn.createStatement().executeQuery(
163: // "SELECT TEST, UNIT FROM RESULTS " +
164: // "GROUP BY TESTID, TEST, UNIT ORDER BY TESTID");
165: // while(rs.next()) {
166: // writer.println("<tr><td>" + rs.getString(1) + "</td>");
167: // writer.println("<td>" + rs.getString(2) + "</td>");
168: // ResultSet rsRes = conn.createStatement().executeQuery(
169: // "SELECT RESULT FROM RESULTS WHERE TESTID=? ORDER BY DBID");
170: //
171: //
172: // }
173:
174: // PrintWriter writer =
175: // new PrintWriter(new FileWriter("benchmark.html"));
176: // writer.println("<table><tr><th>Test Case</th><th>Unit</th>");
177: // for(int j=0; j<dbs.size(); j++) {
178: // Database db = (Database)dbs.get(j);
179: // writer.println("<th>" + db.getName() + "</th>");
180: // }
181: // writer.println("</tr>");
182: // for(int i=0; i<results.size(); i++) {
183: // Object[] res = (Object[])results.get(i);
184: // writer.println("<tr><td>" + res[0] + "</td>");
185: // writer.println("<td>" + res[1] + "</td>");
186: // for(int j=0; j<dbs.size(); j++) {
187: // Database db = (Database)dbs.get(j);
188: // ArrayList r = db.getResults();
189: // Object[] v = (Object[])r.get(i);
190: // writer.println(
191: // "<td style=\"text-align: right\">" + v[2] + "</td>");
192: // }
193: // writer.println("</tr>");
194: // }
195: // writer.println("</table>");
196:
197: System.out.println("Test finished");
198: System.exit(0);
199: }
200:
201: private void testAll(ArrayList dbs, ArrayList tests, int size)
202: throws Exception {
203: for (int i = 0; i < dbs.size(); i++) {
204: if (i > 0) {
205: Thread.sleep(1000);
206: }
207: // calls garbage collection
208: TestBase.getMemoryUsed();
209: Database db = (Database) dbs.get(i);
210: System.out.println("testing " + db.getName());
211: db.startServer();
212: Connection conn = db.getConnection();
213: runDatabase(db, tests, 1);
214: runDatabase(db, tests, 1);
215: collect = true;
216: runDatabase(db, tests, size);
217: conn.close();
218: db.log("Executed Statements", "#", db
219: .getExecutedStatements());
220: db.log("Total Time", "ms", db.getTotalTime());
221: db.log("Statement per Second", "#", db
222: .getExecutedStatements()
223: * 1000 / db.getTotalTime());
224: collect = false;
225: db.stopServer();
226: }
227: }
228:
229: private void runDatabase(Database db, ArrayList tests, int size)
230: throws Exception {
231: for (int j = 0; j < tests.size(); j++) {
232: Bench bench = (Bench) tests.get(j);
233: runTest(db, bench, size);
234: }
235: }
236:
237: private void runTest(Database db, Bench bench, int size)
238: throws Exception {
239: bench.init(db, size);
240: bench.runTest();
241: }
242:
243: public void log(String s) {
244: if (log) {
245: System.out.println(s);
246: }
247: }
248:
249: }
|