| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.tests.stubs; |
| 14 | |
| 15 | import java.io.BufferedOutputStream; |
| 16 | import java.io.DataOutputStream; |
| 17 | import java.io.File; |
| 18 | import java.io.FileNotFoundException; |
| 19 | import java.io.FileOutputStream; |
| 20 | import java.io.IOException; |
| 21 | import java.util.Random; |
| 22 | |
| 23 | /** |
| 24 | * <b><u>CreateTestFiles</u></b> |
| 25 | * <p> |
| 26 | * Create a number of event test files of various lengths. |
| 27 | * <p> |
| 28 | * Events have the following format: |
| 29 | * <ul> |
| 30 | * <li> [timestamp] [source] [type] [ref] [field]* |
| 31 | * <li> There are NB_SOURCES sources and NB_TYPES types. |
| 32 | * <li> The number of fields (0 .. NB_TYPES-1) depends on the event type. |
| 33 | * </ul> |
| 34 | */ |
| 35 | @SuppressWarnings("nls") |
| 36 | public class CreateTestFiles { |
| 37 | |
| 38 | // ======================================================================== |
| 39 | // Constants |
| 40 | // ======================================================================== |
| 41 | |
| 42 | private static final String DIRECTORY = "testfiles"; |
| 43 | // private static final String FILE_NAMES[] = { "Test-10", "Test-1K", "Test-10K", "Test-100K" }; |
| 44 | // private static final int FILE_SIZES[] = { 10 , 1000 , 10000 , 100000 }; |
| 45 | private static final String FILE_NAMES[] = { "Test-10K" }; |
| 46 | private static final int FILE_SIZES[] = { 10000 }; |
| 47 | |
| 48 | private static final int NB_SOURCES = 15; |
| 49 | private static final int NB_TYPES = 7; |
| 50 | |
| 51 | // ======================================================================== |
| 52 | // Constructors |
| 53 | // ======================================================================== |
| 54 | |
| 55 | /** |
| 56 | * @param args |
| 57 | */ |
| 58 | public static void main(final String[] args) { |
| 59 | |
| 60 | try { |
| 61 | System.out.println("Creating test files in directory: " + new File(".").getCanonicalPath() + File.separator + DIRECTORY); |
| 62 | } catch (final IOException e) { |
| 63 | e.printStackTrace(); |
| 64 | } |
| 65 | |
| 66 | for (int i = 0; i < FILE_SIZES.length; i++) |
| 67 | try { |
| 68 | createTestFile("testfiles" + File.separator + "O-" + FILE_NAMES[i], FILE_SIZES[i], true, true); |
| 69 | createTestFile("testfiles" + File.separator + "E-" + FILE_NAMES[i], FILE_SIZES[i], true, false); |
| 70 | createTestFile("testfiles" + File.separator + "R-" + FILE_NAMES[i], FILE_SIZES[i], false, false); |
| 71 | } catch (final FileNotFoundException e) { |
| 72 | } catch (final IOException e) { |
| 73 | } |
| 74 | |
| 75 | System.out.println("Done."); |
| 76 | } |
| 77 | |
| 78 | // ======================================================================== |
| 79 | // Operators |
| 80 | // ======================================================================== |
| 81 | |
| 82 | /** |
| 83 | * @param file |
| 84 | * @param size |
| 85 | * @param monotonic |
| 86 | * @throws FileNotFoundException |
| 87 | * @throws IOException |
| 88 | */ |
| 89 | private static void createTestFile(final String file, final int size, final boolean monotonic, final boolean odd) throws FileNotFoundException, IOException { |
| 90 | DataOutputStream out; |
| 91 | System.out.println("Creating " + file); |
| 92 | out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); |
| 93 | |
| 94 | final Random generator = new Random(19580427 + size); |
| 95 | long ts = (monotonic && odd) ? -1 : 0; |
| 96 | for (int i = 0; i < size; i++) { |
| 97 | ts += monotonic ? 2 : generator.nextInt(10); |
| 98 | final int sourceIndex = i % NB_SOURCES; |
| 99 | final int typeIndex = i % NB_TYPES; |
| 100 | out.writeLong(ts); // Timestamp |
| 101 | out.writeUTF("Source-" + sourceIndex); // Source |
| 102 | out.writeUTF("Type-" + typeIndex); // Type |
| 103 | out.writeInt(i + 1); // Reference (event #) |
| 104 | for (int j = 0; j < typeIndex; j++) |
| 105 | out.writeUTF("Field-" + sourceIndex + "-" + j); |
| 106 | } |
| 107 | out.flush(); |
| 108 | out.close(); |
| 109 | } |
| 110 | |
| 111 | } |