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.poweroff;
007:
008: import java.io.DataOutputStream;
009: import java.io.File;
010: import java.io.FileDescriptor;
011: import java.io.RandomAccessFile;
012: import java.net.Socket;
013: import java.sql.Connection;
014: import java.sql.DriverManager;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Statement;
019:
020: import org.h2.util.FileUtils;
021:
022: /**
023: * This application tests the durability / non-durability of file systems and
024: * databases. Two computers with network connection are required to run this
025: * test. Before starting this application, the Listener application must be
026: * started on another computer.
027: */
028: public class Test {
029:
030: String driver;
031: String url;
032: String user;
033: String password;
034: Connection conn;
035: Statement stat;
036: PreparedStatement prep;
037:
038: private Test() {
039: }
040:
041: private Test(String driver, String url, String user,
042: String password, boolean writeDelay0) {
043: this .driver = driver;
044: this .url = url;
045: this .user = user;
046: this .password = password;
047: try {
048: Class.forName(driver);
049: conn = DriverManager.getConnection(url, user, password);
050: stat = conn.createStatement();
051: if (writeDelay0) {
052: stat.execute("SET WRITE_DELAY 0");
053: }
054: System.out.println(url + " started");
055: } catch (Exception e) {
056: System.out.println(url + ": " + e.toString());
057: return;
058: }
059: try {
060: ResultSet rs = stat
061: .executeQuery("SELECT MAX(ID) FROM TEST");
062: rs.next();
063: System.out.println(url + ": MAX(ID)=" + rs.getInt(1));
064: stat.execute("DROP TABLE TEST");
065: } catch (SQLException e) {
066: // ignore
067: }
068: try {
069: stat
070: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
071: prep = conn
072: .prepareStatement("INSERT INTO TEST VALUES(?, ?)");
073: } catch (SQLException e) {
074: System.out.println(url + ": " + e.toString());
075: }
076: }
077:
078: private void insert(int id) {
079: try {
080: if (prep != null) {
081: prep.setInt(1, id);
082: prep.setString(2, "World " + id);
083: prep.execute();
084: }
085: } catch (SQLException e) {
086: System.out.println(url + ": " + e.toString());
087: }
088: }
089:
090: public static void main(String[] args) throws Exception {
091: new Test().test(args);
092: }
093:
094: void test(String[] args) throws Exception {
095: int port = 9099;
096: String connect = "192.168.0.3";
097: boolean file = false;
098: for (int i = 0; i < args.length; i++) {
099: if (args[i].equals("-port")) {
100: port = Integer.parseInt(args[++i]);
101: } else if (args[i].equals("-connect")) {
102: connect = args[++i];
103: } else if (args[i].equals("-file")) {
104: file = true;
105: }
106: }
107: test(connect, port, file);
108: }
109:
110: void test(String connect, int port, boolean file) throws Exception {
111: Socket socket = new Socket(connect, port);
112: DataOutputStream out = new DataOutputStream(socket
113: .getOutputStream());
114: System.out.println("Connected to " + socket.toString());
115: if (file) {
116: testFile(out);
117: } else {
118: testDatabases(out);
119: }
120: }
121:
122: void testFile(DataOutputStream out) throws Exception {
123: File file = new File("test.txt");
124: if (file.exists()) {
125: file.delete();
126: }
127: RandomAccessFile write = new RandomAccessFile(file, "rws");
128: // RandomAccessFile write = new RandomAccessFile(file, "rwd");
129: int fileSize = 10 * 1024 * 1024;
130: FileUtils.setLength(write, fileSize);
131: write.seek(0);
132: int i = 0;
133: FileDescriptor fd = write.getFD();
134: while (true) {
135: if (write.getFilePointer() >= fileSize) {
136: break;
137: }
138: write.writeBytes(i + "\r\n");
139: fd.sync();
140: out.writeInt(i);
141: out.flush();
142: i++;
143: }
144: write.close();
145: }
146:
147: void testDatabases(DataOutputStream out) throws Exception {
148: Test[] dbs = new Test[] {
149: new Test("org.h2.Driver", "jdbc:h2:test1", "sa", "",
150: true),
151: new Test("org.h2.Driver", "jdbc:h2:test2", "sa", "",
152: false),
153: new Test("org.hsqldb.jdbcDriver", "jdbc:hsqldb:test4",
154: "sa", "", false),
155: // new Test("com.mysql.jdbc.Driver",
156: // "jdbc:mysql://localhost/test", "sa", ""),
157: new Test("org.postgresql.Driver",
158: "jdbc:postgresql:test", "sa", "sa", false),
159: new Test("org.apache.derby.jdbc.EmbeddedDriver",
160: "jdbc:derby:test;create=true", "sa", "", false),
161: new Test("org.h2.Driver", "jdbc:h2:test5", "sa", "",
162: true),
163: new Test("org.h2.Driver", "jdbc:h2:test6", "sa", "",
164: false), };
165: for (int i = 0;; i++) {
166: for (int j = 0; j < dbs.length; j++) {
167: dbs[j].insert(i);
168: }
169: out.writeInt(i);
170: out.flush();
171: }
172: }
173:
174: }
|