1 package org.apache.ojb.performance; 2 3 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 27 public abstract class PerfHandle implements Runnable 28 { 29 private String PREFIX_LOG = "[" + this.getClass().getName() + "] "; 30 private PerfTest test; 31 private String objectName; 32 33 public PerfHandle(PerfTest test) 34 { 35 this.test = test; 36 } 37 38 41 public abstract void init() throws Exception ; 42 43 46 public abstract void tearDown() throws Exception ; 47 48 52 public abstract void insertNewArticles(PerfArticle[] arr) throws Exception ; 53 54 58 public abstract void insertNewArticlesStress(PerfArticle[] arr) throws Exception ; 59 60 67 public abstract Collection readArticlesByCursor(String articleName) throws Exception ; 68 69 73 public abstract void deleteArticles(PerfArticle[] arr) throws Exception ; 74 75 79 public abstract void deleteArticlesStress(PerfArticle[] arr) throws Exception ; 80 81 85 public abstract void updateArticles(PerfArticle[] arr) throws Exception ; 86 87 91 public abstract void updateArticlesStress(PerfArticle[] arr) throws Exception ; 92 93 102 public PerfArticle newPerfArticle() 103 { 104 return new PerfArticleImpl(); 105 } 106 107 114 public String getTestObjectName() 115 { 116 if (objectName == null) 117 objectName = test.testName() + "_" + 118 Thread.currentThread().toString() + "_" + test.getPerfTestId(); 119 return objectName; 120 } 121 122 131 protected PerfArticle getPreparedPerfArticle(String articleName) 132 { 133 PerfArticle a = newPerfArticle(); 134 a.setArticleName(articleName); 135 a.setMinimumStock(100); 136 a.setPrice(0.45); 137 a.setProductGroupId(1); 138 a.setStock(234); 139 a.setSupplierId(4); 140 a.setUnit("bottle"); 141 return a; 142 } 143 144 147 public void run() 148 { 149 PerfArticle[] m_arr = new PerfArticle[PerfMain.getIterationsPerThread()]; 150 for (int i = 0; i < PerfMain.getIterationsPerThread(); i++) 151 { 152 PerfArticle a = getPreparedPerfArticle(getTestObjectName()); 153 m_arr[i] = a; 154 } 155 156 try 157 { 158 long period = 0; 159 init(); 160 161 if (!PerfMain.isUseStressMode()) 163 { 164 period = System.currentTimeMillis(); 165 insertNewArticles(m_arr); 166 period = System.currentTimeMillis() - period; 167 test.addTime(1, period); 168 } 169 else 170 { 171 period = System.currentTimeMillis(); 172 insertNewArticlesStress(m_arr); 173 period = System.currentTimeMillis() - period; 174 test.addTime(1, period); 175 } 176 177 period = System.currentTimeMillis(); 179 Collection col = readArticlesByCursor(objectName); 180 period = System.currentTimeMillis() - period; 181 try 182 { 183 checkQueryResult(col, m_arr); 184 } 185 catch (Exception e) 186 { 187 test.registerException(PREFIX_LOG + "(Something wrong with query result) ", e); 188 } 189 test.addTime(2, period); 190 191 modifyPerfArticle(m_arr); 193 if (!PerfMain.isUseStressMode()) 194 { 195 period = System.currentTimeMillis(); 196 updateArticles(m_arr); 197 period = System.currentTimeMillis() - period; 198 test.addTime(3, period); 199 } 200 else 201 { 202 period = System.currentTimeMillis(); 203 deleteArticlesStress(m_arr); 204 period = System.currentTimeMillis() - period; 205 test.addTime(3, period); 206 } 207 208 if (!PerfMain.isUseStressMode()) 210 { 211 period = System.currentTimeMillis(); 212 deleteArticles(m_arr); 213 period = System.currentTimeMillis() - period; 214 test.addTime(4, period); 215 } 216 else 217 { 218 period = System.currentTimeMillis(); 219 deleteArticlesStress(m_arr); 220 period = System.currentTimeMillis() - period; 221 test.addTime(4, period); 222 } 223 m_arr = null; 224 tearDown(); 225 } 226 catch (Exception e) 227 { 228 e.printStackTrace(); 229 test.registerException(PREFIX_LOG + "(Unexpected behaviour) ", e); 230 test.interruptThreads(); 231 } 232 } 233 234 private void modifyPerfArticle(PerfArticle[] m_arr) 235 { 236 PerfArticle article; 237 String prefix = "updated_"; 238 for (int i = 0; i < m_arr.length; i++) 239 { 240 article = m_arr[i]; 241 article.setArticleName(prefix + article.getArticleName()); 242 } 243 } 244 245 private void checkQueryResult(Collection col, PerfArticle[] m_arr) throws Exception 246 { 247 if(col.size() > 0) 248 { 249 Iterator it = col.iterator(); 250 Object obj = it.next(); 251 if(!(obj instanceof PerfArticle)) 252 { 253 throw new Exception ("Wrong object type found. Expected instance of"+ 254 PerfArticle.class.getName() + ", found " + obj.getClass().getName()); 255 } 256 } 257 if (col.size() != m_arr.length) 258 { 259 throw new Exception ("Read objects: Wrong number of objects found. Expected " + 260 (m_arr.length) + ", found " + col.size()); 261 } 262 } 263 } 264 | Popular Tags |