Java example: Integrating user-defined transactions into Collection Services

 

This Java™ example program shows how to use the Start Transaction and End Transaction APIs to integrate user-defined transaction performance data into Collection Services.

By using the code examples, you agree to the terms of the Code license and disclaimer information.

import com.ibm.iseries.collectionservices.PerformanceDataReporter; 

// parameters: //   number of TXs per thread //   number of threads //   log|nolog //   enable|disable //   transaction seconds 
public class TestTXApi {
  static TestTXApiThread[]    thread; 
  static private String[] TxTypeString;   static private byte[][] TxTypeArray; 
  static private String   TxEventString;   static private byte[]   TxEventArray; 
  static   {
    int i; 
    // initialize transaction type strings and byte arrays 
      TxTypeString = new String[20];       TxTypeString[ 0] = "Transaction type  00";       TxTypeString[ 1] = "Transaction type  01";       TxTypeString[ 2] = "Transaction type  02";       TxTypeString[ 3] = "Transaction type  03";       TxTypeString[ 4] = "Transaction type  04";       TxTypeString[ 5] = "Transaction type  05";       TxTypeString[ 6] = "Transaction type  06";       TxTypeString[ 7] = "Transaction type  07";       TxTypeString[ 8] = "Transaction type  08";       TxTypeString[ 9] = "Transaction type  09";       TxTypeString[10] = "Transaction type  10";       TxTypeString[11] = "Transaction type  11";       TxTypeString[12] = "Transaction type  12";       TxTypeString[13] = "Transaction type  13";       TxTypeString[14] = "Transaction type  14";       TxTypeString[15] = "Transaction type  15";       TxTypeString[16] = "Transaction type  16";       TxTypeString[17] = "Transaction type  17";       TxTypeString[18] = "Transaction type  18";       TxTypeString[19] = "Transaction type  19"; 
      TxTypeArray = new byte[20][];       for ( i = 0; i < 20; i++ )
        try         {
          TxTypeArray[i] = TxTypeString[i].getBytes("Cp037");         } catch(Exception e)
        {
          System.out.println("Exception \"" + e + "\" when converting");         }

  }/* static */



  public static void main( String[] args )
  {
    int     numberOfTXPerThread;     int     numberOfThreads;     boolean log;     boolean enable;     int     secsToDelay; 
      // process parameters       if ( args.length >= 5 )
 try         {
          numberOfTXPerThread = Integer.parseInt( args[0] );           numberOfThreads     = Integer.parseInt( args[1] ); 
          if ( args[2].equalsIgnoreCase( "log" ) )
     log = true;           else           if ( args[2].equalsIgnoreCase( "nolog" ) )
            log = false;           else           {
            System.out.println( "Wrong value for 3rd parameter!" );             System.out.println( "\tshould be log|nolog" );             return;           }

          if ( args[3].equalsIgnoreCase( "enable" ) )
     enable = true;           else           if ( args[3].equalsIgnoreCase( "disable" ) )
            enable = false;           else           {
            System.out.println( "Wrong value for 4th parameter!" );             System.out.println( "\tshould be enable|disable" );             return;           }

          secsToDelay = Integer.parseInt( args[4] ); 
        } catch (Exception e)
        {
          System.out.println( "Oops! Cannot process parameters!" );           return;         }
      else       {
        System.out.println( "Incorrect Usage." );         System.out.println( "The correct usage is:" );         System.out.println( "java TestTXApi numberOfTXPerThread numberOfThreads     log|nolog enable|disable secsToDelay");         System.out.println("\tlog will make the program cut 1 log transaction per start / end pair");         System.out.println("\tdisable will disable performance collection to minimize overhead");         System.out.print("\nExample: \"java TestTXApi 10000 100 log enable 3\" will call " );         System.out.println("cause 10000 transactions for each of 100 threads");         System.out.println("with 3 seconds between start and end of transaction");         System.out.println("Plus it will place additional log call and will enable reporting." );         return;       }

      System.out.println( "Parameters are processed:" );       System.out.println( "\tnumberOfTxPerThread = " + numberOfTXPerThread );       System.out.println( "\tnumberOfThreads = " + numberOfThreads );       System.out.println( "\tlog = " + log );       System.out.println( "\tenable = " + enable );       System.out.println( "\tsecsToDelay = " + secsToDelay ); 
    // cause initialization of a PerformanceDataReporter class       {
        PerformanceDataReporter pReporter = new PerformanceDataReporter();  pReporter.enableReporting();       }

