/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 net.arp7.HdfsPerfTest;

import java.io.*;
import java.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import static org.apache.hadoop.fs.CreateFlag.*;

public class ReadFile {
  static final int BUFFER_SIZE = 64 * 1024;
  static boolean lazyPersist;
  static long blockSize;
  static long ioSize;
  static long fileSize;
  static long numReads;

  public static void main (String[] args) throws Exception{
    parseArgs(args);
    final byte[] data = new byte[(int) ioSize];
    Arrays.fill(data, (byte) 0);

    final Random rand = new Random(System.currentTimeMillis()/1000);
    final Configuration conf = new Configuration();
    final FileSystem fs = FileSystem.get(conf);
    DataOutputStream os = null;
    FSDataInputStream is = null;
    boolean success = false;
    long startTime;
    long timeElapsed;
    long totalBytesRead = 0;
    final Path path = new Path("/ReadFile." + rand.nextInt(100000000) + ".dat");

    try {
      EnumSet<CreateFlag> createFlags = EnumSet.of(CREATE, OVERWRITE);
      if (lazyPersist) {
        createFlags.add(LAZY_PERSIST);
      }

      os =
          fs.create(
              path,
              FsPermission.getFileDefault(),
              createFlags,
              BUFFER_SIZE,
              (short) 1,
              blockSize,
              null);

      for (long bytesWritten = 0;
           bytesWritten < fileSize;
           bytesWritten += data.length) {
        os.write(data, 0, data.length);
      }

      System.out.println(" >> Wrote file " + path + " [" + fileSize + " bytes]");

      os.close();
      os = null;
      is = fs.open(path, BUFFER_SIZE);
      startTime = System.nanoTime();

      for (int i = 0; i < numReads; ++i) {
        for (int fileBytesRead = 0;
             fileBytesRead < fileSize;
             fileBytesRead += data.length) {
          is.readFully(data, 0, data.length);
          totalBytesRead += data.length;        
        }
        is.seek(0L);
      }

      timeElapsed = System.nanoTime() - startTime;
      success = true;
    } finally {
      if (os != null) {
        os.close();
      }

      if (is != null) {
        is.close();
      }
    }

    if (success) {
      timeElapsed /= (1000L * 1000);    // Convert to milliseconds.
      System.out.println("Mean Time to read file fully: "+
          timeElapsed / numReads + "ms");
      System.out.println("Mean Read throughput: " +
          ((numReads * fileSize * 1000) / (timeElapsed * 1024 * 1024)) + "MBps");
    }
  }

  static private void parseArgs(String[] args) {
    if (args.length < 4 || args.length > 5) {
      System.err.println(
          "\n  Usage: ReadFile --lazyPersist <blocksize> <filesize> <io-size> <numReads>");
      System.exit(1);
    }

    int argIndex = 0;
    if (args[argIndex].equalsIgnoreCase("--lazyPersist")) {
      lazyPersist = true;
      ++argIndex;
    } else {
      lazyPersist = false;
    }

    blockSize = Utils.parseReadableLong(args[argIndex++]);
    fileSize = Utils.parseReadableLong(args[argIndex++]);
    ioSize =  Utils.parseReadableLong(args[argIndex++]);
    numReads = Utils.parseReadableLong(args[argIndex++]);
  }
}


@@ -837,3195 +837,164 @@
 t;%0A%0A
