| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import static android.os.FileUtils.copyInternalSendfile; |
| 20 |
import static android.os.FileUtils.copyInternalSplice; |
| 21 |
import static android.os.FileUtils.copyInternalUserspace; |
| 22 |
|
| 23 |
import android.os.FileUtils.MemoryPipe; |
| 24 |
|
| 25 |
import com.google.caliper.BeforeExperiment; |
| 26 |
import com.google.caliper.Param; |
| 27 |
|
| 28 |
import java.io.File; |
| 29 |
import java.io.FileInputStream; |
| 30 |
import java.io.FileOutputStream; |
| 31 |
|
| 32 |
public 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(), Long.MAX_VALUE, null, null, null); |
| 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(), Long.MAX_VALUE, null, null, null); |
| 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(), Long.MAX_VALUE, null, null, null); |
| 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(), Long.MAX_VALUE, null, null, null); |
| 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(), Long.MAX_VALUE, null, null, null); |
| 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(), Long.MAX_VALUE, null, null, null); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |