1 /*
2  * Copyright (C) 2020 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 
17 package com.android.internal.util;
18 
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 
22 import androidx.test.filters.LargeTest;
23 import androidx.test.runner.AndroidJUnit4;
24 
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import java.io.BufferedInputStream;
30 import java.io.BufferedOutputStream;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.DataInput;
34 import java.io.DataInputStream;
35 import java.io.DataOutput;
36 import java.io.DataOutputStream;
37 import java.io.IOException;
38 
39 @LargeTest
40 @RunWith(AndroidJUnit4.class)
41 public class FastDataPerfTest {
42     @Rule
43     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
44 
45     private static final int OUTPUT_SIZE = 64000;
46     private static final int BUFFER_SIZE = 4096;
47 
48     @Test
timeWrite_Upstream()49     public void timeWrite_Upstream() throws IOException {
50         final ByteArrayOutputStream os = new ByteArrayOutputStream(OUTPUT_SIZE);
51         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
52         while (state.keepRunning()) {
53             os.reset();
54             final BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE);
55             final DataOutput out = new DataOutputStream(bos);
56             doWrite(out);
57             bos.flush();
58         }
59     }
60 
61     @Test
timeWrite_Local()62     public void timeWrite_Local() throws IOException {
63         final ByteArrayOutputStream os = new ByteArrayOutputStream(OUTPUT_SIZE);
64         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
65         while (state.keepRunning()) {
66             os.reset();
67             final FastDataOutput out = new FastDataOutput(os, BUFFER_SIZE);
68             doWrite(out);
69             out.flush();
70         }
71     }
72 
73     @Test
timeRead_Upstream()74     public void timeRead_Upstream() throws Exception {
75         final ByteArrayInputStream is = new ByteArrayInputStream(doWrite());
76         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
77         while (state.keepRunning()) {
78             is.reset();
79             final BufferedInputStream bis = new BufferedInputStream(is, BUFFER_SIZE);
80             final DataInput in = new DataInputStream(bis);
81             doRead(in);
82         }
83     }
84 
85     @Test
timeRead_Local()86     public void timeRead_Local() throws Exception {
87         final ByteArrayInputStream is = new ByteArrayInputStream(doWrite());
88         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
89         while (state.keepRunning()) {
90             is.reset();
91             final DataInput in = new FastDataInput(is, BUFFER_SIZE);
92             doRead(in);
93         }
94     }
95 
96     /**
97      * Since each iteration is around 64 bytes, we need to iterate many times to
98      * exercise the buffer logic.
99      */
100     private static final int REPEATS = 1000;
101 
doWrite()102     private static byte[] doWrite() throws IOException {
103         final ByteArrayOutputStream os = new ByteArrayOutputStream(OUTPUT_SIZE);
104         final DataOutput out = new DataOutputStream(os);
105         doWrite(out);
106         return os.toByteArray();
107     }
108 
doWrite(DataOutput out)109     private static void doWrite(DataOutput out) throws IOException {
110         for (int i = 0; i < REPEATS; i++) {
111             out.writeByte(Byte.MAX_VALUE);
112             out.writeShort(Short.MAX_VALUE);
113             out.writeInt(Integer.MAX_VALUE);
114             out.writeLong(Long.MAX_VALUE);
115             out.writeFloat(Float.MAX_VALUE);
116             out.writeDouble(Double.MAX_VALUE);
117             out.writeUTF("com.example.typical_package_name");
118         }
119     }
120 
doRead(DataInput in)121     private static void doRead(DataInput in) throws IOException {
122         for (int i = 0; i < REPEATS; i++) {
123             in.readByte();
124             in.readShort();
125             in.readInt();
126             in.readLong();
127             in.readFloat();
128             in.readDouble();
129             in.readUTF();
130         }
131     }
132 }
133