TestToFixPerformance.java :  » Messenger » to-fix » org » tomac » tools » Java Open Source

Java Open Source » Messenger » to fix 
to fix » org » tomac » tools » TestToFixPerformance.java
/**
 * Copyright (c) 2011 Sebastian Tomac (tomac.org)
 * Licensed under LGPL licenses.
 * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
 **/
package org.tomac.tools;

import static org.junit.Assert.assertFalse;

import java.nio.ByteBuffer;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import org.tomac.protocol.fix.FixMessage;
import org.tomac.protocol.fix.FixUtils;
import org.tomac.protocol.fix.FixValidationError;
import org.tomac.protocol.fix.messaging.FixMarketDataSnapshotFullRefresh;
import org.tomac.protocol.fix.messaging.FixMessageListenerImpl;
import org.tomac.protocol.fix.messaging.FixMessageParser;
import org.tomac.protocol.fix.messaging.FixMessagePool;
import org.tomac.protocol.fix.messaging.FixNewOrderSingle;


/**
 * @author seto
 *
 */
public class TestToFixPerformance {
  final int ITERATIONS = 100000;
  final int DO_SAMPLE_DATA = 100;
  // discard data > 7000 due to grabage collection distortions and jvm warmup
  final long DISCARD_LEVEL = 7000L;
  FixValidationError err;
  FixMessageParser parser;
  FixMessageListenerImpl listener;
  FixMarketDataSnapshotFullRefresh message;
  
  @Before
  public void setUp() {
    FixUtils.validateSession = false;
    FixUtils.validateChecksum = false;
    FixUtils.validateSendingTime = false;
    FixUtils.validateOnlyDefinedTagsAllowed = false;
    err = new FixValidationError();
    parser = new FixMessageParser();
    listener = new FixMessageListenerImpl() {
        @Override
        public void onFixNewOrderSingle(FixNewOrderSingle msg) {
        }
        @Override
        public void onFixMarketDataSnapshotFullRefresh(FixMarketDataSnapshotFullRefresh msg) {
          message = msg.clone();
        }
      };
  }
  
  @Test
  public void testInBoundLatency() throws Exception {
        final String data = "8=FIXT.1.19=24935=D49=SenderCompId56=TargetCompId34=3752=20070223-22:28:33"
            + "11=18333922=838=140=244=1248=BHP54=255=BHP59=1"
            + "60=20060223-22:38:33526=362078=279=AllocACC180=1010.1"
            + "79=AllocACC280=2020.2453=2448=8447=D452=4448=AAA35354447=D452=310=168";
        ByteBuffer buf = ByteBuffer.wrap(data.getBytes());

        int count = 0;
        long cumTime = 0L;
        long cumTimeIntervall = 0L;
        int sampleCount = 0;
        long sampleTime = 0L;

        System.out.println("toFIX testInBoundLatency");
        while (count < ITERATIONS) {
      err.clear();
          long t0 = System.nanoTime();
          parser.parse(buf, err, listener);
          long t1 = System.nanoTime();
          buf.flip();
          assertFalse(err.hasError());
          cumTime += t1 - t0;
          cumTimeIntervall += t1 - t0;
          ++count;
          if (count % DO_SAMPLE_DATA == 0) {
            if (cumTimeIntervall / DO_SAMPLE_DATA < DISCARD_LEVEL) {
              sampleCount++;
              sampleTime += cumTimeIntervall / DO_SAMPLE_DATA;
            }
            //System.out.println(cumTime / count + " " + cumTimeIntervall / DO_SAMPLE_DATA);
            cumTimeIntervall = 0L;
          }
        }
    System.out.println("ns/msg\t#count\ttotns/msg\t#totCount");
    System.out.println(sampleTime / sampleCount + "\t" + 
        sampleCount * DO_SAMPLE_DATA + "\t" + cumTime / ITERATIONS + "\t" + ITERATIONS);
  }

  
    @Test
    public void testOutBoundLatency() throws Exception {
        final String data = "8=FIXT.1.19=18235=W34=249=ABFX52=20080722-16:37:11.23456=X2RV1"
                + "55=EUR/USD262=CAP0000011268=2269=0270=1.5784415=EUR271=500000272=20080724"
                + "269=1270=1.5786915=EUR271=500000272=2008072410=097";
        ByteBuffer buf = ByteBuffer.wrap(data.getBytes());
        
      parser.parse(buf, err, listener);
      assertFalse(err.hasError());
        ByteBuffer out = ByteBuffer.allocate(1024);

        int count = 0;
        long cumTime = 0L;
        long cumTimeIntervall = 0L;
        int sampleCount = 0;
        long sampleTime = 0L;
        
        System.out.println("toFIX testOutBoundLatency");
        while (count < ITERATIONS) {
      err.clear();
          long t0 = System.nanoTime();
          message.encode(out);
          long t1 = System.nanoTime();
          out.clear();
          cumTime += t1 -t0;
          cumTimeIntervall += t1 -t0;
          message.clear();
          ++count;
          if (count % DO_SAMPLE_DATA == 0) {
            if (cumTimeIntervall / DO_SAMPLE_DATA < DISCARD_LEVEL) {
              sampleCount++;
              sampleTime += cumTimeIntervall / DO_SAMPLE_DATA;
            }
            //System.out.println(cumTime / count + " " + cumTimeIntervall / DO_SAMPLE_DATA);
            cumTimeIntervall = 0L;
          }
        }
    System.out.println("ns/msg\t#count\ttotns/msg\t#totCount");
    System.out.println(sampleTime / sampleCount + "\t" + 
        sampleCount * DO_SAMPLE_DATA + "\t" + cumTime / ITERATIONS + "\t" + ITERATIONS);
    }
  
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.