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.File;
009: import java.io.FileDescriptor;
010: import java.io.RandomAccessFile;
011: import java.sql.Connection;
012: import java.sql.DriverManager;
013: import java.sql.PreparedStatement;
014: import java.sql.SQLException;
015: import java.sql.Statement;
016:
017: /**
018: * This test shows the raw file access performance using various file modes.
019: * It also tests databases.
020: */
021: public class TestWrite {
022: public static void main(String[] args) throws Exception {
023: testFile("rw", false);
024: testFile("rwd", false);
025: testFile("rws", false);
026: testFile("rw", true);
027: testFile("rwd", true);
028: testFile("rws", true);
029: testDatabase("org.h2.Driver", "jdbc:h2:test", "sa", "");
030: testDatabase("org.hsqldb.jdbcDriver", "jdbc:hsqldb:test4",
031: "sa", "");
032: testDatabase("org.apache.derby.jdbc.EmbeddedDriver",
033: "jdbc:derby:test;create=true", "sa", "");
034: testDatabase("com.mysql.jdbc.Driver",
035: "jdbc:mysql://localhost/test", "sa", "sa");
036: testDatabase("org.postgresql.Driver", "jdbc:postgresql:test",
037: "sa", "sa");
038: }
039:
040: static void testFile(String mode, boolean flush) throws Exception {
041: System.out.println("Testing RandomAccessFile(.., \"" + mode
042: + "\")...");
043: if (flush) {
044: System.out.println(" with FileDescriptor.sync()");
045: }
046: RandomAccessFile file = new RandomAccessFile("test.txt", mode);
047: file.setLength(0);
048: FileDescriptor fd = file.getFD();
049: long start = System.currentTimeMillis();
050: byte[] data = new byte[] { 0 };
051: file.write(data);
052: int i = 0;
053: if (flush) {
054: for (;; i++) {
055: file.seek(0);
056: file.write(data);
057: fd.sync();
058: if ((i & 15) == 0) {
059: long time = System.currentTimeMillis() - start;
060: if (time > 5000) {
061: break;
062: }
063: }
064: }
065: } else {
066: for (;; i++) {
067: file.seek(0);
068: file.write(data);
069: if ((i & 1023) == 0) {
070: long time = System.currentTimeMillis() - start;
071: if (time > 5000) {
072: break;
073: }
074: }
075: }
076: }
077: long time = System.currentTimeMillis() - start;
078: System.out.println("Time: " + time);
079: System.out.println("Operations: " + i);
080: System.out.println("Operations/second: " + (i * 1000 / time));
081: System.out.println();
082: file.close();
083: new File("test.txt").delete();
084: }
085:
086: static void testDatabase(String driver, String url, String user,
087: String password) throws Exception {
088: Class.forName(driver);
089: Connection conn = DriverManager.getConnection(url, user,
090: password);
091: System.out.println("Testing Database, URL=" + url);
092: Statement stat = conn.createStatement();
093: try {
094: stat.execute("DROP TABLE TEST");
095: } catch (SQLException e) {
096: // ignore
097: }
098: stat.execute("CREATE TABLE TEST(ID INT)");
099: PreparedStatement prep = conn
100: .prepareStatement("INSERT INTO TEST VALUES(?)");
101: long start = System.currentTimeMillis();
102: int i = 0;
103: for (;; i++) {
104: prep.setInt(1, i);
105: // autocommit is on by default, so this commits as well
106: prep.execute();
107: if ((i & 15) == 0) {
108: long time = System.currentTimeMillis() - start;
109: if (time > 5000) {
110: break;
111: }
112: }
113: }
114: long time = System.currentTimeMillis() - start;
115: System.out.println("Time: " + time);
116: System.out.println("Operations: " + i);
117: System.out.println("Operations/second: " + (i * 1000 / time));
118: System.out.println();
119: stat.execute("DROP TABLE TEST");
120: conn.close();
121: }
122:
123: }
|