From 4a5b076822b95723fc89df5ade4a5dd9058569bb Mon Sep 17 00:00:00 2001 From: carltimmer Date: Wed, 25 Sep 2024 09:58:56 -0400 Subject: [PATCH] move files to test dir, add javadoc, delete unused code --- .../jlab/coda/hipo/test/ReadWriteTest.java | 361 ++++++++++++++++++ .../coda/hipo/test/RecordSupplyTester.java | 130 +++++++ .../org/jlab/coda/hipo/test/TestBuilding.java | 147 +++++++ java/org/jlab/coda/hipo/test/TestWriter.java | 359 +++++++++++++++++ 4 files changed, 997 insertions(+) create mode 100644 java/org/jlab/coda/hipo/test/ReadWriteTest.java create mode 100644 java/org/jlab/coda/hipo/test/RecordSupplyTester.java create mode 100644 java/org/jlab/coda/hipo/test/TestBuilding.java create mode 100644 java/org/jlab/coda/hipo/test/TestWriter.java diff --git a/java/org/jlab/coda/hipo/test/ReadWriteTest.java b/java/org/jlab/coda/hipo/test/ReadWriteTest.java new file mode 100644 index 00000000..999dcd1d --- /dev/null +++ b/java/org/jlab/coda/hipo/test/ReadWriteTest.java @@ -0,0 +1,361 @@ +/* + * Copyright (c) 2017. Jefferson Lab (JLab). All rights reserved. Permission + * to use, copy, modify, and distribute this software and its documentation for + * educational, research, and not-for-profit purposes, without fee and without a + * signed licensing agreement. + */ +package org.jlab.coda.hipo.test; + + +import org.jlab.coda.hipo.*; +import org.jlab.coda.jevio.*; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * Class used to test behavior of the RecordSupply and RecordRingItem classes. + * @version 6.0 + * @since 6.0 9/21/17 + * @author timmer + */ +public class ReadWriteTest { + + /** + * Write ints. + * + * @param size number of INTS + * @param order byte order of generated ints + * @return byte array + */ + static byte[] generateSequentialInts(int size, ByteOrder order) { + try { + int[] buffer = new int[size]; + for (int i = 0; i < size; i++) { + buffer[i] = i; + //buffer[i] = 1; + } + return ByteDataTransformer.toBytes(buffer, order); + } + catch (EvioException e) { + e.printStackTrace(); + } + return null; + } + + /** + * Write shorts. + * + * @param size number of SHORTS + * @param order byte order of shorts in memory + * @return byte array + */ + static byte[] generateSequentialShorts(int size, ByteOrder order) { + try { + short[] buffer = new short[size]; + for (short i = 0; i < size; i++) { + buffer[i] = i; + //buffer[i] = (short)1; + } + return ByteDataTransformer.toBytes(buffer, order); + } + catch (EvioException e) { + e.printStackTrace(); + } + return null; + } + + + /** + * Generate a buffer with CODA prestart event format data. + * @param order byte order of generated data. + * @return ByteBuffer with prestart event. + */ + static ByteBuffer generateEvioBuffer(ByteOrder order) { + // Create an evio bank of ints + ByteBuffer evioDataBuf = ByteBuffer.allocate(20).order(order); // 5 ints + evioDataBuf.putInt(0, 4); // length in words + evioDataBuf.putInt(4, 0xffd10100); // 2nd evio header word (prestart event) + evioDataBuf.putInt(8, 0x1234); // time + evioDataBuf.putInt(12, 0x5); // run # + evioDataBuf.putInt(16, 0x6); // run type + return evioDataBuf; + } + + + /** + * Write a file using Writer with a dictionary, first event and trailer index, + * in uncompressed form. Add junk data as an "event", write prestart event in + * EvioNode form, and write out an entire Record. + * @param finalFilename name of file. + */ + static void writeFile(String finalFilename) { + + try { + + // Variables to track record build rate + double freqAvg; + long totalC = 0; + long loops = 3; + + // Create files + String dictionary = "This is a dictionary"; + //dictionary = ""; + boolean addTrailerIndex = true; + //CompressionType compType = CompressionType.RECORD_COMPRESSION_GZIP; + CompressionType compType = CompressionType.RECORD_UNCOMPRESSED; + + byte firstEvent[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int firstEventLen = 10; + ByteOrder order = ByteOrder.LITTLE_ENDIAN; + + Writer writer = new Writer(HeaderType.EVIO_FILE, order, 0, 0, + dictionary, firstEvent, compType, + addTrailerIndex); + + byte[] userHdr = new byte[10]; + for (byte i = 0; i < 10; i++) { + userHdr[i] = i; + } + + writer.open(finalFilename, userHdr); + //writer.open(finalFilename); + System.out.println("Past creating writer"); + + //byte[] buffer = generateSequentialInts(100, order); + byte[] dataArray = generateSequentialShorts(13, order); + ByteBuffer dataBuffer = ByteBuffer.wrap(dataArray).order(order); + + // Create an evio bank of ints + ByteBuffer evioDataBuf = generateEvioBuffer(order); + // Create node from this buffer + EvioNode node = EvioNode.extractEventNode(evioDataBuf, null, 0, 0, 0); + + long t1 = System.currentTimeMillis(); + + while (true) { + // random data array + //writer.addEvent(dataArray, 0, 26); + writer.addEvent(dataBuffer); + + //cout << int(20000000 - loops) << endl; + totalC++; + + if (--loops < 1) break; + } + + writer.addEvent(node); + + long t2 = System.currentTimeMillis(); + long deltaT = (t2 - t1); + + freqAvg = (double) totalC / deltaT * 1000; + + System.out.println("Time = " + deltaT + " msec, Hz = " + freqAvg); + System.out.println("Finished all loops, count = " + totalC); + + //------------------------------ + // Add entire record at once + //------------------------------ + + RecordOutputStream recOut = new RecordOutputStream(); + recOut.addEvent(dataArray, 0, 26); + writer.writeRecord(recOut); + + + // writer1.addTrailer(true); + // writer1.addTrailerWithIndex(true); + // cout << "Past write 1" << endl; + + // writer.addTrailer(true); + // writer.addTrailerWithIndex(true); + + // writer3.addTrailer(true); + // writer3.addTrailerWithIndex(true); + + // cout << "Past write 3" << endl; + + // writer1.close(); + // cout << "Past close 1" << endl; + writer.close(); + System.out.println("Past close 2"); + // writer3.close(); + + // Doing a diff between files shows they're identical! + + System.out.println("Finished writing files " + finalFilename + ", now read it in"); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + + /** + * Just like {@link #writeFile(String)} except the WriterMT is used to write. + * @param finalFilename name of file. + */ + static void writeFileMT(String finalFilename) { + + try { + // Variables to track record build rate + double freqAvg; + long totalC = 0; + long loops = 3; + + // Create files + String dictionary = "This is a dictionary"; + //dictionary = ""; + boolean addTrailerIndex = true; + //CompressionType compType = CompressionType.RECORD_COMPRESSION_GZIP; + CompressionType compType = CompressionType.RECORD_UNCOMPRESSED; + + byte firstEvent[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int firstEventLen = 10; + ByteOrder order = ByteOrder.LITTLE_ENDIAN; + WriterMT writer = new WriterMT(HeaderType.EVIO_FILE, order, 0, 0, + dictionary, firstEvent, firstEventLen, + compType, 2, addTrailerIndex, 16); + + byte[] userHdr = new byte[10]; + for (byte i = 0; i < 10; i++) { + userHdr[i] = i; + } + + writer.open(finalFilename, userHdr); + //writer.open(finalFilename); + System.out.println("Past creating writer"); + + //byte[] buffer = generateSequentialInts(100, order); + byte[] dataArray = generateSequentialShorts(13, order); + ByteBuffer dataBuffer = ByteBuffer.wrap(dataArray).order(order); + + // Create an evio bank of ints + ByteBuffer evioDataBuf = generateEvioBuffer(order); + // Create node from this buffer + EvioNode node = EvioNode.extractEventNode(evioDataBuf, null, 0, 0, 0); + + long t1 = System.currentTimeMillis(); + + while (true) { + // random data array + //writer.addEvent(dataArray, 0, 26); + writer.addEvent(dataBuffer); + + //cout << int(20000000 - loops) << endl; + totalC++; + + if (--loops < 1) break; + } + + writer.addEvent(node); + + long t2 = System.currentTimeMillis(); + long deltaT = (t2 - t1); + + freqAvg = (double) totalC / deltaT * 1000; + + System.out.println("Time = " + deltaT + " msec, Hz = " + freqAvg); + System.out.println("Finished all loops, count = " + totalC); + + //------------------------------ + // Add entire record at once + //------------------------------ + + RecordOutputStream recOut = new RecordOutputStream(); + recOut.addEvent(dataArray, 0, 26); + writer.writeRecord(recOut); + + writer.close(); + System.out.println("Past close "); + + // Doing a diff between files shows they're identical! + + System.out.println("Finished writing files " + finalFilename + ", now read it in"); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * Read file with Reader, get event count, dictionary, first event. + * Then get first data event and print data. + * @param finalFilename file to read. + */ + static void readFile(String finalFilename) { + + try { + Reader reader = new Reader(finalFilename); + ByteOrder order = reader.getByteOrder(); + + int evCount = reader.getEventCount(); + System.out.println("Read in file " + finalFilename + ", got " + evCount + " events"); + + String dict = reader.getDictionary(); + System.out.println(" Got dictionary = " + dict); + + byte[] pFE = reader.getFirstEvent(); + if (pFE != null) { + int feBytes = pFE.length; + System.out.println(" First Event bytes = " + feBytes); + System.out.print(" First Event values = \n "); + for (int i = 0; i < feBytes; i++) { + System.out.print(pFE[i] + ", "); + } + System.out.println(); + } + + byte[] data = reader.getEvent(0); + + if (data != null) { +// int[] intData = ByteDataTransformer.toIntArray(data, order); +// System.out.print(" Event #0, values =\n "); +// int pos = 0; +// for (int i: intData) { +// System.out.print(i + ", "); +// if ((++pos)%5 == 0) System.out.println(); +// } + + + short[] sData = ByteDataTransformer.toShortArray(data, order); + System.out.print(" Event #0, values =\n "); + int pos = 0; + for (short i: sData) { + System.out.print(i + ", "); + if ((++pos)%5 == 0) System.out.println(); + } + + System.out.println(); + } + + } + catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * Main + * @param args args + */ + public static void main(String[] args){ + +// String filename = "/dev/shm/hipoTest.evio"; +// String filenameMT = "/dev/shm/hipoTest.evio"; + String filename = "/dev/shm/hipoTest-j.evio"; + String filenameMT = "/dev/shm/hipoTestMT-j.evio"; + + // Write files + writeFile(filename); + writeFileMT(filenameMT); + + // Now read the written files + readFile(filename); + System.out.println("\n\n----------------------------------------\n\n"); + readFile(filenameMT); + } + +} diff --git a/java/org/jlab/coda/hipo/test/RecordSupplyTester.java b/java/org/jlab/coda/hipo/test/RecordSupplyTester.java new file mode 100644 index 00000000..324f6c1b --- /dev/null +++ b/java/org/jlab/coda/hipo/test/RecordSupplyTester.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2017. Jefferson Lab (JLab). All rights reserved. Permission + * to use, copy, modify, and distribute this software and its documentation for + * educational, research, and not-for-profit purposes, without fee and without a + * signed licensing agreement. + */ +package org.jlab.coda.hipo.test; + + +import org.jlab.coda.hipo.*; +import java.nio.ByteOrder; + +/** + * Class used to test behavior of the RecordSupply and RecordRingItem classes. + * @version 6.0 + * @since 6.0 9/21/17 + * @author timmer + */ +public class RecordSupplyTester { + + /** Supply of RecordSupplyItem objects each of which encapsulate a Record. */ + RecordSupply supply; + /** Number of compression threads. */ + int compressThreadCount = 1; + /** Endianness of each created record. */ + ByteOrder order = ByteOrder.LITTLE_ENDIAN; + + /** Class for getting new RecordRingItem from the RecordSupply and publishing them + * so they're accessible to the compressor. */ + class Producer extends Thread { + @Override + public void run() { + while (true) { + RecordRingItem item = supply.get(); + //System.out.println("Producer: got item"); + //try {Thread.sleep(1500);} catch (InterruptedException e) {} + supply.publish(item); + //System.out.println("Producer: published item"); + } + + } + } + + /** Class for getting an item from the supply in order to compress data. */ + class Compressor extends Thread { + /** ID number of the thread used to compress. */ + int num; + + /** + * Constructor. + * @param threadNumber ID number of the thread used to compress. + */ + Compressor(int threadNumber) { + num = threadNumber; + } + + @Override + public void run() { + try { + while (true) { + //System.out.println(" Compressor " + num + ": waiting 4 item"); + RecordRingItem item = supply.getToCompress(num); + Thread.sleep(3000); + supply.releaseCompressor(item); + //System.out.println(" Compressor " + num + ": released item"); + } + } + catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** Class to get an item with compressed data from + * the supply in order to write it somewhere. */ + class Writerr extends Thread { + @Override + public void run() { + try { + while (true) { + //System.out.println(" Writer: waiting 4 item"); + RecordRingItem item = supply.getToWrite(); + //Thread.sleep(1500); + supply.releaseWriter(item); + System.out.print("."); + //System.out.println(" Writer: released item"); + } + } + catch (Exception e) { + e.printStackTrace(); + } + } + } + + + /** + * Constructor. + * @param threadCount number of compression threads. + */ + RecordSupplyTester(int threadCount) { + + supply = new RecordSupply(8, order, threadCount, + 0, 0, CompressionType.RECORD_COMPRESSION_LZ4); + compressThreadCount = threadCount; + + Writerr writer = new Writerr(); + writer.start(); + + for (int i=0; i < threadCount; i++) { + System.out.println("Starting compressor #" + (i+1)); + Compressor comp = new Compressor(i); + comp.start(); + } + + Producer producer = new Producer(); + producer.start(); + } + + + /** + * Example use of this class. + * @param args unused. + */ + public static void main(String[] args){ + + RecordSupplyTester tester = new RecordSupplyTester(8); + + } + +} diff --git a/java/org/jlab/coda/hipo/test/TestBuilding.java b/java/org/jlab/coda/hipo/test/TestBuilding.java new file mode 100644 index 00000000..8583b595 --- /dev/null +++ b/java/org/jlab/coda/hipo/test/TestBuilding.java @@ -0,0 +1,147 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.jlab.coda.hipo.test; + +import org.jlab.coda.hipo.*; +import java.io.RandomAccessFile; + +/** + * Class used to test the speed of the RecordOutputStream.build() + * method. Help to speed up LZ4 data compression. + * @author timmer + */ +public class TestBuilding { + + + /** Amount of real data bytes in hallDdata array. */ + static private int arrayBytes; + + /** Put extracted real data in here. */ + static private byte[] realData; + + + /** + * Method to get real data from an existing file of data previously extracted from + * a Hall D data file. + * + * @return true if hall D data found and available, else false. + */ + static boolean getRealData() { + + int num = 1; + + // First check to see if we already have some data in a file + String filename = System.getenv("CODA"); + if (filename != null) { + filename += "/common/bin/hallDdata" + num + ".bin"; + } + else { + filename = "/Users/timmer/coda/coda3/common/bin/hallDdata" + num + ".bin"; + } + + try { + RandomAccessFile file = new RandomAccessFile(filename, "rw"); + arrayBytes = (int) file.length(); + realData = new byte[arrayBytes]; + file.read(realData, 0, arrayBytes); + file.close(); + } + catch (Exception e) { + // No file to be read, so try creating our own + System.out.println("getRealData: cannot open data file " + filename); + return false; + } + +System.out.println("getRealData: successfully read in file " + filename); + return true; + } + + + + + + /** + * Example method using RecordOutputStream class. + */ + public static void testStreamRecord() { + + // Variables to track record build rate + double freqAvg; + long t1, t2, deltaT, totalC=0; + // Ignore the first N values found for freq in order + // to get better avg statistics. Since the JIT compiler in java + // takes some time to analyze & compile code, freq may initially be low. + long ignore = 100; + long loops = 1100; + + // Create file + RecordOutputStream ros = new RecordOutputStream(); + ros.getHeader().setCompressionType(CompressionType.RECORD_COMPRESSION_LZ4); + + + // position in realData to get data from + int offset = 0; + int eventLength = 10000; + + getRealData(); + + // Fill object with events + while (true) { + // Go through real data array + if (offset + eventLength > arrayBytes) { + offset = 0; + } + + boolean added = ros.addEvent(realData, offset, eventLength); + if (!added) { + // record output stream is full, time to build + break; + } + + offset += eventLength; + } + + + + t1 = System.currentTimeMillis(); + + do { + ros.build(); + + // Ignore beginning loops to remove JIT compile time + if (ignore-- > 0) { + t1 = System.currentTimeMillis(); + } + else { + totalC++; + } + } while (--loops > 0); + + + + + t2 = System.currentTimeMillis(); + deltaT = t2 - t1; // millisec + freqAvg = (double) totalC / deltaT * 1000; + + System.out.println("Time = " + deltaT + " msec, Hz = " + freqAvg); + + System.out.println("Finished all loops, count = " + totalC); + + } + + + + /** + * Example use of this class. + * @param args unused. + */ + public static void main(String[] args){ + + testStreamRecord(); + + } +} diff --git a/java/org/jlab/coda/hipo/test/TestWriter.java b/java/org/jlab/coda/hipo/test/TestWriter.java new file mode 100644 index 00000000..4dc58929 --- /dev/null +++ b/java/org/jlab/coda/hipo/test/TestWriter.java @@ -0,0 +1,359 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.jlab.coda.hipo.test; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteOrder; +import java.nio.channels.FileChannel; + +import org.jlab.coda.hipo.*; + +/** + * + * @author gavalian + */ +public class TestWriter { + + /** + * Generate a byte array of random data. + * @return byte array of random data. + */ + public static byte[] generateBuffer(){ + int size = (int) (Math.random()*35.0); + size+= 480; + byte[] buffer = new byte[size]; + for(int i = 0; i < buffer.length; i++){ + buffer[i] = (byte) (Math.random()*126.0); + } + return buffer; + } + + /** + * Generate a byte array of random data. + * @param size number of bytes in array. + * @return byte array of random data. + */ + public static byte[] generateBuffer(int size){ + byte[] buffer = new byte[size]; + for(int i = 0; i < buffer.length; i++){ + buffer[i] = (byte) (Math.random()*125.0 + 1.0); + } + return buffer; + } + + /** + * Print out given byte array. + * @param array array to print out. + */ + public static void print(byte[] array){ + StringBuilder str = new StringBuilder(); + int wrap = 20; + for(int i = 0; i < array.length; i++){ + str.append(String.format("%3X ", array[i])); + if((i+1)%wrap==0) str.append("\n"); + } + System.out.println(str.toString()); + } + + + /** + * Test the writing speed of compressed, random data to a file. + * @throws IOException for problems writing file + * @throws HipoException for problems writing file + */ + public static void testStreamRecord() throws IOException, HipoException { + + // Variables to track record build rate + double freqAvg; + long t1, t2, deltaT, totalC=0; + // Ignore the first N values found for freq in order + // to get better avg statistics. Since the JIT compiler in java + // takes some time to analyze & compile code, freq may initially be low. + long ignore = 10000; + long loops = 2000000; + + // Create file + Writer writer = new Writer(); + writer.setCompressionType(CompressionType.RECORD_COMPRESSION_LZ4); + writer.open("./exampleFile.v6.evio"); + + + byte[] buffer = TestWriter.generateBuffer(400); + + t1 = System.currentTimeMillis(); + + while (true) { + // random data array + writer.addEvent(buffer); + +//System.out.println(""+ (20000000 - loops)); + // Ignore beginning loops to remove JIT compile time + if (ignore-- > 0) { + t1 = System.currentTimeMillis(); + } + else { + totalC++; + } + + if (--loops < 1) break; + } + + t2 = System.currentTimeMillis(); + deltaT = t2 - t1; // millisec + freqAvg = (double) totalC / deltaT * 1000; + + System.out.println("Time = " + deltaT + " msec, Hz = " + freqAvg); + + System.out.println("Finished all loops, count = " + totalC); + +// // Create our own record +// RecordOutputStream myRecord = new RecordOutputStream(writer.getByteOrder()); +// buffer = TestWriter.generateBuffer(200); +// myRecord.addEvent(buffer); +// myRecord.addEvent(buffer); +// writer.writeRecord(myRecord); + +// writer.addTrailer(true); +// writer.addTrailerWithIndex(true); + writer.close(); + + System.out.println("Finished writing file"); + + + } + + /** + * Compare the writing of identical data to both Writer and WriterMT + * to see if resulting files are identical. + * + * @throws IOException for problems writing file + * @throws HipoException for problems writing file + */ + public static void testWriterVsWriterMT() throws IOException, HipoException { + + // Variables to track record build rate + double freqAvg; + long t1, t2, deltaT, totalC=0; + // Ignore the first N values found for freq in order + // to get better avg statistics. Since the JIT compiler in java + // takes some time to analyze & compile code, freq may initially be low. + long ignore = 0; + long loops = 200000; + + // Create 1 file with Writer + Writer writer = new Writer(ByteOrder.BIG_ENDIAN, 100000, 8000000); + writer.setCompressionType(CompressionType.RECORD_COMPRESSION_LZ4); + String file1 = "./dataFile.v6.writer"; + writer.open(file1); + + // Create 1 file with WriterMT + + WriterMT writerN = new WriterMT(ByteOrder.BIG_ENDIAN, 100000, 8000000, + CompressionType.RECORD_COMPRESSION_LZ4, 1, 8); + String file2 = "./dataFile.v6.writerMT"; + writerN.open(file2); + + // Buffer with random dta + byte[] buffer = TestWriter.generateBuffer(400); + + t1 = System.currentTimeMillis(); + + while (true) { + // write data with Writer class + writer.addEvent(buffer); + + // write data with WriterNew class + writerN.addEvent(buffer); + + // Ignore beginning loops to remove JIT compile time + if (ignore-- > 0) { + t1 = System.currentTimeMillis(); + } + else { + totalC++; + } + + if (--loops < 1) break; + } + + t2 = System.currentTimeMillis(); + deltaT = t2 - t1; // millisec + freqAvg = (double) totalC / deltaT * 1000; + + System.out.println("Time = " + deltaT + " msec, Hz = " + freqAvg); + + System.out.println("Finished all loops, count = " + totalC); + + writer.close(); + writerN.close(); + + System.out.println("Finished writing 2 files, now compare"); + + FileInputStream fileStream1 = new FileInputStream(file1); + FileChannel fileChannel = fileStream1.getChannel(); + long fSize = fileChannel.size(); + + FileInputStream fileStream2 = new FileInputStream(file2); + + + for (long l=0; l < fSize; l++) { + if (fileStream1.read() != fileStream2.read()) { + System.out.println("Files differ at byte = " + l); + } + } + + } + + + /** + * Write 3 files, all using WriterMT, comparing using 1, 2, or 3 compression threads. + * It's up to the caller to compare the output files to each other to see if they're + * identical. + */ + public static void testStreamRecordMT() { + + try { + // Variables to track record build rate + double freqAvg; + long t1, t2, deltaT, totalC=0; + // Ignore the first N values found for freq in order + // to get better avg statistics. Since the JIT compiler in java + // takes some time to analyze & compile code, freq may initially be low. + long ignore = 0; + long loops = 6; + + String fileName = "./exampleFile.v6.evio"; + + // Create files + WriterMT writer1 = new WriterMT(fileName + ".1", ByteOrder.LITTLE_ENDIAN, 0, 0, + CompressionType.RECORD_COMPRESSION_LZ4, 1, 8); + WriterMT writer2 = new WriterMT(fileName + ".2", ByteOrder.LITTLE_ENDIAN, 0, 0, + CompressionType.RECORD_COMPRESSION_LZ4, 2, 8); + WriterMT writer3 = new WriterMT(fileName + ".3", ByteOrder.LITTLE_ENDIAN, 0, 0, + CompressionType.RECORD_COMPRESSION_LZ4, 3, 8); + + byte[] buffer = TestWriter.generateBuffer(400); + + t1 = System.currentTimeMillis(); + + while (true) { + // random data array + writer1.addEvent(buffer); + writer2.addEvent(buffer); + writer3.addEvent(buffer); + + //System.out.println(""+ (20000000 - loops)); + // Ignore beginning loops to remove JIT compile time + if (ignore-- > 0) { + t1 = System.currentTimeMillis(); + } + else { + totalC++; + } + + if (--loops < 1) break; + } + + t2 = System.currentTimeMillis(); + deltaT = t2 - t1; // millisec + freqAvg = (double) totalC / deltaT * 1000; + + System.out.println("Time = " + deltaT + " msec, Hz = " + freqAvg); + + System.out.println("Finished all loops, count = " + totalC); + + + writer1.addTrailer(true); + writer1.addTrailerWithIndex(true); + + writer2.addTrailer(true); + writer2.addTrailerWithIndex(true); + + writer3.addTrailer(true); + writer3.addTrailerWithIndex(true); + + + writer1.close(); + writer2.close(); + writer3.close(); + + // Doing a diff between files shows they're identical! + + System.out.println("Finished writing files"); + } + catch (HipoException e) { + e.printStackTrace(); + } + + } + + + /** + * Test the RecordOutputStream by writing an array of random data to it, + * then building the stream, then resetting it, in an infinite loop. + */ + public static void streamRecord() { + RecordOutputStream stream = new RecordOutputStream(); + byte[] buffer = TestWriter.generateBuffer(); + while(true){ + //byte[] buffer = TestWriter.generateBuffer(); + boolean flag = stream.addEvent(buffer); + if(flag==false){ + stream.build(); + stream.reset(); + } + } + + } + + /** + * Test writing random, compressed data to file. + * @throws IOException problems with file I/O. + */ + public static void writerTest() throws IOException { + Writer writer = new Writer("compressed_file.evio", + ByteOrder.BIG_ENDIAN, 0, 0); + //byte[] array = TestWriter.generateBuffer(); + for(int i = 0; i < 340000; i++){ + byte[] array = TestWriter.generateBuffer(); + writer.addEvent(array); + } + writer.close(); + } + + /** + * Test writing random, compressed data to file along with a user header. + * @throws IOException for problems writing file + * @throws HipoException for problems writing file + */ + public static void writerTestWithUserHdr() throws IOException, HipoException { + String userHeader = "Example of creating a new header file.......?"; + System.out.println("STRING LENGTH = " + userHeader.length()); + Writer writer = new Writer(); + writer.open("example_file.evio", userHeader.getBytes()); + for(int i = 0; i < 5; i++){ + byte[] array = TestWriter.generateBuffer(); + writer.addEvent(array); + } + + writer.close(); + } + + /** + * Example use of this class. + * @param args unused. + */ + public static void main(String[] args){ + + testStreamRecordMT(); + //testWriterVsWriterMT(); + //testStreamRecord(); + //streamRecord(); + //writerTest(); + ///writerTestWithUserHdr(); + } +}