You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2012/08/13 18:45:08 UTC

svn commit: r1372504 - /labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/MultiThreadedBtreeTest.java

Author: elecharny
Date: Mon Aug 13 16:45:08 2012
New Revision: 1372504

URL: http://svn.apache.org/viewvc?rev=1372504&view=rev
Log:
Added a multi-threaded test

Added:
    labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/MultiThreadedBtreeTest.java

Added: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/MultiThreadedBtreeTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/MultiThreadedBtreeTest.java?rev=1372504&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/MultiThreadedBtreeTest.java (added)
+++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/MultiThreadedBtreeTest.java Mon Aug 13 16:45:08 2012
@@ -0,0 +1,206 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mavibot.btree;
+
+
+import java.io.IOException;
+import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.mavibot.btree.comparator.LongComparator;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+/**
+ * A class to test multi-threaded operations on the btree
+ *  
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class MultiThreadedBtreeTest
+{
+    /** The btree we use */
+    private static BTree<Long, String> btree;
+
+
+    /**
+     * Create the btree once
+     * @throws IOException If the creation failed
+     */
+    @BeforeClass
+    public static void setup() throws IOException
+    {
+        btree = new BTree<Long, String>( new LongComparator() );
+    }
+
+
+    /**
+     * Close the btree
+     */
+    @AfterClass
+    public static void shutdown()
+    {
+        btree.close();
+    }
+
+
+    /**
+     * Create a btree with 5 million elements in it
+     * @throws IOException If the creation failed
+     */
+    private void create5MBTree() throws IOException
+    {
+        Random random = new Random( System.nanoTime() );
+
+        int nbElems = 5000000;
+
+        // Create a BTree with 5 million entries
+        btree.setPageSize( 32 );
+
+        for ( int i = 0; i < nbElems; i++ )
+        {
+            Long key = ( long ) random.nextLong();
+            String value = Long.toString( key );
+
+            try
+            {
+                btree.insert( key, value );
+
+                if ( i % 100000 == 0 )
+                {
+                    System.out.println( "Written " + i + " elements" );
+                }
+            }
+            catch ( Exception e )
+            {
+                e.printStackTrace();
+                System.out.println( btree );
+                System.out.println( "Error while adding " + value );
+                return;
+            }
+        }
+    }
+
+
+    /**
+     * Browse the btree in its current revision, reading all of its elements
+     * @return The number of read elements 
+     * @throws IOException If the browse failed
+     */
+    private int testBrowse() throws IOException
+    {
+        Cursor<Long, String> cursor = btree.browse();
+
+        int nb = 0;
+        long elem = Long.MIN_VALUE;
+
+        while ( cursor.hasNext() )
+        {
+            Tuple<Long, String> res = cursor.next();
+
+            if ( res.getKey() > elem )
+            {
+                elem = res.getKey();
+                nb++;
+            }
+        }
+
+        long revision = cursor.getRevision();
+
+        cursor.close();
+
+        //System.out.println( Thread.currentThread().getName() + " Nb elements read : " + nb + " on revision : "
+        //    + revision );
+
+        return nb;
+    }
+
+
+    /**
+     * Chack that we can read the btree while it is being modified. We will start
+     * 1000 readers for one writer.
+     * 
+     * @throws InterruptedException If the btree access failed.
+     */
+    @Test
+    public void testBrowseMultiThreads() throws InterruptedException
+    {
+        int nbThreads = 1000;
+        final CountDownLatch latch = new CountDownLatch( nbThreads );
+
+        Thread writer = new Thread()
+        {
+            public void run()
+            {
+                try
+                {
+                    create5MBTree();
+                }
+                catch ( Exception e )
+                {
+                }
+            }
+        };
+
+        long t0 = System.currentTimeMillis();
+
+        // Start the writer
+        writer.start();
+
+        for ( int i = 0; i < nbThreads; i++ )
+        {
+            Thread test = new Thread()
+            {
+                public void run()
+                {
+                    try
+                    {
+                        int res = 0;
+                        int previous = -1;
+
+                        while ( previous < res )
+                        {
+                            previous = res;
+                            res = testBrowse();
+                            Thread.sleep( 100 );
+                        }
+
+                        latch.countDown();
+                    }
+                    catch ( Exception e )
+                    {
+                    }
+                }
+            };
+
+            // Start each reader
+            test.start();
+        }
+
+        // Wait for all the readers to be done
+        latch.await();
+
+        long t1 = System.currentTimeMillis();
+
+        System.out.println( " Time to create 5M entries and to have " + nbThreads + " threads reading them : "
+            + ( ( t1 - t0 ) / 1000 ) + " seconds" );
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org