@Override protected void setUp() throws Exception { super.setUp(); if (!performanceTestsEnabled()) return; // Populate documents into the database: char[] chars = new char[getSizeOfDocument()]; Arrays.fill(chars, 'a'); final String content = new String(chars); boolean success = database.runInTransaction( new TransactionalTask() { public boolean run() { for (int i = 0; i < getNumberOfDocuments(); i++) { try { Map<String, Object> props = new HashMap<String, Object>(); props.put("content", content); Document doc = database.createDocument(); doc.putProperties(props); } catch (CouchbaseLiteException e) { Log.e(TAG, "Error when creating a document", e); return false; } } return true; } }); assertTrue(success); }
public void runInPerformanceMode( Object testCase, String className, boolean junitTest, String testNameInDb) throws Exception { boolean increaseIterations = true; int iterations = 1; long duration = 0; mIntermediates = null; mInternalIterations = 1; Class clazz = mContext.getClassLoader().loadClass(className); Object perftest = clazz.newInstance(); PerformanceTestCase perftestcase = null; if (perftest instanceof PerformanceTestCase) { perftestcase = (PerformanceTestCase) perftest; // only run the test if it is not marked as a performance only test if (mMode == REGRESSION && perftestcase.isPerformanceOnly()) return; } // First force GCs, to avoid GCs happening during out // test and skewing its time. Runtime.getRuntime().runFinalization(); Runtime.getRuntime().gc(); if (perftestcase != null) { mIntermediates = new ArrayList<IntermediateTime>(); iterations = perftestcase.startPerformance(this); if (iterations > 0) { increaseIterations = false; } else { iterations = 1; } } // Pause briefly to let things settle down... Thread.sleep(1000); do { mEndTime = 0; if (increaseIterations) { // Test case does not implement // PerformanceTestCase or returned 0 iterations, // so we take care of measure the whole test time. mStartTime = SystemClock.currentThreadTimeMillis(); } else { // Try to make it obvious if the test case // doesn't call startTiming(). mStartTime = 0; } if (junitTest) { for (int i = 0; i < iterations; i++) { junit.textui.TestRunner.run((junit.framework.Test) testCase); } } else { Runnable test = (Runnable) testCase; for (int i = 0; i < iterations; i++) { test.run(); } } long endTime = mEndTime; if (endTime == 0) { endTime = SystemClock.currentThreadTimeMillis(); } duration = endTime - mStartTime; if (!increaseIterations) { break; } if (duration <= 1) { iterations *= 1000; } else if (duration <= 10) { iterations *= 100; } else if (duration < 100) { iterations *= 10; } else if (duration < 1000) { iterations *= (int) ((1000 / duration) + 2); } else { break; } } while (true); if (duration != 0) { iterations *= mInternalIterations; performance(testNameInDb, (duration * 1000000) / iterations, iterations, mIntermediates); } }