/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.store.journal;

import java.io.File;

import junit.framework.TestCase;

import org.activeio.Packet;
import org.activeio.journal.active.JournalImpl;
import org.activeio.packet.ByteArrayPacket;

/**
 * Used to micro benchmark the ActiveIO Journal operations.
 * 
 * Make sure you run with jvm option -server (makes a big difference).
 * The tests simulate storing 100000 1k jms messages to see the rate of 
 * processing msg/sec.
 * 
 * @version $Revision: 1.1 $
 */
public class ActiveIOJournalBenchmark extends TestCase {

    private static final int MESSAGE_COUNT = 100000;
    private Packet packet;
    private JournalImpl journal;
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(ActiveIOJournalBenchmark.class);
    }

    protected void setUp() throws Exception {
        File file = new File("target/journal");
        JournalTestHelper.delete(file);
        journal = new JournalImpl(file);
        packet = new ByteArrayPacket(new byte[1072]);
    }
    
    protected void tearDown() throws Exception {
        journal.dispose();
    }
    
    
    /** 
     * Runs at about 24000 msg/sec on OS X G4 1.5ghz.  This shows that writing to the
     * activeio journal is not a bottleneck.
     */
    public void testAsyncAddMessage() throws Exception {
        long start = System.currentTimeMillis();
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            journal.write(packet, false);
        }
        long end = System.currentTimeMillis();
        
        System.out.println(getName()+": test duration: "+(end-start)+" ms, msg/s: "+(MESSAGE_COUNT*1000f/(end-start)));
        
     }
}
