`
huangjun_mail
  • 浏览: 115256 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JDBM4(MapDB)性能测试

    博客分类:
  • Java
阅读更多

最近需要一个大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<Long,Long> 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<Long,Long> 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<Long,Long> 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<Long,Long> map = db.getStatMapDB();
		long startTime = System.currentTimeMillis();
		for (Entry<Long, Long> 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();
	}

}

 

分享到:
评论
2 楼 lintaozhou 2016-12-23  
这个file里存放是Map中的数据吗?
1 楼 在世界的中心呼喚愛 2014-07-31  
写入时间应该不准确

相关推荐

Global site tag (gtag.js) - Google Analytics