Search in sources :

Example 56 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMultiThread method testConcurrentAnalyze.

private void testConcurrentAnalyze() throws Exception {
    if (config.mvcc) {
        return;
    }
    deleteDb(getTestName());
    final String url = getURL("concurrentAnalyze;MULTI_THREADED=1", true);
    try (Connection conn = getConnection(url)) {
        Statement stat = conn.createStatement();
        stat.execute("create table test(id bigint primary key) " + "as select x from system_range(1, 1000)");
        Task t = new Task() {

            @Override
            public void call() throws SQLException {
                try (Connection conn2 = getConnection(url)) {
                    for (int i = 0; i < 1000; i++) {
                        conn2.createStatement().execute("analyze");
                    }
                }
            }
        };
        t.execute();
        Thread.yield();
        for (int i = 0; i < 1000; i++) {
            conn.createStatement().execute("analyze");
        }
        t.get();
        stat.execute("drop table test");
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 57 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMultiThread method testConcurrentSchemaChange.

private void testConcurrentSchemaChange() throws Exception {
    String db = getTestName();
    deleteDb(db);
    final String url = getURL(db + ";MULTI_THREADED=1;LOCK_TIMEOUT=10000", true);
    try (Connection conn = getConnection(url)) {
        Task[] tasks = new Task[2];
        for (int i = 0; i < tasks.length; i++) {
            final int x = i;
            Task t = new Task() {

                @Override
                public void call() throws Exception {
                    try (Connection c2 = getConnection(url)) {
                        Statement stat = c2.createStatement();
                        for (int i = 0; !stop; i++) {
                            stat.execute("create table test" + x + "_" + i);
                            c2.getMetaData().getTables(null, null, null, null);
                            stat.execute("drop table test" + x + "_" + i);
                        }
                    }
                }
            };
            tasks[i] = t;
            t.execute();
        }
        Thread.sleep(1000);
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 58 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMultiThreadedKernel method testCache.

private void testCache() throws Exception {
    ArrayList<Task> list = New.arrayList();
    int size = 3;
    final int count = 100;
    final Connection[] connections = new Connection[count];
    String url = getURL("multiThreadedKernel;" + "MULTI_THREADED=TRUE;CACHE_SIZE=1", true);
    for (int i = 0; i < size; i++) {
        final Connection conn = DriverManager.getConnection(url, getUser(), getPassword());
        connections[i] = conn;
        if (i == 0) {
            Statement stat = conn.createStatement();
            stat.execute("drop table test if exists");
            stat.execute("create table test(id int primary key, name varchar) " + "as select x, space(3000) from system_range(1, " + count + ")");
        }
        final Random random = new Random(i);
        Task t = new Task() {

            @Override
            public void call() throws SQLException {
                PreparedStatement prep = conn.prepareStatement("select * from test where id = ?");
                while (!stop) {
                    prep.setInt(1, random.nextInt(count));
                    prep.execute();
                }
            }
        };
        t.execute();
        list.add(t);
    }
    Thread.sleep(1000);
    for (Task t : list) {
        t.get();
    }
    for (int i = 0; i < size; i++) {
        connections[i].close();
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 59 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMultiThreadedKernel method testConcurrentRead.

private void testConcurrentRead() throws Exception {
    ArrayList<Task> list = New.arrayList();
    int size = 2;
    final int count = 1000;
    final Connection[] connections = new Connection[count];
    String url = getURL("multiThreadedKernel;" + "MULTI_THREADED=TRUE;CACHE_SIZE=16", true);
    for (int i = 0; i < size; i++) {
        final Connection conn = DriverManager.getConnection(url, getUser(), getPassword());
        connections[i] = conn;
        if (i == 0) {
            Statement stat = conn.createStatement();
            stat.execute("drop table test if exists");
            stat.execute("create table test(id int primary key, name varchar) " + "as select x, x || space(10) from system_range(1, " + count + ")");
        }
        final Random random = new Random(i);
        Task t = new Task() {

            @Override
            public void call() throws Exception {
                PreparedStatement prep = conn.prepareStatement("select * from test where id = ?");
                while (!stop) {
                    prep.setInt(1, random.nextInt(count));
                    prep.execute();
                }
            }
        };
        t.execute();
        list.add(t);
    }
    Thread.sleep(1000);
    for (Task t : list) {
        t.get();
    }
    for (int i = 0; i < size; i++) {
        connections[i].close();
    }
}
Also used : Task(org.h2.util.Task) Random(java.util.Random) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 60 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMVStoreCachePerformance method testCache.

private void testCache(int threadCount, String fileNamePrefix) {
    String fileName = getBaseDir() + "/" + getTestName();
    fileName = fileNamePrefix + fileName;
    FileUtils.delete(fileName);
    MVStore store = new MVStore.Builder().fileName(fileName).open();
    final MVMap<Integer, byte[]> map = store.openMap("test");
    final AtomicInteger counter = new AtomicInteger();
    byte[] data = new byte[8 * 1024];
    final int count = 10000;
    for (int i = 0; i < count; i++) {
        map.put(i, data);
        store.commit();
        if (i % 1000 == 0) {
        // System.out.println("add " + i);
        }
    }
    Task[] tasks = new Task[threadCount];
    for (int i = 0; i < threadCount; i++) {
        tasks[i] = new Task() {

            @Override
            public void call() throws Exception {
                Random r = new Random();
                do {
                    int id = r.nextInt(count);
                    map.get(id);
                    counter.incrementAndGet();
                } while (!stop);
            }
        };
        tasks[i].execute();
    }
    for (int i = 0; i < 4; i++) {
        // Profiler prof = new Profiler().startCollecting();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        // ignore
        }
    // System.out.println(prof.getTop(5));
    // System.out.println("  " + counter.get() / (i + 1) + " op/s");
    }
    // long time = System.nanoTime();
    for (Task t : tasks) {
        t.get();
    }
    store.close();
    System.out.println(counter.get() / 10000 + " ops/ms; " + threadCount + " thread(s); " + fileNamePrefix);
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Task (org.h2.util.Task)71 Connection (java.sql.Connection)33 Statement (java.sql.Statement)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)22 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)18 ResultSet (java.sql.ResultSet)14 JdbcConnection (org.h2.jdbc.JdbcConnection)7 IOException (java.io.IOException)5 ServerSocket (java.net.ServerSocket)5 Socket (java.net.Socket)5 ConcurrentModificationException (java.util.ConcurrentModificationException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SSLServerSocket (javax.net.ssl.SSLServerSocket)4 SSLSocket (javax.net.ssl.SSLSocket)4 OutputStream (java.io.OutputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3