001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.tests;
024:
025: import java.sql.*;
026: import drjava.util.*;
027:
028: public class SQLBench extends Benchmark {
029: static final int n = 100000;
030: static final boolean lock = true;
031: Connection con;
032:
033: public SQLBench() {
034: setDescription("Inserting and deleting " + n + " rows");
035: }
036:
037: void create() throws SQLException {
038: con.createStatement().executeUpdate(
039: "drop table if exists people");
040: con.createStatement().executeUpdate(
041: "create table people (name varchar(30), age bigint)");
042: }
043:
044: void createIndex() throws SQLException {
045: con.createStatement().executeUpdate(
046: "create index age on people (age)");
047: }
048:
049: void optimizeTable() throws SQLException {
050: con.createStatement().executeUpdate("optimize table people");
051: }
052:
053: void lockTable() throws SQLException {
054: if (lock)
055: con.createStatement().executeUpdate(
056: "lock tables people write");
057: }
058:
059: void unlockTable() throws SQLException {
060: if (lock)
061: con.createStatement().executeUpdate("unlock tables");
062: }
063:
064: void add() throws SQLException {
065: for (int i = 0; i < n; i++)
066: con.createStatement().executeUpdate(
067: "insert people values ('" + i + "', " + i + ")");
068: done("insert " + n + " rows");
069: }
070:
071: void removeFromTop() throws SQLException {
072: for (int i = 0; i < n; i++)
073: con.createStatement().executeUpdate(
074: "delete from people where age=" + i);
075: done("remove all records from top");
076: }
077:
078: void addCombined() throws SQLException {
079: StringBuffer buf = new StringBuffer();
080: for (int i = 0; i < n; i++)
081: buf.append("insert people values ('" + i + "', " + i
082: + ");\n");
083: con.createStatement().executeUpdate(buf.toString());
084: done("insert " + n + " rows (combined)");
085: }
086:
087: protected void action() {
088: try {
089: Class.forName("org.gjt.mm.mysql.Driver").newInstance();
090: con = DriverManager
091: .getConnection("jdbc:mysql://localhost/smyletest?user=smyletest&password=smyletest");
092:
093: create();
094:
095: lockTable();
096: add();
097: unlockTable();
098: //addCombined(); //-->socket write error
099: createIndex();
100: //optimizeTable();
101: lockTable();
102: removeFromTop();
103: unlockTable();
104: //removeFromBottom();
105:
106: con.close();
107: } catch (Exception e) {
108: e.printStackTrace();
109: throw new RuntimeException(e.toString());
110: }
111: }
112:
113: public static void main(String[] args) {
114: SQLBench bench = new SQLBench();
115: bench.runAndPrint();
116: System.out.println("Records/s: "
117: + (long) (n / (bench.totalTime() * 0.001)));
118: }
119: }
|