    TestTXApi t = new TestTXApi( ); 
      System.out.println( "\nAbout to start ..." );       t.prepareTests( numberOfTXPerThread, numberOfThreads, log, enable, secsToDelay );         
    long startTime = System.currentTimeMillis();     
      t.runTests( numberOfThreads ); 
      // wait for threads to complete       for ( int i = 0; i < numberOfThreads; i++ )
        try         {
          thread[i].join( );         } catch(Exception e)
        {
          System.out.println( "***Exception \"" + e + "\" while joining thread " + i );         }

    long endTime = System.currentTimeMillis(); 
      System.out.println( "\nTest runtime for " + ( numberOfTXPerThread * numberOfThreads) +
                          " TXs was " + ( endTime - startTime ) + " msec" ); 
  }/* main() */

  private void prepareTests( int numberOfTxPerThread,
                             int numberOfThreads, boolean log,
boolean enable, int secsToDelay )
  {
    System.out.println( "Creating " + numberOfThreads + " threads");     thread = new TestTXApiThread[numberOfThreads];     for ( int i = 0; i < numberOfThreads; i++ )
      thread[i] = new TestTXApiThread( i, numberOfTxPerThread,
                                       log, enable, secsToDelay ); 
  }/* prepareTests() */

  private void runTests( int numberOfThreads )
  {
    for ( int i = 0; i < numberOfThreads; i++ )
      thread[i].start( ); 
  }/* runTests() */

  private class TestTXApiThread extends Thread   {
    private int     ordinal;     private int     numberOfTxPerThread;     private boolean log;     private boolean enable;     private int     secsToDelay; 
    private PerformanceDataReporter     pReporter; 
    private long    timeStamp[];     private long    userCounters[]; 

      public TestTXApiThread( int ordinal, int numberOfTxPerThread,
                                boolean log, boolean enable, int secsToDelay )
      {
        super();         this.ordinal             = ordinal;         this.numberOfTxPerThread = numberOfTxPerThread;         this.log                 = log;         this.enable              = enable;         this.secsToDelay         = secsToDelay; 
          pReporter = new PerformanceDataReporter( false );           if ( enable )
            pReporter.enableReporting();           timeStamp = new long[1];           userCounters = new long[16];           for ( int i = 0; i < 16; i++ )
              userCounters[i] = i; 
      }/* constructor */

      public void run()
      {
        int i; 
              for ( i = 0; i < numberOfTxPerThread; i++ )
              {
                pReporter.startTransaction( TxTypeArray[i%20], i, TxTypeArray[i%20], 20, timeStamp ); //                pReporter.startTransaction( TxTypeArray[i%20], i, TxTypeString[i%20], timeStamp );                 if ( log )
                  pReporter.logTransaction( TxTypeArray[i%20], i, TxTypeArray[i%20], 20 ); //                  pReporter.logTransaction( TxTypeArray[i%20], i, TxTypeString[i%20] );                 if (secsToDelay > 0)
                  try                   {
                    Thread.sleep(secsToDelay * 1000);                   } catch(Exception e) { }
                pReporter.endTransaction( TxTypeArray[i%20], i, TxTypeArray[i%20], 20, timeStamp,
                                          userCounters ); //                pReporter.endTransaction( TxTypeArray[i%20], i, TxTypeString[i%20], timeStamp,
//                                          userCounters );               }

      }/* run() */

 }/* class TestTXApiThread */

}/* class TestTXApi */

 

Parent topic:

User-defined transactions