Home | History | Annotate | Download | only in benchmarks
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * 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  */
     16 
     17 package benchmarks;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import com.google.caliper.Param;
     21 import java.io.BufferedInputStream;
     22 import java.io.File;
     23 import java.io.FileOutputStream;
     24 import java.io.InputStream;
     25 import java.util.Random;
     26 import java.util.zip.ZipEntry;
     27 import java.util.zip.ZipFile;
     28 import java.util.zip.ZipOutputStream;
     29 
     30 public final class BufferedZipFileBenchmark {
     31     @Param({"128", "1024", "8192", "65536"}) int compressedSize;
     32     @Param({"4", "32", "128"}) int readSize;
     33 
     34     private File file;
     35 
     36     @BeforeExperiment
     37     protected void setUp() throws Exception {
     38         System.setProperty("java.io.tmpdir", "/data/local/tmp");
     39         file = File.createTempFile(getClass().getName(), ".zip");
     40         file.deleteOnExit();
     41 
     42         Random random = new Random(0);
     43         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
     44         byte[] data = new byte[8192];
     45         out.putNextEntry(new ZipEntry("entry.data"));
     46         int written = 0;
     47         while (written < compressedSize) {
     48             random.nextBytes(data);
     49             int toWrite = Math.min(compressedSize - written, data.length);
     50             out.write(data, 0, toWrite);
     51             written += toWrite;
     52         }
     53         out.close();
     54     }
     55 
     56     public void timeUnbufferedRead(int reps) throws Exception {
     57         for (int i = 0; i < reps; i++) {
     58             ZipFile zipFile = new ZipFile(file);
     59             ZipEntry entry = zipFile.getEntry("entry.data");
     60             InputStream in = zipFile.getInputStream(entry);
     61             byte[] buffer = new byte[readSize];
     62             while (in.read(buffer) != -1) {
     63             }
     64             in.close();
     65             zipFile.close();
     66         }
     67     }
     68 
     69     public void timeBufferedRead(int reps) throws Exception {
     70         for (int i = 0; i < reps; i++) {
     71             ZipFile zipFile = new ZipFile(file);
     72             ZipEntry entry = zipFile.getEntry("entry.data");
     73             InputStream in = new BufferedInputStream(zipFile.getInputStream(entry));
     74             byte[] buffer = new byte[readSize];
     75             while (in.read(buffer) != -1) {
     76             }
     77             in.close();
     78             zipFile.close();
     79         }
     80     }
     81 }
     82