FileDocCategorySizeDatePackage
AppenderTable.javaAPI DocApache log4j 1.2.157445Sat Aug 25 00:09:36 BST 2007None

AppenderTable

public class AppenderTable extends JTable
The AppenderTable illustrates one possible implementation of an Table possibly containing a great many number of rows.

In this particular example we use a fixed size buffer (CyclicBuffer) although this data structure could be easily replaced by dynamically growing one, such as a Vector. The required properties of the data structure is 1) support for indexed element access 2) support for the insertion of new elements at the end.

Experimentation on my 1400Mhz AMD machine show that it takes about 45 micro-seconds to insert an element in the table. This number does not depend on the size of the buffer. It takes as much (or as little) time to insert one million elements to a buffer of size 10 as to a buffer of size 10'000. It takes about 4 seconds to insert a total of 100'000 elements into the table.

On windows NT the test will run about twice as fast if you give the focus to the window that runs "java AppenderTable" and not the window that contains the Swing JFrame.

Fields Summary
static Logger
logger
Constructors Summary
public AppenderTable()

    this.setDefaultRenderer(Object.class, new Renderer());
  
Methods Summary
public voiddoAppend(org.apache.log4j.spi.LoggingEvent event)
When asked to append we just insert directly into the model. In a real appender we would use two buffers one for accumulating events and another to accumalte events but after filtering. Only the second buffer would be displayed in the table and made visible to the user.

    ((AppenderTableModel)getModel()).insert(event);
  
public static voidmain(java.lang.String[] args)


       

    if(args.length != 2) {
      System.err.println(
      "Usage: java AppenderTable bufferSize runLength\n"
      +"  where bufferSize is the size of the cyclic buffer in the TableModel\n"
      +"  and runLength is the total number of elements to add to the table in\n"
      +"  this test run.");
      return;
    }

    JFrame frame = new JFrame("JTableAppennder test");
    Container container = frame.getContentPane();

    AppenderTable tableAppender = new AppenderTable();
    
    int bufferSize = Integer.parseInt(args[0]);
    AppenderTableModel model = new AppenderTableModel(bufferSize);
    tableAppender.setModel(model);

    int runLength = Integer.parseInt(args[1]);

    JScrollPane sp = new JScrollPane(tableAppender);
    sp.setPreferredSize(new Dimension(250, 80));
    
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
    container.add(sp);

    // The "ADD" button is intended for manual testing. It will
    // add one new logging event to the table.
    JButton button = new JButton("ADD");
    container.add(button);
    button.addActionListener(new JTableAddAction(tableAppender));

    frame.setSize(new Dimension(500,300));
    frame.setVisible(true);

    long before = System.currentTimeMillis();

    int i = 0;
    while(i++ < runLength) {      
      LoggingEvent event = new LoggingEvent("x", logger, Level.ERROR, 
					    "Message "+i, null);
      tableAppender.doAppend(event);
    }

    long after = System.currentTimeMillis();

    long totalTime = (after-before);
    
    System.out.println("Total time :"+totalTime+ " milliseconds for "+
		       "runLength insertions.");
    System.out.println("Average time per insertion :"
		       +(totalTime*1000/runLength)+ " micro-seconds.");