import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import lotus.domino.AgentBase; import lotus.domino.Database; import lotus.domino.Document; import lotus.domino.NoteCollection; import lotus.domino.NotesException; import lotus.domino.View; import lotus.domino.ViewEntry; import lotus.domino.ViewNavigator; public class JavaAgent extends AgentBase { private final static int NUM_ITERATIONS = 20; private final static String TEST1_NOTE_IDS_BY_NAVIGATOR = "1. Fetching NoteIDs using ViewNavigator"; private final static String TEST2_NOTE_IDS_BY_NOTE_COLLECTION = "2. Fetching NoteIDs using view/NoteCollection"; private final static String TEST3_NOTE_IDS_BY_NOTE_COLLECTIONV9 = "3. Fetching NoteIDs using view/NoteCollection.getNoteIds (V9)"; private final static String TEST4_DOCUMENT_BY_NOTE_ID = "4. Traversing documents using Database.getDocumentById"; private final static String TEST5_DOCUMENT_FIELD_BY_NOTE_ID = "5. Traversing documents/fetching field value using Database.getDocumentById"; private final static String TEST6_DOCUMENT_BY_VIEW = "6. Traversing documents using View getFirst/getNext"; private final static String TEST7_DOCUMENT_FIELD_BY_VIEW = "7. Traversing documents/fetching field value using View getFirst/getNext"; private final static String TEST_VIEW_NAME = "(lupPersonModified)"; private final static String TEST_FIELD_NAME = "first_name"; private Database db; private Map> testResults = new HashMap>(); private void doTest() throws NotesException { // Create containers for test results this.testResults.put( TEST1_NOTE_IDS_BY_NAVIGATOR, new ArrayList() ); this.testResults.put( TEST2_NOTE_IDS_BY_NOTE_COLLECTION, new ArrayList() ); this.testResults.put( TEST3_NOTE_IDS_BY_NOTE_COLLECTIONV9, new ArrayList() ); this.testResults.put( TEST4_DOCUMENT_BY_NOTE_ID, new ArrayList() ); this.testResults.put( TEST6_DOCUMENT_BY_VIEW, new ArrayList() ); this.testResults.put( TEST5_DOCUMENT_FIELD_BY_NOTE_ID, new ArrayList() ); this.testResults.put( TEST7_DOCUMENT_FIELD_BY_VIEW, new ArrayList() ); for ( int i = 1; i <= NUM_ITERATIONS; i++ ) { System.out.println( "Test number: " + i ); this.testNoteIdsByNavigator(); this.testNoteIdsByNoteCollection(); this.testNoteIdsByNoteCollectionV9(); this.testDocsFromNotesIDs(); this.testDocsFromNotesIDsFetchValue(); this.testDocsFromView(); this.testDocsFromViewFetchValue(); } } private List getNoteIdsByNoteCollection( View notesView ) throws NotesException { List noteIds = new ArrayList(); NoteCollection collection = this.db.createNoteCollection( false ); collection.setSelectDocuments( true ); collection.setSelectionFormula( notesView.getSelectionFormula() ); collection.buildCollection(); String noteId = collection.getFirstNoteID(); while ( !noteId.isEmpty() ) { noteIds.add( noteId ); noteId = collection.getNextNoteID( noteId ); } collection.recycle(); return noteIds; } private List getNoteIdsByNoteCollectionV9( View notesView ) throws NotesException { notesView.setAutoUpdate( false ); List noteIds = new ArrayList(); NoteCollection collection = this.db.createNoteCollection( false ); collection.setSelectDocuments( true ); collection.setSelectionFormula( notesView.getSelectionFormula() ); collection.buildCollection(); int[] noteIdsArray = collection.getNoteIDs(); int numNoteIds = noteIdsArray.length; for ( int i = 0; i < numNoteIds; i++ ) { // Note ids needs to be converted to hex to be usable for lookups noteIds.add( Integer.toHexString( noteIdsArray[i] ) ); } collection.recycle(); return noteIds; } private List getNoteIdsByViewNavigator( View notesView ) throws NotesException { List noteIds = new ArrayList(); notesView.setAutoUpdate( false ); ViewNavigator navigator = notesView.createViewNav(); navigator.setBufferMaxEntries( 400 ); navigator.setEntryOptions( ViewNavigator.VN_ENTRYOPT_NOCOUNTDATA + ViewNavigator.VN_ENTRYOPT_NOCOLUMNVALUES ); ViewEntry entry = navigator.getFirst(); while ( entry != null ) { ViewEntry nextEntry = navigator.getNext(); noteIds.add( entry.getNoteID() ); entry.recycle(); entry = nextEntry; } return noteIds; } public void NotesMain() { try { this.db = getSession().getCurrentDatabase(); PrintWriter agentOutput = getAgentOutput(); agentOutput.println( "content-type:text/html;charset=utf-8" ); agentOutput.println( "" ); this.doTest(); List testNames = new ArrayList( this.testResults.keySet() ); Collections.sort( testNames ); // Print min/max/average agentOutput.println( "

Results after " + NUM_ITERATIONS + " iterations

" ); agentOutput.println( "Documents in view: " + this.db.getView( TEST_VIEW_NAME ).getAllEntries().getCount() + "" ); agentOutput.println( "" ); agentOutput.println( "" ); for ( String testName : testNames ) { int sum = 0; int max = 0; int min = 0; List testTimes = this.testResults.get( testName ); for ( Long testTime : testTimes ) { int testTimeInt = testTime.intValue(); if( min == 0 || testTimeInt < min ) { min = testTimeInt; } if( testTimeInt > max ) { max = testTimeInt; } sum += testTimeInt; } int average = sum / testTimes.size(); agentOutput.println( "" ); agentOutput.println( "" ); } agentOutput.println( "
Test nameMaxMinAverage
" + testName + "" + max + "" + min + "" + average + "


" ); // Print results per test agentOutput.println( "

All results

" ); for ( String testName : testNames ) { List testTimes = this.testResults.get( testName ); agentOutput.println( "
" + testName.substring( 2 ) + "
" + testTimes.toString() + "
" ); } agentOutput.println( "
" ); } catch ( Exception e ) { e.printStackTrace(); } } private void testDocsFromNotesIDs() throws NotesException { View lupView = this.db.getView( TEST_VIEW_NAME ); // Test fetching note ids using NotesViewNavigator long startTime = System.currentTimeMillis(); List noteIdsFromViewNavigator = this.getNoteIdsByNoteCollectionV9( lupView ); // Test fetching documents by note id without getting field value for ( String noteId : noteIdsFromViewNavigator ) { Document personDoc = this.db.getDocumentByID( noteId ); personDoc.recycle(); } lupView.recycle(); this.testResults.get( TEST4_DOCUMENT_BY_NOTE_ID ).add( System.currentTimeMillis() - startTime ); } private void testDocsFromNotesIDsFetchValue() throws NotesException { View lupView = this.db.getView( TEST_VIEW_NAME ); // Test fetching documents by note id and getting a field value long startTime = System.currentTimeMillis(); List noteIdsFromViewNavigator = this.getNoteIdsByNoteCollectionV9( lupView ); for ( String noteId : noteIdsFromViewNavigator ) { Document personDoc = this.db.getDocumentByID( noteId ); personDoc.getItemValue( TEST_FIELD_NAME ); personDoc.recycle(); } lupView.recycle(); this.testResults.get( TEST5_DOCUMENT_FIELD_BY_NOTE_ID ).add( System.currentTimeMillis() - startTime ); } private void testDocsFromView() throws NotesException { View lupView = this.db.getView( TEST_VIEW_NAME ); long startTime = System.currentTimeMillis(); // Test fetching documents from view without getting field value Document personDoc = lupView.getFirstDocument(); while ( personDoc != null ) { Document nextDoc = lupView.getNextDocument( personDoc ); personDoc.recycle(); personDoc = nextDoc; } lupView.recycle(); this.testResults.get( TEST6_DOCUMENT_BY_VIEW ).add( System.currentTimeMillis() - startTime ); } private void testDocsFromViewFetchValue() throws NotesException { // Test fetching documents from view and getting a field value View lupView = this.db.getView( TEST_VIEW_NAME ); long startTime = System.currentTimeMillis(); Document personDoc = lupView.getFirstDocument(); while ( personDoc != null ) { Document nextDoc = lupView.getNextDocument( personDoc ); personDoc.getItemValue( TEST_FIELD_NAME ); personDoc.recycle(); personDoc = nextDoc; } lupView.recycle(); this.testResults.get( TEST7_DOCUMENT_FIELD_BY_VIEW ).add( System.currentTimeMillis() - startTime ); } private void testNoteIdsByNavigator() throws NotesException { View lupView = this.db.getView( TEST_VIEW_NAME ); // Test fetching note ids using NotesViewNavigator long startTime = System.currentTimeMillis(); List noteIdsFromViewNavigator = this.getNoteIdsByViewNavigator( lupView ); this.testResults.get( TEST1_NOTE_IDS_BY_NAVIGATOR ).add( System.currentTimeMillis() - startTime ); lupView.recycle(); System.out.println( TEST1_NOTE_IDS_BY_NAVIGATOR + " - Note ids found: " + noteIdsFromViewNavigator.size() ); } private void testNoteIdsByNoteCollection() throws NotesException { View lupView = this.db.getView( TEST_VIEW_NAME ); long startTime = System.currentTimeMillis(); List noteIdsFromView = this.getNoteIdsByNoteCollection( lupView ); lupView.recycle(); this.testResults.get( TEST2_NOTE_IDS_BY_NOTE_COLLECTION ).add( System.currentTimeMillis() - startTime ); System.out.println( TEST2_NOTE_IDS_BY_NOTE_COLLECTION + " - Note ids found: " + noteIdsFromView.size() ); } private void testNoteIdsByNoteCollectionV9() throws NotesException { View lupView = this.db.getView( TEST_VIEW_NAME ); long startTime = System.currentTimeMillis(); List noteIdsFromViewV9 = this.getNoteIdsByNoteCollectionV9( lupView ); lupView.recycle(); this.testResults.get( TEST3_NOTE_IDS_BY_NOTE_COLLECTIONV9 ).add( System.currentTimeMillis() - startTime ); System.out.println( TEST3_NOTE_IDS_BY_NOTE_COLLECTIONV9 + " - Note ids found: " + noteIdsFromViewV9.size() ); } }