1/*
2 * Copyright (C) 2010 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
17package benchmarks.regression;
18
19import com.google.caliper.Param;
20import java.io.File;
21import java.io.IOException;
22import java.io.RandomAccessFile;
23import java.nio.ByteBuffer;
24import java.nio.ByteOrder;
25import java.nio.CharBuffer;
26import java.nio.DoubleBuffer;
27import java.nio.FloatBuffer;
28import java.nio.IntBuffer;
29import java.nio.LongBuffer;
30import java.nio.ShortBuffer;
31import java.nio.channels.FileChannel;
32
33public class ByteBufferBenchmark {
34    public enum MyByteOrder {
35        BIG(ByteOrder.BIG_ENDIAN), LITTLE(ByteOrder.LITTLE_ENDIAN);
36        final ByteOrder byteOrder;
37        MyByteOrder(ByteOrder byteOrder) {
38            this.byteOrder = byteOrder;
39        }
40    }
41
42    @Param private MyByteOrder byteOrder;
43
44    @Param({"true", "false"}) private boolean aligned;
45
46    enum MyBufferType {
47        DIRECT, HEAP, MAPPED;
48    }
49    @Param private MyBufferType bufferType;
50
51    public static ByteBuffer newBuffer(MyByteOrder byteOrder, boolean aligned, MyBufferType bufferType) throws IOException {
52        int size = aligned ? 8192 : 8192 + 8 + 1;
53        ByteBuffer result = null;
54        switch (bufferType) {
55        case DIRECT:
56            result = ByteBuffer.allocateDirect(size);
57            break;
58        case HEAP:
59            result = ByteBuffer.allocate(size);
60            break;
61        case MAPPED:
62            File tmpFile = new File("/sdcard/bm.tmp");
63            if (new File("/tmp").isDirectory()) {
64                // We're running on the desktop.
65                tmpFile = File.createTempFile("MappedByteBufferTest", ".tmp");
66            }
67            tmpFile.createNewFile();
68            tmpFile.deleteOnExit();
69            RandomAccessFile raf = new RandomAccessFile(tmpFile, "rw");
70            raf.setLength(8192*8);
71            FileChannel fc = raf.getChannel();
72            result = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
73            break;
74        }
75        result.order(byteOrder.byteOrder);
76        result.position(aligned ? 0 : 1);
77        return result;
78    }
79
80    //
81    // peeking
82    //
83
84    public void timeByteBuffer_getByte(int reps) throws Exception {
85        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
86        for (int rep = 0; rep < reps; ++rep) {
87            src.position(aligned ? 0 : 1);
88            for (int i = 0; i < 1024; ++i) {
89                src.get();
90            }
91        }
92    }
93
94    public void timeByteBuffer_getByteArray(int reps) throws Exception {
95        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
96        byte[] dst = new byte[1024];
97        for (int rep = 0; rep < reps; ++rep) {
98            for (int i = 0; i < 1024; ++i) {
99                src.position(aligned ? 0 : 1);
100                src.get(dst);
101            }
102        }
103    }
104
105    public void timeByteBuffer_getByte_indexed(int reps) throws Exception {
106        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
107        for (int rep = 0; rep < reps; ++rep) {
108            src.position(aligned ? 0 : 1);
109            for (int i = 0; i < 1024; ++i) {
110                src.get(i);
111            }
112        }
113    }
114
115    public void timeByteBuffer_getChar(int reps) throws Exception {
116        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
117        for (int rep = 0; rep < reps; ++rep) {
118            src.position(aligned ? 0 : 1);
119            for (int i = 0; i < 1024; ++i) {
120                src.getChar();
121            }
122        }
123    }
124
125    public void timeCharBuffer_getCharArray(int reps) throws Exception {
126        CharBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asCharBuffer();
127        char[] dst = new char[1024];
128        for (int rep = 0; rep < reps; ++rep) {
129            for (int i = 0; i < 1024; ++i) {
130                src.position(0);
131                src.get(dst);
132            }
133        }
134    }
135
136    public void timeByteBuffer_getChar_indexed(int reps) throws Exception {
137        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
138        for (int rep = 0; rep < reps; ++rep) {
139            src.position(aligned ? 0 : 1);
140            for (int i = 0; i < 1024; ++i) {
141                src.getChar(i * 2);
142            }
143        }
144    }
145
146    public void timeByteBuffer_getDouble(int reps) throws Exception {
147        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
148        for (int rep = 0; rep < reps; ++rep) {
149            src.position(aligned ? 0 : 1);
150            for (int i = 0; i < 1024; ++i) {
151                src.getDouble();
152            }
153        }
154    }
155
156    public void timeDoubleBuffer_getDoubleArray(int reps) throws Exception {
157        DoubleBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asDoubleBuffer();
158        double[] dst = new double[1024];
159        for (int rep = 0; rep < reps; ++rep) {
160            for (int i = 0; i < 1024; ++i) {
161                src.position(0);
162                src.get(dst);
163            }
164        }
165    }
166
167    public void timeByteBuffer_getFloat(int reps) throws Exception {
168        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
169        for (int rep = 0; rep < reps; ++rep) {
170            src.position(aligned ? 0 : 1);
171            for (int i = 0; i < 1024; ++i) {
172                src.getFloat();
173            }
174        }
175    }
176
177    public void timeFloatBuffer_getFloatArray(int reps) throws Exception {
178        FloatBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asFloatBuffer();
179        float[] dst = new float[1024];
180        for (int rep = 0; rep < reps; ++rep) {
181            for (int i = 0; i < 1024; ++i) {
182                src.position(0);
183                src.get(dst);
184            }
185        }
186    }
187
188    public void timeByteBuffer_getInt(int reps) throws Exception {
189        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
190        for (int rep = 0; rep < reps; ++rep) {
191            src.position(aligned ? 0 : 1);
192            for (int i = 0; i < 1024; ++i) {
193                src.getInt();
194            }
195        }
196    }
197
198    public void timeIntBuffer_getIntArray(int reps) throws Exception {
199        IntBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asIntBuffer();
200        int[] dst = new int[1024];
201        for (int rep = 0; rep < reps; ++rep) {
202            for (int i = 0; i < 1024; ++i) {
203                src.position(0);
204                src.get(dst);
205            }
206        }
207    }
208
209    public void timeByteBuffer_getLong(int reps) throws Exception {
210        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
211        for (int rep = 0; rep < reps; ++rep) {
212            src.position(aligned ? 0 : 1);
213            for (int i = 0; i < 1024; ++i) {
214                src.getLong();
215            }
216        }
217    }
218
219    public void timeLongBuffer_getLongArray(int reps) throws Exception {
220        LongBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asLongBuffer();
221        long[] dst = new long[1024];
222        for (int rep = 0; rep < reps; ++rep) {
223            for (int i = 0; i < 1024; ++i) {
224                src.position(0);
225                src.get(dst);
226            }
227        }
228    }
229
230    public void timeByteBuffer_getShort(int reps) throws Exception {
231        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
232        for (int rep = 0; rep < reps; ++rep) {
233            src.position(aligned ? 0 : 1);
234            for (int i = 0; i < 1024; ++i) {
235                src.getShort();
236            }
237        }
238    }
239
240    public void timeShortBuffer_getShortArray(int reps) throws Exception {
241        ShortBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asShortBuffer();
242        short[] dst = new short[1024];
243        for (int rep = 0; rep < reps; ++rep) {
244            for (int i = 0; i < 1024; ++i) {
245                src.position(0);
246                src.get(dst);
247            }
248        }
249    }
250
251    //
252    // poking
253    //
254
255    public void timeByteBuffer_putByte(int reps) throws Exception {
256        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
257        for (int rep = 0; rep < reps; ++rep) {
258            src.position(0);
259            for (int i = 0; i < 1024; ++i) {
260                src.put((byte) 0);
261            }
262        }
263    }
264
265    public void timeByteBuffer_putByteArray(int reps) throws Exception {
266        ByteBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
267        byte[] src = new byte[1024];
268        for (int rep = 0; rep < reps; ++rep) {
269            for (int i = 0; i < 1024; ++i) {
270                dst.position(aligned ? 0 : 1);
271                dst.put(src);
272            }
273        }
274    }
275
276    public void timeByteBuffer_putChar(int reps) throws Exception {
277        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
278        for (int rep = 0; rep < reps; ++rep) {
279            src.position(aligned ? 0 : 1);
280            for (int i = 0; i < 1024; ++i) {
281                src.putChar(' ');
282            }
283        }
284    }
285
286    public void timeCharBuffer_putCharArray(int reps) throws Exception {
287        CharBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asCharBuffer();
288        char[] src = new char[1024];
289        for (int rep = 0; rep < reps; ++rep) {
290            for (int i = 0; i < 1024; ++i) {
291                dst.position(0);
292                dst.put(src);
293            }
294        }
295    }
296
297    public void timeByteBuffer_putDouble(int reps) throws Exception {
298        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
299        for (int rep = 0; rep < reps; ++rep) {
300            src.position(aligned ? 0 : 1);
301            for (int i = 0; i < 1024; ++i) {
302                src.putDouble(0.0);
303            }
304        }
305    }
306
307    public void timeDoubleBuffer_putDoubleArray(int reps) throws Exception {
308        DoubleBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asDoubleBuffer();
309        double[] src = new double[1024];
310        for (int rep = 0; rep < reps; ++rep) {
311            for (int i = 0; i < 1024; ++i) {
312                dst.position(0);
313                dst.put(src);
314            }
315        }
316    }
317
318    public void timeByteBuffer_putFloat(int reps) throws Exception {
319        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
320        for (int rep = 0; rep < reps; ++rep) {
321            src.position(aligned ? 0 : 1);
322            for (int i = 0; i < 1024; ++i) {
323                src.putFloat(0.0f);
324            }
325        }
326    }
327
328    public void timeFloatBuffer_putFloatArray(int reps) throws Exception {
329        FloatBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asFloatBuffer();
330        float[] src = new float[1024];
331        for (int rep = 0; rep < reps; ++rep) {
332            for (int i = 0; i < 1024; ++i) {
333                dst.position(0);
334                dst.put(src);
335            }
336        }
337    }
338
339    public void timeByteBuffer_putInt(int reps) throws Exception {
340        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
341        for (int rep = 0; rep < reps; ++rep) {
342            src.position(aligned ? 0 : 1);
343            for (int i = 0; i < 1024; ++i) {
344                src.putInt(0);
345            }
346        }
347    }
348
349    public void timeIntBuffer_putIntArray(int reps) throws Exception {
350        IntBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asIntBuffer();
351        int[] src = new int[1024];
352        for (int rep = 0; rep < reps; ++rep) {
353            for (int i = 0; i < 1024; ++i) {
354                dst.position(0);
355                dst.put(src);
356            }
357        }
358    }
359
360    public void timeByteBuffer_putLong(int reps) throws Exception {
361        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
362        for (int rep = 0; rep < reps; ++rep) {
363            src.position(aligned ? 0 : 1);
364            for (int i = 0; i < 1024; ++i) {
365                src.putLong(0L);
366            }
367        }
368    }
369
370    public void timeLongBuffer_putLongArray(int reps) throws Exception {
371        LongBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asLongBuffer();
372        long[] src = new long[1024];
373        for (int rep = 0; rep < reps; ++rep) {
374            for (int i = 0; i < 1024; ++i) {
375                dst.position(0);
376                dst.put(src);
377            }
378        }
379    }
380
381    public void timeByteBuffer_putShort(int reps) throws Exception {
382        ByteBuffer src = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType);
383        for (int rep = 0; rep < reps; ++rep) {
384            src.position(aligned ? 0 : 1);
385            for (int i = 0; i < 1024; ++i) {
386                src.putShort((short) 0);
387            }
388        }
389    }
390
391    public void timeShortBuffer_putShortArray(int reps) throws Exception {
392        ShortBuffer dst = ByteBufferBenchmark.newBuffer(byteOrder, aligned, bufferType).asShortBuffer();
393        short[] src = new short[1024];
394        for (int rep = 0; rep < reps; ++rep) {
395            for (int i = 0; i < 1024; ++i) {
396                dst.position(0);
397                dst.put(src);
398            }
399        }
400    }
401
402/*
403    public void time_new_byteArray(int reps) throws Exception {
404        for (int rep = 0; rep < reps; ++rep) {
405            byte[] bs = new byte[8192];
406        }
407    }
408
409    public void time_ByteBuffer_allocate(int reps) throws Exception {
410        for (int rep = 0; rep < reps; ++rep) {
411            ByteBuffer bs = ByteBuffer.allocate(8192);
412        }
413    }
414    */
415}
416