-import java.io.*;%0Aimport java.util.*;%0Aimport org.apache.hadoop.conf.*;%0Aimport org.apache.hadoop.fs.*;%0Aimport org.apache.hadoop.fs.permission.FsPermission;%0Aimport static org.apache.hadoop.fs.CreateFlag.*;%0A%0Apublic class ReadFile %7B%0A  static final int BUFFER_SIZE = 64 * 1024;%0A  static boolean lazyPersist;%0A  static long blockSize;%0A  static long ioSize;%0A  static long fileSize;%0A  static long numReads;%0A%0A  public static void main (String%5B%5D args) throws Exception%7B%0A    parseArgs(args);%0A    final byte%5B%5D data = new byte%5B(int) ioSize%5D;%0A    Arrays.fill(data, (byte) 0);%0A%0A    final Random rand = new Random(System.currentTimeMillis()/1000);%0A    final Configuration conf = new Configuration();%0A    final FileSystem fs = FileSystem.get(conf);%0A    DataOutputStream os = null;%0A    FSDataInputStream is = null;%0A    boolean success = false;%0A    long startTime;%0A    long timeElapsed;%0A    long totalBytesRead = 0;%0A    final Path path = new Path(%22/ReadFile.%22 + rand.nextInt(100000000) + %22.dat%22);%0A%0A    try %7B%0A      EnumSet%3CCreateFlag%3E createFlags = EnumSet.of(CREATE, OVERWRITE);%0A      if (lazyPersist) %7B%0A        createFlags.add(LAZY_PERSIST);%0A      %7D%0A%0A      os =%0A          fs.create(%0A              path,%0A              FsPermission.getFileDefault(),%0A              createFlags,%0A              BUFFER_SIZE,%0A              (short) 1,%0A              blockSize,%0A              null);%0A%0A      for (long bytesWritten = 0;%0A           bytesWritten %3C fileSize;%0A           bytesWritten += data.length) %7B%0A        os.write(data, 0, data.length);%0A      %7D%0A%0A      System.out.println(%22 %3E%3E Wrote file %22 + path + %22 %5B%22 + fileSize + %22 bytes%5D%22);%0A%0A      os.close();%0A      os = null;%0A      is = fs.open(path, BUFFER_SIZE);%0A      startTime = System.nanoTime();%0A%0A      for (int i = 0; i %3C numReads; ++i) %7B%0A        for (int fileBytesRead = 0;%0A             fileBytesRead %3C fileSize;%0A             fileBytesRead += data.length) %7B%0A          is.readFully(data, 0, data.length);%0A          totalBytesRead += data.length;        %0A        %7D%0A        is.seek(0L);%0A      %7D%0A%0A      timeElapsed = System.nanoTime() - startTime;%0A      success = true;%0A    %7D finally %7B%0A      if (os != null) %7B%0A        os.close();%0A      %7D%0A%0A      if (is != null) %7B%0A        is.close();%0A      %7D%0A    %7D%0A%0A    if (success) %7B%0A      timeElapsed /= (1000L * 1000);    // Convert to milliseconds.%0A      System.out.println(%22Mean Time to read file fully: %22+%0A          timeElapsed / numReads + %22ms%22);%0A      System.out.println(%22Mean Read throughput: %22 +%0A          ((numReads * fileSize * 1000) / (timeElapsed * 1024 * 1024)) + %22MBps%22);%0A    %7D%0A  %7D%0A%0A  static private void parseArgs(String%5B%5D args) %7B%0A    if (args.length %3C 4 %7C%7C args.length %3E 5) %7B%0A      System.err.println(%0A          %22%5Cn  Usage: ReadFile --lazyPersist %3Cblocksize%3E %3Cfilesize%3E %3Cio-size%3E %3CnumReads%3E%22);%0A      System.exit(1);%0A    %7D%0A%0A    int argIndex = 0;%0A    if (args%5BargIndex%5D.equalsIgnoreCase(%22--lazyPersist%22)) %7B%0A      lazyPersist = true;%0A      ++argIndex;%0A    %7D else %7B%0A      lazyPersist = false;%0A    %7D%0A%0A    blockSize = Utils.parseReadableLong(args%5BargIndex++%5D);%0A    fileSize = Utils.parseReadableLong(args%5BargIndex++%5D);%0A    ioSize =  Utils.parseReadableLong(args%5BargIndex++%5D);%0A    numReads = Utils.parseReadableLong(args%5BargIndex++%5D
+%0Apublic class ReadFile %7B%0A  public static void main (String%5B%5D args) throws Exception%7B%0A    System.err.println(%22%5Cn  ReadFile benchmark is not implemented yet.%22
 );%0A 

Remove the old implementation of ReadFile.