changeset 193:6819a376de75

add mongoDB benchMark from DataBaseBenchMark.java
author tatsuki
date Thu, 30 Apr 2015 21:44:38 +0900
parents 35f3be8d9ffe
children 87c7c86ecca2
files build.gradle src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/DataBaseBenchMark.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/DataBaseBenchMarkThread.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/JungleBenchMark.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/JungleBenchMarkThread.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/findMongoAttributeThread.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/findTreeAttributeThread.java src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/readTreeAttributeThread.java
diffstat 8 files changed, 224 insertions(+), 148 deletions(-) [+]
line wrap: on
line diff
--- a/build.gradle	Thu Apr 30 06:52:06 2015 +0900
+++ b/build.gradle	Thu Apr 30 21:44:38 2015 +0900
@@ -7,29 +7,30 @@
 version = '1.0'
 
 repositories {
-  mavenCentral()
+    mavenCentral()
 }
 
 dependencies {
-  compile "commons-collections:commons-collections:3.2.1"
+    compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.0.0'
+    compile "commons-collections:commons-collections:3.2.1"
     compile "org.apache.maven.surefire:surefire-junit4:2.13"
     compile "com.google.guava:guava:12.0"
-       compile fileTree(dir: 'lib', include: '*.jar')
+    compile fileTree(dir: 'lib', include: '*.jar')
     testCompile "junit:junit:4.7"
 }
 
 jar {
-  manifest {
-    attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
-  }
-  from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
-  archiveName = 'jungle-core.jar'
+    manifest {
+        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
+    }
+    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
+    archiveName = 'jungle-core.jar'
 }
 
 uploadArchives {
-  repositories {
-    flatDir {
-      dirs '.'
+    repositories {
+        flatDir {
+            dirs '.'
+        }
     }
-  }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/DataBaseBenchMark.java	Thu Apr 30 21:44:38 2015 +0900
@@ -0,0 +1,160 @@
+package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.test;
+
+
+import com.mongodb.*;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.util.Random;
+
+/**
+ * Created by e115731 on 15/03/20.
+ */
+public class DataBaseBenchMark {
+    public static void main(String[] args) throws InterruptedException, IOException, ParserConfigurationException, SAXException {
+
+        if (args.length < 2) {
+            System.out.println("args[0] = jungle or mongo, args[1] = read or write");
+            System.exit(0);
+        }
+
+        Runtime rt = Runtime.getRuntime();
+        int cpuNum = rt.availableProcessors();
+
+        if (args[0].equals("jungle"))
+            jungleBench(args, cpuNum);
+        else if (args[0].equals("mongo"))
+            mongoBench(args, cpuNum);
+    }
+
+    private static void mongoBench(String[] args, int cpuNum) throws IOException, InterruptedException {
+        MongoClient client = new MongoClient("localhost", 27017);
+        client.setWriteConcern(WriteConcern.JOURNALED);
+        //client.dropDatabase("test");
+        DB db = client.getDB("test");
+        DBCollection coll = null;
+        if (!db.collectionExists("testCollection")) {
+            coll = db.getCollection("testCollection");
+            for (int i = 0; i < 1000; i++) {
+                if (i % 100 == 0)
+                    System.out.println(i / 100);
+                coll.insert(new BasicDBObject("key", String.valueOf(i)));
+            }
+        } else
+            coll = db.getCollection("testCollection");
+        coll.createIndex(new BasicDBObject("key", 1));
+        File file = new File("./time/" + args[0] + args[1] + "Time");
+        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
+        DataBaseBenchMarkThread readThread[] = null;
+        for (int THREAD_COUNT = 1; THREAD_COUNT <= cpuNum; THREAD_COUNT++) {
+            readThread = new DataBaseBenchMarkThread[THREAD_COUNT];
+            for (int count = 0; THREAD_COUNT > count; count++) {
+                readThread[count] = new findMongoAttributeThread(coll);
+            }
+            for (int count = 0; THREAD_COUNT > count; count++) {
+                readThread[count].start();
+            }
+            System.out.println("StartThread");
+            Thread.sleep(1000);
+            long readCount = 0;
+            for (int count = 0; THREAD_COUNT > count; count++) {
+                readCount = readCount + readThread[count].getFindCount();
+                readThread[count].set(false);
+                readThread[count] = null;
+            }
+            pw.println(THREAD_COUNT + " " + readCount);
+            System.out.println(THREAD_COUNT + "readCount = " + readCount);
+            System.gc();
+            System.out.println("-------------GC--------------");
+            Thread.sleep(1000);
+        }
+        client.close();
+        pw.close();
+    }
+
+    private static void jungleBench(String[] args, int cpuNum) throws IOException, InterruptedException {
+        JungleTree tree = createJungleTree(cpuNum);
+        File file = new File("./time/" + args[0] + args[0] + "Time");
+        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
+        DataBaseBenchMarkThread readThread[] = null;
+
+        for (int THREAD_COUNT = 1; THREAD_COUNT <= cpuNum; THREAD_COUNT++) {
+            readThread = new DataBaseBenchMarkThread[THREAD_COUNT];
+            for (int count = 0; THREAD_COUNT > count; count++) {
+                readThread[count] = new findTreeAttributeThread(tree);
+            }
+            JungleWriteThread writeThread = null;
+            if (args[1].equals("write")) {
+                writeThread = new JungleWriteThread(tree);
+                writeThread.start();
+            }
+            for (int count = 0; THREAD_COUNT > count; count++) {
+                readThread[count].start();
+            }
+            System.out.println("StartThread");
+            Thread.sleep(1000);
+            if (args[1].equals("write")) {
+                writeThread.set(false);
+                writeThread.get();
+            }
+            long readCount = 0;
+            for (int count = 0; THREAD_COUNT > count; count++) {
+                readCount = readCount + readThread[count].getFindCount();
+                readThread[count].set(false);
+                readThread[count] = null;
+            }
+            pw.println(THREAD_COUNT + " " + readCount);
+            System.out.println(THREAD_COUNT + "readCount = " + readCount);
+            System.gc();
+            System.out.println("-------------GC--------------");
+            Thread.sleep(1000);
+        }
+        pw.close();
+    }
+
+    private static JungleTree createJungleTree(int cpuNum) {
+        Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser()));
+        JungleTree trees[] = new JungleTree[cpuNum];
+        JungleTree tree = jungle.createNewTree("tree");
+        JungleTreeEditor editor = tree.getTreeEditor();
+        editor = editor.putAttribute(new DefaultNodePath(), "key", ByteBuffer.wrap(String.valueOf(0).getBytes())).b();
+        JungleTreeEditor newEditor = createTree(0, new DefaultNodePath(), editor);
+        if (newEditor.success().isA()) {
+            System.out.println("success faild");
+            System.exit(1);
+        }
+        trees[0] = tree;
+        return tree;
+    }
+
+    static Integer nodeNum = 1;
+
+    public static JungleTreeEditor createTree(int deep, NodePath path, JungleTreeEditor editor) {
+
+        Random rnd = new Random();
+        String value1 = String.valueOf(nodeNum);
+        nodeNum++;
+        String value2 = String.valueOf(nodeNum);
+        nodeNum++;
+        editor = editor.addNewChildAt(path, 0).b();
+        editor = editor.putAttribute(path.add(0), "key", ByteBuffer.wrap(value1.getBytes())).b();
+        editor = editor.addNewChildAt(path, 1).b();
+        editor = editor.putAttribute(path.add(1), "key", ByteBuffer.wrap(value2.getBytes())).b();
+        deep++;
+        if (deep < 10) {
+            editor = createTree(deep, path.add(0), editor);
+            editor = createTree(deep, path.add(1), editor);
+        }
+        return editor;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/DataBaseBenchMarkThread.java	Thu Apr 30 21:44:38 2015 +0900
@@ -0,0 +1,9 @@
+package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.test;
+
+/**
+ * Created by e115731 on 15/03/17.
+ */
+public abstract class DataBaseBenchMarkThread extends Thread{
+    public abstract long getFindCount();
+    public abstract void set(boolean flag);
+}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/JungleBenchMark.java	Thu Apr 30 06:52:06 2015 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,123 +0,0 @@
-package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.test;
-
-
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
-import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
-import org.xml.sax.SAXException;
-
-import javax.xml.parsers.ParserConfigurationException;
-import java.io.*;
-import java.nio.ByteBuffer;
-import java.util.Random;
-
-/**
- * Created by e115731 on 15/03/20.
- */
-public class JungleBenchMark {
-    public static void main(String[] args) throws InterruptedException, IOException, ParserConfigurationException, SAXException {
-
-
-        if (args.length < 1) {
-            System.out.println("args[0] = read or find, args[1] = separate or nonSeparate");
-            System.exit(0);
-        }
-
-        Runtime rt = Runtime.getRuntime();
-        int cpuNum = rt.availableProcessors();
-
-        Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser()));
-        JungleTree trees[] = new JungleTree[cpuNum];
-
-        JungleTree tree = jungle.createNewTree("tree");
-        JungleTreeEditor editor = tree.getTreeEditor();
-        editor = editor.putAttribute(new DefaultNodePath(), "key", ByteBuffer.wrap(String.valueOf(0).getBytes())).b();
-        JungleTreeEditor newEditor = createTree(0, new DefaultNodePath(), editor);
-        if (newEditor.success().isA()) {
-            System.out.println("success faild");
-            System.exit(1);
-        }
-        trees[0] = tree;
-
-        File file = new File("./time/" + args[1] + "JungleTree" + args[0] + "Time");
-        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
-        JungleBenchMarkThread readThread[] = null;
-
-        for (int THREAD_COUNT = 1; THREAD_COUNT <= cpuNum; THREAD_COUNT++) {
-
-            readThread = new JungleBenchMarkThread[THREAD_COUNT];
-
-            if (args[0].equals("read")) {
-                for (int count = 0; THREAD_COUNT > count; count++) {
-                    readThread[count] = new readTreeAttributeThread(tree);
-                }
-            } else if (args[0].equals("find")) {
-                for (int count = 0; THREAD_COUNT > count; count++) {
-                    readThread[count] = new findTreeAttributeThread(tree);
-                }
-            } else {
-                System.out.println("not allow args");
-                System.exit(0);
-            }
-
-            JungleWriteThread writeThread = null;
-            if (args[1].equals("write")) {
-                writeThread = new JungleWriteThread(tree);
-                writeThread.start();
-            }
-            for (int count = 0; THREAD_COUNT > count; count++) {
-                readThread[count].start();
-            }
-
-            System.out.println("StartThread");
-
-            Thread.sleep(1000);
-            if (args[1].equals("write")) {
-                writeThread.set(false);
-                writeThread.get();
-            }
-            long readCount = 0;
-            for (int count = 0; THREAD_COUNT > count; count++) {
-                readCount = readCount + readThread[count].getFindCount();
-                readThread[count].set(false);
-                readThread[count] = null;
-            }
-
-            pw.println(THREAD_COUNT + " " + readCount);
-            System.out.println(THREAD_COUNT + "readCount = " + readCount);
-            System.gc();
-            System.out.println("-------------GC--------------");
-            Thread.sleep(1000);
-        }
-
-        pw.close();
-    }
-
-    static Integer nodeNum = 1;
-
-    public static JungleTreeEditor createTree(int deep, NodePath path, JungleTreeEditor editor) {
-
-        Random rnd = new Random();
-        String value1 = String.valueOf(nodeNum);
-        nodeNum++;
-        String value2 = String.valueOf(nodeNum);
-        nodeNum++;
-
-        editor = editor.addNewChildAt(path, 0).b();
-        editor = editor.putAttribute(path.add(0), "key", ByteBuffer.wrap(value1.getBytes())).b();
-        editor = editor.addNewChildAt(path, 1).b();
-        editor = editor.putAttribute(path.add(1), "key", ByteBuffer.wrap(value2.getBytes())).b();
-        deep++;
-
-        if (deep < 10) {
-            editor = createTree(deep, path.add(0), editor);
-            editor = createTree(deep, path.add(1), editor);
-        }
-        return editor;
-    }
-}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/JungleBenchMarkThread.java	Thu Apr 30 06:52:06 2015 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.test;
-
-/**
- * Created by e115731 on 15/03/17.
- */
-public abstract class JungleBenchMarkThread extends Thread{
-    public abstract long getFindCount();
-    public abstract void set(boolean flag);
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/findMongoAttributeThread.java	Thu Apr 30 21:44:38 2015 +0900
@@ -0,0 +1,40 @@
+package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.test;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+
+/**
+ * Created by e115731 on 15/04/30.
+ */
+public class findMongoAttributeThread extends DataBaseBenchMarkThread {
+    private long findCount;
+    boolean loop = true;
+    DBCollection coll;
+
+    public findMongoAttributeThread(DBCollection coll) {
+        this.coll = coll;
+        this.findCount = 0;
+    }
+
+    @Override
+    public long getFindCount() {
+        System.out.println("thread count = " + findCount);
+        return findCount;
+    }
+
+    public void set(boolean loop) {
+        this.loop = loop;
+    }
+
+    @Override
+    public void run() {
+        BasicDBObject query = new BasicDBObject("key", "0");
+        while (loop) {
+            DBCursor cursor = coll.find(query);
+            if (cursor.hasNext()) {
+                findCount++;
+            }
+        }
+    }
+}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/findTreeAttributeThread.java	Thu Apr 30 06:52:06 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/findTreeAttributeThread.java	Thu Apr 30 21:44:38 2015 +0900
@@ -8,7 +8,7 @@
 /**
  * Created by e115731 on 15/03/20.
  */
-public class findTreeAttributeThread extends JungleBenchMarkThread {
+public class findTreeAttributeThread extends DataBaseBenchMarkThread {
 
     JungleTree tree;
     private long findCount;
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/readTreeAttributeThread.java	Thu Apr 30 06:52:06 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/test/readTreeAttributeThread.java	Thu Apr 30 21:44:38 2015 +0900
@@ -3,12 +3,10 @@
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
 
-import java.nio.ByteBuffer;
-
 /**
  * Created by e115731 on 15/03/17.
  */
-public class readTreeAttributeThread extends JungleBenchMarkThread {
+public class readTreeAttributeThread extends DataBaseBenchMarkThread {
 
     JungleTree tree;
     private long findCount;