KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > Loader


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import java.util.concurrent.CountDownLatch JavaDoc;
27 /**
28  * Store test
29  *
30  * @version $Revision: 1.2 $
31  */

32 class Loader extends Thread JavaDoc{
33     
34     protected static final Log log = LogFactory.getLog(Loader.class);
35
36     private String JavaDoc name;
37     private Store store;
38     private int count;
39     private CountDownLatch JavaDoc start;
40     private CountDownLatch JavaDoc stop;
41
42     public Loader(String JavaDoc name,Store store,int count,CountDownLatch JavaDoc start,CountDownLatch JavaDoc 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             // set data
62
Object JavaDoc value=getData(1024);
63             long startTime=System.currentTimeMillis();
64             long startLoad=System.currentTimeMillis();
65             for(int i=0;i<count;i++){
66                 String JavaDoc 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 JavaDoc keys=container.keySet();
74             long startExtract=System.currentTimeMillis();
75             
76             for(Iterator JavaDoc 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 JavaDoc 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             //re-insert data of longer length
91
startLoad=System.currentTimeMillis();
92             value = getData(2048);
93             for(int i=0;i<count;i++){
94                 //System.out.println(this + " Container size = " + container.size());
95
String JavaDoc 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 JavaDoc 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