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 17 package benchmarks.regression; 18 19 import com.google.caliper.Param; 20 import java.nio.ByteBuffer; 21 import java.security.MessageDigest; 22 23 public class MessageDigestBenchmark { 24 25 private static final int DATA_SIZE = 8192; 26 private static final byte[] DATA = new byte[DATA_SIZE]; 27 static { 28 for (int i = 0; i < DATA_SIZE; i++) { 29 DATA[i] = (byte)i; 30 } 31 } 32 33 private static final int LARGE_DATA_SIZE = 256 * 1024; 34 private static final byte[] LARGE_DATA = new byte[LARGE_DATA_SIZE]; 35 static { 36 for (int i = 0; i < LARGE_DATA_SIZE; i++) { 37 LARGE_DATA[i] = (byte)i; 38 } 39 } 40 41 private static final ByteBuffer SMALL_BUFFER = ByteBuffer.wrap(DATA); 42 private static final ByteBuffer SMALL_DIRECT_BUFFER = ByteBuffer.allocateDirect(DATA_SIZE); 43 static { 44 SMALL_DIRECT_BUFFER.put(DATA); SMALL_DIRECT_BUFFER.flip()45 SMALL_DIRECT_BUFFER.flip(); 46 } 47 48 private static final ByteBuffer LARGE_BUFFER = ByteBuffer.wrap(LARGE_DATA); 49 private static final ByteBuffer LARGE_DIRECT_BUFFER = 50 ByteBuffer.allocateDirect(LARGE_DATA_SIZE); 51 static { 52 LARGE_DIRECT_BUFFER.put(LARGE_DATA); LARGE_DIRECT_BUFFER.flip()53 LARGE_DIRECT_BUFFER.flip(); 54 } 55 56 @Param private Algorithm algorithm; 57 58 public enum Algorithm { MD5, SHA1, SHA256, SHA384, SHA512 }; 59 60 @Param private Provider provider; 61 62 public enum Provider { AndroidOpenSSL, BC }; 63 time(int reps)64 public void time(int reps) throws Exception { 65 for (int i = 0; i < reps; ++i) { 66 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 67 provider.toString()); 68 digest.update(DATA, 0, DATA_SIZE); 69 digest.digest(); 70 } 71 } 72 timeLargeArray(int reps)73 public void timeLargeArray(int reps) throws Exception { 74 for (int i = 0; i < reps; ++i) { 75 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 76 provider.toString()); 77 digest.update(LARGE_DATA, 0, LARGE_DATA_SIZE); 78 digest.digest(); 79 } 80 } 81 timeSmallChunkOfLargeArray(int reps)82 public void timeSmallChunkOfLargeArray(int reps) throws Exception { 83 for (int i = 0; i < reps; ++i) { 84 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 85 provider.toString()); 86 digest.update(LARGE_DATA, LARGE_DATA_SIZE / 2, DATA_SIZE); 87 digest.digest(); 88 } 89 } 90 timeSmallByteBuffer(int reps)91 public void timeSmallByteBuffer(int reps) throws Exception { 92 for (int i = 0; i < reps; ++i) { 93 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 94 provider.toString()); 95 SMALL_BUFFER.position(0); 96 SMALL_BUFFER.limit(SMALL_BUFFER.capacity()); 97 digest.update(SMALL_BUFFER); 98 digest.digest(); 99 } 100 } 101 timeSmallDirectByteBuffer(int reps)102 public void timeSmallDirectByteBuffer(int reps) throws Exception { 103 for (int i = 0; i < reps; ++i) { 104 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 105 provider.toString()); 106 SMALL_DIRECT_BUFFER.position(0); 107 SMALL_DIRECT_BUFFER.limit(SMALL_DIRECT_BUFFER.capacity()); 108 digest.update(SMALL_DIRECT_BUFFER); 109 digest.digest(); 110 } 111 } 112 timeLargeByteBuffer(int reps)113 public void timeLargeByteBuffer(int reps) throws Exception { 114 for (int i = 0; i < reps; ++i) { 115 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 116 provider.toString()); 117 LARGE_BUFFER.position(0); 118 LARGE_BUFFER.limit(LARGE_BUFFER.capacity()); 119 digest.update(LARGE_BUFFER); 120 digest.digest(); 121 } 122 } 123 timeLargeDirectByteBuffer(int reps)124 public void timeLargeDirectByteBuffer(int reps) throws Exception { 125 for (int i = 0; i < reps; ++i) { 126 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 127 provider.toString()); 128 LARGE_DIRECT_BUFFER.position(0); 129 LARGE_DIRECT_BUFFER.limit(LARGE_DIRECT_BUFFER.capacity()); 130 digest.update(LARGE_DIRECT_BUFFER); 131 digest.digest(); 132 } 133 } 134 timeSmallChunkOfLargeByteBuffer(int reps)135 public void timeSmallChunkOfLargeByteBuffer(int reps) throws Exception { 136 for (int i = 0; i < reps; ++i) { 137 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 138 provider.toString()); 139 LARGE_BUFFER.position(LARGE_BUFFER.capacity() / 2); 140 LARGE_BUFFER.limit(LARGE_BUFFER.position() + DATA_SIZE); 141 digest.update(LARGE_BUFFER); 142 digest.digest(); 143 } 144 } 145 timeSmallChunkOfLargeDirectByteBuffer(int reps)146 public void timeSmallChunkOfLargeDirectByteBuffer(int reps) throws Exception { 147 for (int i = 0; i < reps; ++i) { 148 MessageDigest digest = MessageDigest.getInstance(algorithm.toString(), 149 provider.toString()); 150 LARGE_DIRECT_BUFFER.position(LARGE_DIRECT_BUFFER.capacity() / 2); 151 LARGE_DIRECT_BUFFER.limit(LARGE_DIRECT_BUFFER.position() + DATA_SIZE); 152 digest.update(LARGE_DIRECT_BUFFER); 153 digest.digest(); 154 } 155 } 156 } 157