Benchmark with MongoDB

Fast transactional nosql Application Database edited this page Apr 29, 2015 · 6 revisions
Results:
threadCount=100000 , batchCount=10
MongoDB(Default)
Database Transaction Test: None
MongoDB Insert&Counting:1000000  AVG:10521 objects/s

iBoxDB(File Mode)
Database Transaction Test: Succeeded
iBoxDB Insert&Counting:1000000  AVG:17981 objects/s
iBoxDB Update:1000000  AVG:12397 objects/s
iBoxDB UpdateNoIndex:1000000  AVG:18744 objects/s
iBoxDB Delete:1000000  AVG:22869 objects/s

iBoxDB(MemoryMappedFile Mode)
Database Transaction Test: Succeeded
iBoxDB Insert&Counting:1000000  AVG:38458 objects/s
iBoxDB Update:1000000  AVG:20200 objects/s
iBoxDB UpdateNoIndex:1000000  AVG:33119 objects/s
iBoxDB Delete:1000000  AVG:33342 objects/s

iBoxDB(InMemory Mode)
Database Transaction Test: Succeeded
iBoxDB Insert&Counting:1000000  AVG:39132 objects/s
iBoxDB Update:1000000  AVG:20226 objects/s
iBoxDB UpdateNoIndex:1000000  AVG:35202 objects/s
iBoxDB Delete:1000000  AVG:33658 objects/s

Code

package com;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel.MapMode;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

import com.mongodb.*;

import iBoxDB.LocalServer.*;
import iBoxDB.LocalServer.IO.*;

//  iBoxDB.java v1.4.1
//  mongodb-win32-i386-2.5.4  mongo-java-driver-2.9.3.jar
public class BenchmarkDBTest {

    static int threadCount = 100000;
    static int batchCount = 10;

    public static boolean UseMMAP = false;
    public static boolean UseMem = false;

