KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > PerformanceNodeServiceTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.node;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.repo.dictionary.DictionaryComponent;
28 import org.alfresco.repo.dictionary.DictionaryDAO;
29 import org.alfresco.repo.dictionary.M2Model;
30 import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
31 import org.alfresco.repo.transaction.TransactionUtil;
32 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
33 import org.alfresco.service.cmr.dictionary.DictionaryService;
34 import org.alfresco.service.cmr.repository.ChildAssociationRef;
35 import org.alfresco.service.cmr.repository.ContentService;
36 import org.alfresco.service.cmr.repository.ContentWriter;
37 import org.alfresco.service.cmr.repository.NodeRef;
38 import org.alfresco.service.cmr.repository.NodeService;
39 import org.alfresco.service.cmr.repository.StoreRef;
40 import org.alfresco.service.namespace.QName;
41 import org.alfresco.service.transaction.TransactionService;
42 import org.alfresco.util.ApplicationContextHelper;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.springframework.context.ApplicationContext;
46
47 /**
48  * PerformanceNodeServiceTest
49  */

50 public class PerformanceNodeServiceTest extends TestCase
51 {
52     public static final String JavaDoc NAMESPACE = "http://www.alfresco.org/test/BaseNodeServiceTest";
53     public static final String JavaDoc TEST_PREFIX = "test";
54     public static final QName TYPE_QNAME_TEST = QName.createQName(NAMESPACE, "multiprop");
55     public static final QName PROP_QNAME_NAME = QName.createQName(NAMESPACE, "name");
56     public static final QName ASSOC_QNAME_CHILDREN = QName.createQName(NAMESPACE, "child");
57     
58     private int flushCount = Integer.MAX_VALUE;
59     
60     private int testDepth = 3;
61     private int testChildCount = 5;
62     private int testStringPropertyCount = 10;
63     private int testContentPropertyCount = 10;
64     
65     private static Log logger = LogFactory.getLog(PerformanceNodeServiceTest.class);
66     private static ApplicationContext applicationContext = ApplicationContextHelper.getApplicationContext();
67     
68     protected DictionaryService dictionaryService;
69     protected NodeService nodeService;
70     private ContentService contentService;
71     private TransactionService txnService;
72     
73     private int nodeCount = 0;
74     
75     private long startTime;
76     /** populated during setup */
77     protected NodeRef rootNodeRef;
78
79     @Override JavaDoc
80     protected void setUp() throws Exception JavaDoc
81     {
82         DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
83         
84         // load the system model
85
ClassLoader JavaDoc cl = PerformanceNodeServiceTest.class.getClassLoader();
86         InputStream JavaDoc modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
87         assertNotNull(modelStream);
88         M2Model model = M2Model.createModel(modelStream);
89         dictionaryDao.putModel(model);
90         
91         // load the test model
92
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
93         assertNotNull(modelStream);
94         model = M2Model.createModel(modelStream);
95         dictionaryDao.putModel(model);
96         
97         DictionaryComponent dictionary = new DictionaryComponent();
98         dictionary.setDictionaryDAO(dictionaryDao);
99         dictionaryService = loadModel(applicationContext);
100         
101         nodeService = (NodeService) applicationContext.getBean("nodeService");
102         txnService = (TransactionService) applicationContext.getBean("transactionComponent");
103         contentService = (ContentService) applicationContext.getBean("contentService");
104         
105         // create a first store directly
106
TransactionWork<NodeRef> createStoreWork = new TransactionWork<NodeRef>()
107         {
108             public NodeRef doWork()
109             {
110                 StoreRef storeRef = nodeService.createStore(
111                         StoreRef.PROTOCOL_WORKSPACE,
112                         "Test_" + System.nanoTime());
113                 return nodeService.getRootNode(storeRef);
114             }
115         };
116         rootNodeRef = TransactionUtil.executeInUserTransaction(txnService, createStoreWork);
117     }
118     
119     @Override JavaDoc
120     protected void tearDown()
121     {
122     }
123
124     /**
125      * Loads the test model required for building the node graphs
126      */

127     public static DictionaryService loadModel(ApplicationContext applicationContext)
128     {
129         DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
130         
131         // load the system model
132
ClassLoader JavaDoc cl = PerformanceNodeServiceTest.class.getClassLoader();
133         InputStream JavaDoc modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
134         assertNotNull(modelStream);
135         M2Model model = M2Model.createModel(modelStream);
136         dictionaryDao.putModel(model);
137         
138         // load the test model
139
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
140         assertNotNull(modelStream);
141         model = M2Model.createModel(modelStream);
142         dictionaryDao.putModel(model);
143         
144         DictionaryComponent dictionary = new DictionaryComponent();
145         dictionary.setDictionaryDAO(dictionaryDao);
146         
147         return dictionary;
148     }
149     
150     public void testSetUp() throws Exception JavaDoc
151     {
152         assertNotNull("StoreService not set", nodeService);
153         assertNotNull("NodeService not set", nodeService);
154         assertNotNull("rootNodeRef not created", rootNodeRef);
155     }
156     
157     public void testPerformanceNodeService() throws Exception JavaDoc
158     {
159         startTime = System.currentTimeMillis();
160         
161         // ensure that we execute the node tree building in a transaction
162
TransactionWork<Object JavaDoc> buildChildrenWork = new TransactionWork<Object JavaDoc>()
163         {
164             public Object JavaDoc doWork()
165             {
166                 buildNodeChildren(rootNodeRef, 1, testDepth, testChildCount);
167                 return null;
168             }
169         };
170         TransactionUtil.executeInUserTransaction(txnService, buildChildrenWork);
171         
172         long endTime = System.currentTimeMillis();
173         
174         System.out.println("Test completed: \n" +
175                 " Built " + nodeCount + " nodes in " + (endTime-startTime) + "ms \n" +
176                 " Depth: " + testDepth + "\n" +
177                 " Child count: " + testChildCount);
178     }
179     
180     public void buildNodeChildren(NodeRef parent, int level, int maxLevel, int childCount)
181     {
182         for (int i=0; i < childCount; i++)
183         {
184             ChildAssociationRef assocRef = this.nodeService.createNode(
185                     parent, ASSOC_QNAME_CHILDREN, QName.createQName(NAMESPACE, "child" + i), TYPE_QNAME_TEST);
186            
187             nodeCount++;
188             
189             NodeRef childRef = assocRef.getChildRef();
190              
191             this.nodeService.setProperty(childRef,
192                  ContentModel.PROP_NAME, "node" + level + "_" + i);
193
194             Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>(17);
195             for (int j = 0; j < testStringPropertyCount; j++)
196             {
197                 properties.put(
198                         QName.createQName(NAMESPACE, "string" + j),
199                         level + "_" + i + "_" + j);
200             }
201             this.nodeService.setProperties(childRef, properties);
202             
203             for (int j = 0; j < testContentPropertyCount; j++)
204             {
205                 ContentWriter writer = this.contentService.getWriter(
206                       childRef, QName.createQName(NAMESPACE, "content" + j), true);
207                 
208                 writer.setMimetype("text/plain");
209                 writer.putContent( level + "_" + i + "_" + j );
210             }
211             
212             long currentTime = System.currentTimeMillis();
213             long diffTime = (currentTime - startTime);
214             if (nodeCount % flushCount == 0)
215             {
216                System.out.println("Flushing transaction cache at nodecount: " + nodeCount);
217                System.out.println("At time index " + diffTime + "ms");
218                AlfrescoTransactionSupport.flush();
219             }
220             if (nodeCount % 100 == 0)
221             {
222                 System.out.println("Interim summary: \n" +
223                         " nodes: " + nodeCount + "\n" +
224                         " time: " + (double)diffTime/1000.0/60.0 + " minutes \n" +
225                         " average: " + (double)nodeCount/(double)diffTime*1000.0 + " nodes/s");
226             }
227             
228             if (level < maxLevel)
229             {
230                 buildNodeChildren(childRef, level + 1, maxLevel, childCount);
231             }
232         }
233     }
234     
235     /**
236      * Runs a test with more depth
237      */

238     public static void main(String JavaDoc[] args)
239     {
240         try
241         {
242             PerformanceNodeServiceTest test = new PerformanceNodeServiceTest();
243             test.setUp();
244             test.testChildCount = 5;
245             test.testDepth = 6;
246             test.flushCount = 1000;
247             
248             test.testPerformanceNodeService();
249             
250             test.tearDown();
251         }
252         catch (Throwable JavaDoc e)
253         {
254             e.printStackTrace();
255             System.exit(1);
256         }
257         System.exit(0);
258     }
259 }
260
Popular Tags