1 18 package org.apache.activemq.kaha; 19 20 import java.util.Iterator ; 21 import java.util.Set ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import java.util.concurrent.CountDownLatch ; 27 32 class Loader extends Thread { 33 34 protected static final Log log = LogFactory.getLog(Loader.class); 35 36 private String name; 37 private Store store; 38 private int count; 39 private CountDownLatch start; 40 private CountDownLatch stop; 41 42 public Loader(String name,Store store,int count,CountDownLatch start,CountDownLatch stop){ 43 this.name=name; 44 this.store=store; 45 this.count=count; 46 this.start = start; 47 this.stop = stop; 48 } 49 50 public void run(){ 51 try{ 52 start.countDown(); 53 start.await(); 54 Marshaller keyMarshaller=new StringMarshaller(); 55 Marshaller valueMarshaller=new BytesMarshaller(); 56 MapContainer container=store.getMapContainer(name,Store.DEFAULT_CONTAINER_NAME,true); 57 58 container.setKeyMarshaller(keyMarshaller); 59 container.setValueMarshaller(valueMarshaller); 60 container.load(); 61 Object value=getData(1024); 63 long startTime=System.currentTimeMillis(); 64 long startLoad=System.currentTimeMillis(); 65 for(int i=0;i<count;i++){ 66 String key="key:"+i; 67 container.put(key,value); 68 } 69 long finishLoad=System.currentTimeMillis(); 70 long totalLoadTime=finishLoad-startLoad; 71 log.info("name "+name+" load time = "+totalLoadTime+"(ms)"); 72 73 Set keys=container.keySet(); 74 long startExtract=System.currentTimeMillis(); 75 76 for(Iterator i=keys.iterator();i.hasNext();){ 77 byte[] data=(byte[]) container.get(i.next()); 78 } 79 long finishExtract=System.currentTimeMillis(); 80 long totalExtractTime=finishExtract-startExtract; 81 log.info("name "+name+" extract time = "+totalExtractTime+"(ms)"); 82 83 long startRemove=System.currentTimeMillis(); 84 for(Iterator i=keys.iterator();i.hasNext();){ 85 container.remove(i.next()); 86 } 87 long finishRemove = System.currentTimeMillis(); 88 long totalRemoveTime = finishRemove-startRemove; 89 log.info("name "+name+" remove time = "+totalRemoveTime+"(ms)"); 90 startLoad=System.currentTimeMillis(); 92 value = getData(2048); 93 for(int i=0;i<count;i++){ 94 String key="key:"+i; 96 container.put(key,value); 97 } 98 finishLoad=System.currentTimeMillis(); 99 totalLoadTime=finishLoad-startLoad; 100 log.info("name "+name+" 2nd load time = "+totalLoadTime+"(ms)"); 101 102 103 }catch(Exception e){ 104 e.printStackTrace(); 105 }finally{ 106 stop.countDown(); 107 } 108 } 109 110 byte[] getData(int size){ 111 byte[] result=new byte[size]; 112 for(int i=0;i<size;i++){ 113 result[i]='a'; 114 } 115 return result; 116 } 117 } 118 | Popular Tags |