    public static void main(String[] args) {
        try {

            System.out.println("threadCount=" + threadCount + " , batchCount="
                    + batchCount);

            System.out.println("MongoDB(Default)");
            TestMongoDB();
            System.out.println();

            System.gc();
            System.runFinalization();
            Thread.sleep(1000 * 15);

            UseMMAP = UseMem = false;
            System.out.println("iBoxDB(File Mode)");
            TestiBoxDB();
            System.out.println();

            // -Xmx1024m , following needs more memory
            System.gc();
            System.runFinalization();
            UseMMAP = UseMem = false;
            UseMMAP = true;
            System.out.println("iBoxDB(MemoryMappedFile Mode)");
            TestiBoxDB();
            System.out.println();

            System.gc();
            System.runFinalization();
            Thread.sleep(1000 * 15);

            UseMMAP = UseMem = false;
            UseMem = true;
            System.out.println("iBoxDB(InMemory Mode)");
            TestiBoxDB();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void TestiBoxDB() {
        BoxSystem.DBDebug.DeleteDBFiles(1);

        try (iBoxDBServer server = new iBoxDBServer()) {
            final Database db = server.getInstance();
            System.out.print("Database Transaction Test: ");
            Box box1 = db.cube();
            box1.insert("T1", T1.M(-1, Integer.toString(-1)));

            Box box2 = db.cube();
            box2.insert("T1", T1.M(-2, Integer.toString(-2)));

            List<LocalHashMap> transaction1 = T1
                    .toArray(box1.select("from T1"));
            List<LocalHashMap> transaction2 = T1
                    .toArray(box2.select("from T1"));
            if (transaction1.size() == 1
                    && transaction1.get(0).get("_id").equals(-1)
                    && transaction2.size() == 1
                    && transaction2.get(0).get("_id").equals(-2)) {
                System.out.println("Succeeded");
            } else {
                System.out.println("Failed");
            }
            box1.commit().Assert();
            box2.commit().Assert();

            long watch = System.currentTimeMillis();
            final AtomicInteger count = new AtomicInteger(0);
            ExecutorService pool = CreatePool();
            for (int i = 0; i < threadCount; i++) {
                final int p = i;
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        {
                            Box box = db.cube();
                            for (int i = 0; i < batchCount; i++) {
                                int id = (p * batchCount) + i;
                                box.insert("T1", T1.M(id, Integer.toString(id)));
                            }
                            box.commit().Assert();
                        }
                        {
                            AutoBox box = db.get();
                            int minId = p * batchCount + 0;
                            int maxId = p * batchCount + batchCount;
                            Iterator<LocalHashMap> reader = box
                                    .select("from T1 where _id>=? & _id<? order by _id",
                                            minId, maxId).iterator();
                            int ti = minId;
                            while (reader.hasNext()) {
                                Integer iv = (Integer) reader.next().get("_id");
                                if (ti != iv.intValue()) {
                                    System.out.println("e");
                                    throw new RuntimeException(ti + "  " + iv);
                                }
                                ti++;
                                count.incrementAndGet();
                            }
                            if (ti != maxId) {
                                System.out.println("e");
                                throw new RuntimeException();
                            }
                        }
                    }
                });
            }
            pool.shutdown();
            pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
            watch = System.currentTimeMillis() - watch;
            if (count.get() != (batchCount * threadCount)) {
                throw new RuntimeException(count + "  "
                        + (batchCount * threadCount));
            }
            int avg = (int) (count.get() / (watch / 1000.0));
            System.out.println("iBoxDB Insert&Counting:" + count.get()
                    + "  AVG:" + avg + " objects/s");

            // ----------------------Update------------------
            watch = System.currentTimeMillis();
            count.set(0);
            pool = CreatePool();
            for (int i = 0; i < threadCount; i++) {
                final int p = i;
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try (Box box = db.cube()) {
                            for (int i = 0; i < batchCount; i++) {
                                int id = (p * batchCount) + i;
                                Binder bd = box.bind("T1", id);
                                bd.update(bd.select());
                                count.incrementAndGet();
                            }
                            box.commit().Assert();
                        }
                    }
                });
            }
            pool.shutdown();
            pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
            watch = System.currentTimeMillis() - watch;
            if (count.get() != (batchCount * threadCount)) {
                throw new RuntimeException(count + "  "
                        + (batchCount * threadCount));
            }
            avg = (int) (count.get() / (watch / 1000.0));
            System.out.println("iBoxDB Update:" + count.get() + "  AVG:" + avg
                    + " objects/s");

            // ----------------------UpdateNoIndex------------------
            watch = System.currentTimeMillis();
            count.set(0);
            pool = CreatePool();
            for (int i = 0; i < threadCount; i++) {
                final int p = i;
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try (Box box = db.cube()) {
                            for (int i = 0; i < batchCount; i++) {
                                int id = (p * batchCount) + i;
                                Binder bd = box.bind("T1", id);
                                bd.updateNoIndex(bd.select());
                                count.incrementAndGet();
                            }
                            box.commit().Assert();
                        }
                    }
                });
            }
            pool.shutdown();
            pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
            watch = System.currentTimeMillis() - watch;
            if (count.get() != (batchCount * threadCount)) {
                throw new RuntimeException(count + "  "
                        + (batchCount * threadCount));
            }
            avg = (int) (count.get() / (watch / 1000.0));
            System.out.println("iBoxDB UpdateNoIndex:" + count.get() + "  AVG:"
                    + avg + " objects/s");
            // ------------------------Delete------------------
            watch = System.currentTimeMillis();
            count.set(0);
            pool = CreatePool();
            for (int i = 0; i < threadCount; i++) {
                final int p = i;
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try (Box box = db.cube()) {
                            for (int i = 0; i < batchCount; i++) {
                                int id = (p * batchCount) + i;
                                Binder bd = box.bind("T1", id);
                                bd.delete();
                                count.incrementAndGet();
                            }
                            box.commit().Assert();
                        }
                    }
                });
            }
            pool.shutdown();
            pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
            watch = System.currentTimeMillis() - watch;
            if (count.get() != (batchCount * threadCount)) {
                throw new RuntimeException(count + "  "
                        + (batchCount * threadCount));
            }
            avg = (int) (count.get() / (watch / 1000.0));
            System.out.println("iBoxDB Delete:" + count.get() + "  AVG:" + avg
                    + " objects/s");

            try (Box box = db.cube()) {
                // id = -1 ,-2 from transaction testing
                if (box.selectCount("from T1") != 2) {
                    throw new RuntimeException("SC");
                }
                for (Map<String, Object> m : box.select("from T1")) {
                    System.out.println(m.get("_id"));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void TestMongoDB() throws Exception {
        Mongo mongo = new Mongo();
        mongo.dropDatabase("test");
        com.mongodb.DB db = mongo.getDB("test");
        System.out.print("Database Transaction Test: ");
        DBCollection coll1 = db.getCollection("T1");
        coll1.insert(T1.O(-1, Integer.toString(-1)));

        DBCollection coll2 = db.getCollection("T1");
        coll2.insert(T1.O(-2, Integer.toString(-2)));

        List<DBObject> transaction1 = coll1.find().toArray();
        List<DBObject> transaction2 = coll2.find().toArray();
        if (transaction1.size() == 1
                && transaction1.get(0).get("_id").equals(-1)
                && transaction2.size() == 1
                && transaction2.get(0).get("_id").equals(-2)) {
            System.out.println("Succeeded");
        } else {
            System.out.println("None");
        }

        final DBCollection coll = db.getCollection("T1");

        long watch = System.currentTimeMillis();
        final AtomicInteger count = new AtomicInteger(0);
        ExecutorService pool = CreatePool();
        for (int i = 0; i < threadCount; i++) {
            final int p = i;
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    List<DBObject> list = new ArrayList<>(batchCount);
                    for (int i = 0; i < batchCount; i++) {
                        int id = (p * batchCount) + i;
                        list.add(T1.O(id, Integer.toString(id)));
                    }
                    coll.insert(list);

                    int minId = p * batchCount + 0;
                    int maxId = p * batchCount + batchCount;
                    BasicDBObject bd = new BasicDBObject();
                    bd.put("$gte", minId);
                    bd.put("$lt", maxId);
                    BasicDBObject q = new BasicDBObject();
                    q.put("_id", bd);

                    try (DBCursor reader = coll.find(q)) {
                        int ti = minId;
                        while (reader.hasNext()) {
                            Integer iv = (Integer) reader.next().get("_id");
                            if (ti != iv.intValue()) {
                                System.out.println("e");
                                throw new RuntimeException(ti + "  " + iv);
                            }
                            ti++;
                            count.incrementAndGet();
                        }
                        if (ti != maxId) {
                            System.out.println("e");
                            throw new RuntimeException();
                        }
                    }

                }
            });
        }
        pool.shutdown();
        pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
        watch = System.currentTimeMillis() - watch;
        if (count.get() != (batchCount * threadCount)) {
            throw new Exception(count + "  " + (batchCount * threadCount));
        }
        int avg = (int) (count.get() / (watch / 1000.0));
        System.out.println("MongoDB Insert&Counting:"
                + Integer.toString(count.get()) + "  AVG:"
                + Integer.toString(avg) + " objects/s");

        // ---------------Update-----------------------------
        // watch = System.currentTimeMillis();
        // count.set(0);
        // pool = CreatePool();
        // for (int i = 0; i < threadCount; i++) {
        // final int p = i;
        // pool.execute(new Runnable() {
        // @Override
        // public void run() {
        // for (int i = 0; i < batchCount; i++) {
        // int id = (p * batchCount) + i;
        // count.incrementAndGet();
        // BasicDBObject q = new BasicDBObject();
        // q.put("_id", id);
        // coll.save(coll.findOne(q));
        // }
        // }
        // });
        // }
        // pool.shutdown();
        // pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
        // watch = System.currentTimeMillis() - watch;
        // if (count.get() != (batchCount * threadCount)) {
        // throw new Exception(count + "  " + (batchCount * threadCount));
        // }
        // avg = (int) (count.get() / (watch / 1000.0));
        // System.out.println("MongoDB Save:" + Integer.toString(count.get())
        // + "  AVG:" + Integer.toString(avg) + " objects/s");

        mongo.close();
    }

    private static ExecutorService CreatePool() {
        return Executors.newFixedThreadPool(8);
    }

    public static class T1 {
        public int _id;

        public static BasicDBObject O(int id, String s) {
            BasicDBObject o = new BasicDBObject();
            o.put("_id", id);
            o.put("s", s);
            return o;
        }

        public static HashMap<String, Object> M(int id, String s) {
            HashMap<String, Object> o = new HashMap<String, Object>();
            o.put("_id", id);
            o.put("s", s);
            return o;
        }

        public static <T> List<T> toArray(Iterable<T> it) {
            ArrayList<T> list = new ArrayList<>();
            for (T t : it) {
                list.add(t);
            }
            return list;
        }
    }

    public static class iBoxDBServer extends LocalDatabaseServer {

        protected DatabaseConfig BuildDataBaseConfig(long address) {
            if (UseMem) {
                return new InMemoryConfig();
            } else if (UseMMAP) {
                return new CustomConstMMAP(BoxFileStreamConfig.BaseDirectory
                        + GetNameByAddr(address));
            } else {
                return new FileConfig();
            }
        }

        public static class FileConfig extends BoxFileStreamConfig {
            public FileConfig() {
                this.EnsureTable(T1.class, "T1", "_id");
            }
        }

        public static class InMemoryConfig extends BoxMemoryStreamConfig {
            public InMemoryConfig() {
                this.EnsureTable(T1.class, "T1", "_id");
                this.FileIncSize = 1024 * 1024 * 256;
                this.CachePageCount = 1024 * 128;
            }
        }

        // Custom DataBaseConfig
        public static class CustomConstMMAP extends DatabaseConfig {
            private final int MaxFileLength = 1024 * 1024 * 256;

            private RandomAccessFile file;
            private long fileLength;
            protected MappedByteBuffer mapReader;
            protected MappedByteBuffer mapWriter;

            public CustomConstMMAP(String name) {
                init(name);
                this.EnsureTable(T1.class, "T1", "_id");
                this.CachePageCount = 1024 * 128;
            }

            @Override
            public boolean ExistsStream(String arg0) {
                return false;
            }

            @Override
            public SwapType GetSwapType() {
                return SwapType.None;
            }

            @Override
            public void close() {
                if (file != null) {
                    try {
                        mapWriter.force();
                        file.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    file = null;
                }
                super.close();
            }

            private void init(String name) {
                try {
                    file = new RandomAccessFile(name, "rw");
                    fileLength = file.length();
                    if (fileLength < MaxFileLength) {
                        file.setLength(MaxFileLength);
                    }
                    mapWriter = file.getChannel().map(MapMode.READ_WRITE, 0,
                            file.length());
                    mapReader = (MappedByteBuffer) mapWriter.duplicate();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public Stream CreateStream(String arg0, FileAccess fa) {
                return new MapStream(fa == FileAccess.Read ? mapReader
                        : mapWriter);
            }

            class MapStream implements Stream {

                private MappedByteBuffer mbuffer;

                public MapStream(MappedByteBuffer _buffer) {
                    mbuffer = _buffer;
                }

                @Override
                public long Length() {
                    return fileLength;
                }

                @Override
                public void SetLength(long value) {
                    fileLength = value;
                }

                @Override
                public long Position() {
                    return mbuffer.position();
                }

                @Override
                public void Position(long pos) {
                    mbuffer.position((int) pos);
                }

                @Override
                public int Read(byte[] buffer, int offset, int count) {
                    mbuffer.get(buffer, offset, count);
                    return count;
                }

                @Override
                public void Write(byte[] buffer, int offset, int count) {
                    mbuffer.put(buffer, offset, count);
                }

                @Override
                public void Dispose() {
                }

                @Override
                public void Flush() {
                }

            }
        }

    }

}