1/* 2 * Copyright (C) 2018 The Android Open Source Project 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 17package android.os; 18 19import static android.os.FileUtils.copyInternalSendfile; 20import static android.os.FileUtils.copyInternalSplice; 21import static android.os.FileUtils.copyInternalUserspace; 22 23import android.os.FileUtils.MemoryPipe; 24 25import com.google.caliper.BeforeExperiment; 26import com.google.caliper.Param; 27 28import java.io.File; 29import java.io.FileInputStream; 30import java.io.FileOutputStream; 31 32public class FileUtilsBenchmark { 33 @Param({"32", "32000", "32000000"}) 34 private int mSize; 35 36 private File mSrc; 37 private File mDest; 38 39 private byte[] mData; 40 41 @BeforeExperiment 42 protected void setUp() throws Exception { 43 mSrc = new File("/data/local/tmp/src"); 44 mDest = new File("/data/local/tmp/dest"); 45 46 mData = new byte[mSize]; 47 48 try (FileOutputStream os = new FileOutputStream(mSrc)) { 49 os.write(mData); 50 } 51 } 52 53 public void timeRegularUserspace(int reps) throws Exception { 54 for (int i = 0; i < reps; i++) { 55 try (FileInputStream in = new FileInputStream(mSrc); 56 FileOutputStream out = new FileOutputStream(mDest)) { 57 copyInternalUserspace(in.getFD(), out.getFD(), null, null, Long.MAX_VALUE); 58 } 59 } 60 } 61 62 public void timeRegularSendfile(int reps) throws Exception { 63 for (int i = 0; i < reps; i++) { 64 try (FileInputStream in = new FileInputStream(mSrc); 65 FileOutputStream out = new FileOutputStream(mDest)) { 66 copyInternalSendfile(in.getFD(), out.getFD(), null, null, Long.MAX_VALUE); 67 } 68 } 69 } 70 71 public void timePipeSourceUserspace(int reps) throws Exception { 72 for (int i = 0; i < reps; i++) { 73 try (MemoryPipe in = MemoryPipe.createSource(mData); 74 FileOutputStream out = new FileOutputStream(mDest)) { 75 copyInternalUserspace(in.getFD(), out.getFD(), null, null, Long.MAX_VALUE); 76 } 77 } 78 } 79 80 public void timePipeSourceSplice(int reps) throws Exception { 81 for (int i = 0; i < reps; i++) { 82 try (MemoryPipe in = MemoryPipe.createSource(mData); 83 FileOutputStream out = new FileOutputStream(mDest)) { 84 copyInternalSplice(in.getFD(), out.getFD(), null, null, Long.MAX_VALUE); 85 } 86 } 87 } 88 89 public void timePipeSinkUserspace(int reps) throws Exception { 90 for (int i = 0; i < reps; i++) { 91 try (FileInputStream in = new FileInputStream(mSrc); 92 MemoryPipe out = MemoryPipe.createSink(mData)) { 93 copyInternalUserspace(in.getFD(), out.getFD(), null, null, Long.MAX_VALUE); 94 } 95 } 96 } 97 98 public void timePipeSinkSplice(int reps) throws Exception { 99 for (int i = 0; i < reps; i++) { 100 try (FileInputStream in = new FileInputStream(mSrc); 101 MemoryPipe out = MemoryPipe.createSink(mData)) { 102 copyInternalSplice(in.getFD(), out.getFD(), null, null, Long.MAX_VALUE); 103 } 104 } 105 } 106} 107