View Javadoc

1   package com.flexiblewebsolutions.xdriveunit.loadtest;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.Iterator;
6   
7   import org.apache.log4j.Logger;
8   
9   import com.flexiblewebsolutions.io.util.FileUtils;
10  
11  /***
12   * Initiates logic when all threads have completed.
13   * 
14   * @author Donavon Buss
15   */
16  public abstract class ThreadCallback
17  {
18      static Logger logger = Logger.getLogger( ThreadCallback.class.getName() );
19  
20      private File _TmpDir = null;
21  
22      private String _TestMode = null;
23  
24      /***
25       * Sets the mode which will be part of the filename.
26       * 
27       * @param pTestMode
28       *            Name of test set
29       */
30      public void setTestMode( String pTestMode )
31      {
32          _TestMode = pTestMode;
33      }
34  
35      /***
36       * Returns the mode which will be part of the filename.
37       * 
38       * @return The name of the test set
39       */
40      public String getTestMode()
41      {
42          return ( _TestMode );
43      }
44  
45      /***
46       * Returns the directory where temporary files are written
47       * 
48       * @return Temporary directory location
49       */
50      public File getTmpDir()
51      {
52          return ( _TmpDir );
53      }
54  
55      /***
56       * Sets the directory where temporary files are written
57       * 
58       * @param pTmpDir -
59       *            Directory that can be written by application
60       */
61      public void setTmpDir( File pTmpDir )
62      {
63          _TmpDir = pTmpDir;
64      }
65  
66      /***
67       * Called by ThreadTracker to indicate all expected threads have completed
68       * 
69       * @param pThreadNames -
70       *            Unique names for threads that have completed
71       */
72      public abstract void threadsFinished( ArrayList pThreadNames );
73  
74      /***
75       * Summarizes click time statistics for summarizing data
76       * 
77       * @param pThreadNames -
78       *            Threads that may have output a stats file
79       */
80      protected void writeStatsFile( ArrayList pThreadNames )
81      {
82          logger.debug( "writeStatsFile" );
83          String statsPrefix = "STATS_" + getTestMode() + "_";
84          StringBuffer resultsXML = new StringBuffer();
85          resultsXML.append( "<xmlDirectory>\n" );
86          File[] dirListing = getTmpDir().listFiles(
87                  new PrefixFilter( statsPrefix ) );
88          for( Iterator it = pThreadNames.iterator(); it.hasNext(); )
89          {
90              String threadName = (String) it.next();
91  
92              resultsXML.append( "<result>\n" );
93              resultsXML.append( "<threadName>" );
94              resultsXML.append( threadName );
95              resultsXML.append( "</threadName>\n" );
96              resultsXML.append( "<outputFiles>\n" );
97  
98              String filePrefix = statsPrefix + threadName;
99              // Search for files
100             // USE Filter to show only files that have the stats prefix
101             for( int i = 0; i < dirListing.length; i++ )
102             {
103                 if( dirListing[i].getName().toUpperCase().startsWith(
104                         filePrefix.toUpperCase() ) )
105                 {
106                     resultsXML.append( "<outputFile>" );
107                     resultsXML.append( dirListing[i].getAbsolutePath() );
108                     resultsXML.append( "</outputFile>\n" );
109                 }
110             }
111             resultsXML.append( "</outputFiles>\n" );
112             resultsXML.append( "</result>\n" );
113             threadName = null;
114             filePrefix = null;
115         }
116         resultsXML.append( "</xmlDirectory>" );
117 
118         File outFile = new File( getTmpDir(), getTestMode()
119                 + "_resultsSummary.xml" );
120         FileUtils fu = new FileUtils();
121         fu.writeFile( outFile, resultsXML );
122         fu = null;
123         outFile = null;
124         resultsXML = null;
125         dirListing = null;
126     }
127 }