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.Param;
     20 import com.google.caliper.SimpleBenchmark;
     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 extends SimpleBenchmark {
     31     @Param({"128", "1024", "8192", "65536"}) int compressedSize;
     32     @Param({"4", "32", "128"}) int readSize;
     33 
     34     private File file;
     35 
     36     @Override protected void setUp() throws Exception {
     37         file = File.createTempFile(getClass().getName(), ".zip");
     38         file.deleteOnExit();
     39 
     40         Random random = new Random(0);
     41         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
     42         byte[] data = new byte[8192];
     43         out.putNextEntry(new ZipEntry("entry.data"));
     44         int written = 0;
     45         while (written < compressedSize) {
     46             random.nextBytes(data);
     47             int toWrite = Math.min(compressedSize - written, data.length);
     48             out.write(data, 0, toWrite);
     49             written += toWrite;
     50         }
     51         out.close();
     52     }
     53 
     54     public void timeUnbufferedRead(int reps) throws Exception {
     55         for (int i = 0; i < reps; i++) {
     56             ZipFile zipFile = new ZipFile(file);
     57             ZipEntry entry = zipFile.getEntry("entry.data");
     58             InputStream in = zipFile.getInputStream(entry);
     59             byte[] buffer = new byte[readSize];
     60             while (in.read(buffer) != -1) {
     61             }
     62             in.close();
     63             zipFile.close();
     64         }
     65     }
     66 
     67     public void timeBufferedRead(int reps) throws Exception {
     68         for (int i = 0; i < reps; i++) {
     69             ZipFile zipFile = new ZipFile(file);
     70             ZipEntry entry = zipFile.getEntry("entry.data");
     71             InputStream in = new BufferedInputStream(zipFile.getInputStream(entry));
     72             byte[] buffer = new byte[readSize];
     73             while (in.read(buffer) != -1) {
     74             }
     75             in.close();
     76             zipFile.close();
     77         }
     78     }
     79 }
     80