ScannerParseLargeFileBenchmarkTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.apache.harmony.luni.tests.java.util; 17 18import java.io.IOException; 19import java.io.Reader; 20import java.util.Scanner; 21 22import org.junit.Test; 23 24public class ScannerParseLargeFileBenchmarkTest { 25 26 /** 27 * This test will check when parse a large file like more than 200M bytes if 28 * the Scanner will exhaust all heap memory 29 */ 30 @Test 31 public void testParseLargeFile() throws Exception { 32 MyReader reader = new MyReader(); 33 String delimiter = "\r?\n"; 34 Scanner scanner = new Scanner(reader).useDelimiter(delimiter); 35 36 while (scanner.hasNext()) { 37 scanner.next(); 38 } 39 scanner.close(); 40 reader.close(); 41 } 42 43 private static class MyReader extends Reader { 44 static final char[] CONTENT = "large file!\n".toCharArray(); 45 46 static long fileLength = (8 << 21) * 12; 47 48 static boolean first = true; 49 50 static int position = 0; 51 52 private int count = 0; 53 54 @Override 55 public void close() throws IOException { 56 } 57 58 @Override 59 public int read(char[] buf, int offset, int length) { 60 if (count >= fileLength) { 61 return -1; 62 } 63 if (first == true) { 64 position = 0; 65 first = false; 66 } 67 for (int i = offset; i < length; i++) { 68 buf[i] = CONTENT[(i + position) % CONTENT.length]; 69 count++; 70 } 71 72 position = (length + position) % CONTENT.length; 73 74 return length - offset; 75 } 76 } 77} 78