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<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();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值