JDBM4(MapDB)性能测试

最近需要一个大Map保存大量KV,找了一下最新的JDBM4(已经重命名为MapDB)来测了一下性能,性能不错~

1亿条记录测试:

     5千万读,5千万写:103417 ms

     1亿条记录读:1ms<

     十万条记录平均处理时间: 1295 ns

1亿条记录+(测试过3亿条)

     时间成线性增长

import java.io.File;
import java.util.Map;

import org.mapdb.DB;
import org.mapdb.DBMaker;


public class StatMapDB {
	private static final String MAP_NAME = "STAT_MAP";
	private String filePath;
	DB db = null;
	Map statMap = null;
	DBMod type = null;
	
	static enum DBMod
	{
		READ,
		WRITE
	}
	
	public StatMapDB(String filePath,DBMod type)
	{
		this.filePath = filePath;
		this.type = type;
		init();
	}
	
	private void init()
	{
		 File file = new File(filePath);
		 db = DBMaker
               .newFileDB(file)
               .transactionDisable()
               .asyncFlushDelay(100)
               .make();
		 if(type.equals(DBMod.WRITE))
		 {
			 if(file.exists())
			 {
				 file.delete();
				 new File(filePath + ".p").delete();
			 }
			 statMap = db.createTreeMap(MAP_NAME).make();
		 }
		 else{
			 statMap = db.getTreeMap(MAP_NAME);
		 }
	}
	
	public Map getStatMapDB()
	{
		return this.statMap;
	}
	
	public void close()
	{
		if(db!=null){
			db.close();
			db = null;
		}
	}
	
}

 

 

import java.util.Map;
import java.util.Map.Entry;

public class TestApp {

	private static final String PATH = "test";
	//private static long TOTAL = 10000;
	private static long TOTAL = 100000000;

	private static void write() {
		StatMapDB db = new StatMapDB(PATH, StatMapDB.DBMod.WRITE);
		Map map = db.getStatMapDB();
		long sum = 0;
		long count = 0;
		long startTime = System.currentTimeMillis();
		for (long i = -1 * TOTAL / 2; i < TOTAL / 2; i++) {
			Long key = Math.abs(i);
			long oneStartTime = System.nanoTime();
			if (!map.containsKey(key)) {
				map.put(key, key);
			} else {
				map.put(key, map.get(key) + key);
			}
			if (i % 100000 == 0) {
				sum += (System.nanoTime() - oneStartTime);
				count++;
			}

		}
		System.out.println("avg:" + sum / count + " ns");
		System.out.println("write 10 million times:" + (System.currentTimeMillis() - startTime) + " ms");
		db.close();
	}
	
	private static void read()
	{
		StatMapDB db = new StatMapDB(PATH, StatMapDB.DBMod.READ);
		Map map = db.getStatMapDB();
		long startTime = System.currentTimeMillis();
		for (Entry entry : map.entrySet()) {
			Long key = entry.getKey();
			Long value = entry.getValue();
		}
		System.out.println("read 10 million times:" + (System.currentTimeMillis() - startTime) + " ms");
		db.close();
	}

	public static void main(String[] args) {
		write();
		read();
	}

}

 

你可能感兴趣的:(Java)