@Test public void testBenchmark() throws Exception { BenchmarkOperation op = new BenchmarkOperation(RuntimeEnvironment.application, KeyWritableRepository.create(RuntimeEnvironment.application), null); op.execute(BenchmarkInputParcel.newInstance(), null); }
@NonNull @Override public BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel, CryptoInputParcel cryptoInputParcel) { OperationLog log = new OperationLog(); log.add(LogType.MSG_BENCH, 0); byte[] buf = new byte[1024*1024*10]; new Random().nextBytes(buf); Passphrase passphrase = new Passphrase("a"); int numRepeats = 5; long totalTime = 0; SignEncryptResult encryptResult; int i = 0; do { SignEncryptOperation op = new SignEncryptOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, i*(50/numRepeats), (i+1)*(50/numRepeats), 100), mCancelled); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); data.setSymmetricPassphrase(passphrase); data.setSymmetricEncryptionAlgorithm(SymmetricKeyAlgorithmTags.AES_128); SignEncryptParcel input = SignEncryptParcel.createSignEncryptParcel(data.build(), buf); encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); log.add(encryptResult, 1); log.add(LogType.MSG_BENCH_ENC_TIME, 2, String.format("%.2f", encryptResult.getResults().get(0).mOperationTime / 1000.0)); totalTime += encryptResult.getResults().get(0).mOperationTime; } while (++i < numRepeats); long encryptionTime = totalTime / numRepeats; totalTime = 0; i = 0; do { DecryptVerifyResult decryptResult; PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, 50 +i*(50/numRepeats), 50 +(i+1)*(50/numRepeats), 100)); PgpDecryptVerifyInputParcel.Builder builder = PgpDecryptVerifyInputParcel.builder() .setInputBytes(encryptResult.getResultBytes()) .setAllowSymmetricDecryption(true); decryptResult = op.execute(builder.build(), CryptoInputParcel.createCryptoInputParcel(passphrase)); log.add(decryptResult, 1); log.add(LogType.MSG_BENCH_DEC_TIME, 2, String.format("%.2f", decryptResult.mOperationTime / 1000.0)); totalTime += decryptResult.mOperationTime; } while (++i < numRepeats); long decryptionTime = totalTime / numRepeats; totalTime = 0; int iterationsFor100ms; try { PGPDigestCalculatorProvider digestCalcProvider = new JcaPGPDigestCalculatorProviderBuilder() .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(); PBEDataDecryptorFactory decryptorFactory = new JcePBEDataDecryptorFactoryBuilder( digestCalcProvider).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( "".toCharArray()); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; int iterations = 0; while (iterations < 255 && totalTime < 100) { iterations += 1; S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, iterations); totalTime = System.currentTimeMillis(); decryptorFactory.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k); totalTime = System.currentTimeMillis() -totalTime; if ((iterations % 10) == 0) { log.add(LogType.MSG_BENCH_S2K_FOR_IT, 1, Integer.toString(iterations), Long.toString(totalTime)); } } iterationsFor100ms = iterations; } catch (PGPException e) { Timber.e(e, "internal error during benchmark"); log.add(LogType.MSG_INTERNAL_ERROR, 0); return new BenchmarkResult(BenchmarkResult.RESULT_ERROR, log); } log.add(LogType.MSG_BENCH_S2K_100MS_ITS, 1, Integer.toString(iterationsFor100ms)); log.add(LogType.MSG_BENCH_ENC_TIME_AVG, 1, String.format("%.2f", encryptionTime/1000.0)); log.add(LogType.MSG_BENCH_DEC_TIME_AVG, 1, String.format("%.2f", decryptionTime/1000.0)); log.add(LogType.MSG_BENCH_SUCCESS, 0); return new BenchmarkResult(BenchmarkResult.RESULT_OK, log); }
BenchmarkOperation extends BaseOperation<BenchmarkInputParcel> { @NonNull @Override public BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel, CryptoInputParcel cryptoInputParcel) { OperationLog log = new OperationLog(); log.add(LogType.MSG_BENCH, 0); byte[] buf = new byte[1024*1024*10]; new Random().nextBytes(buf); Passphrase passphrase = new Passphrase("a"); int numRepeats = 5; long totalTime = 0; SignEncryptResult encryptResult; int i = 0; do { SignEncryptOperation op = new SignEncryptOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, i*(50/numRepeats), (i+1)*(50/numRepeats), 100), mCancelled); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); data.setSymmetricPassphrase(passphrase); data.setSymmetricEncryptionAlgorithm(SymmetricKeyAlgorithmTags.AES_128); SignEncryptParcel input = SignEncryptParcel.createSignEncryptParcel(data.build(), buf); encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); log.add(encryptResult, 1); log.add(LogType.MSG_BENCH_ENC_TIME, 2, String.format("%.2f", encryptResult.getResults().get(0).mOperationTime / 1000.0)); totalTime += encryptResult.getResults().get(0).mOperationTime; } while (++i < numRepeats); long encryptionTime = totalTime / numRepeats; totalTime = 0; i = 0; do { DecryptVerifyResult decryptResult; PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, 50 +i*(50/numRepeats), 50 +(i+1)*(50/numRepeats), 100)); PgpDecryptVerifyInputParcel.Builder builder = PgpDecryptVerifyInputParcel.builder() .setInputBytes(encryptResult.getResultBytes()) .setAllowSymmetricDecryption(true); decryptResult = op.execute(builder.build(), CryptoInputParcel.createCryptoInputParcel(passphrase)); log.add(decryptResult, 1); log.add(LogType.MSG_BENCH_DEC_TIME, 2, String.format("%.2f", decryptResult.mOperationTime / 1000.0)); totalTime += decryptResult.mOperationTime; } while (++i < numRepeats); long decryptionTime = totalTime / numRepeats; totalTime = 0; int iterationsFor100ms; try { PGPDigestCalculatorProvider digestCalcProvider = new JcaPGPDigestCalculatorProviderBuilder() .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(); PBEDataDecryptorFactory decryptorFactory = new JcePBEDataDecryptorFactoryBuilder( digestCalcProvider).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( "".toCharArray()); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; int iterations = 0; while (iterations < 255 && totalTime < 100) { iterations += 1; S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, iterations); totalTime = System.currentTimeMillis(); decryptorFactory.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k); totalTime = System.currentTimeMillis() -totalTime; if ((iterations % 10) == 0) { log.add(LogType.MSG_BENCH_S2K_FOR_IT, 1, Integer.toString(iterations), Long.toString(totalTime)); } } iterationsFor100ms = iterations; } catch (PGPException e) { Timber.e(e, "internal error during benchmark"); log.add(LogType.MSG_INTERNAL_ERROR, 0); return new BenchmarkResult(BenchmarkResult.RESULT_ERROR, log); } log.add(LogType.MSG_BENCH_S2K_100MS_ITS, 1, Integer.toString(iterationsFor100ms)); log.add(LogType.MSG_BENCH_ENC_TIME_AVG, 1, String.format("%.2f", encryptionTime/1000.0)); log.add(LogType.MSG_BENCH_DEC_TIME_AVG, 1, String.format("%.2f", decryptionTime/1000.0)); log.add(LogType.MSG_BENCH_SUCCESS, 0); return new BenchmarkResult(BenchmarkResult.RESULT_OK, log); } }
BenchmarkOperation extends BaseOperation<BenchmarkInputParcel> { @NonNull @Override public BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel, CryptoInputParcel cryptoInputParcel) { OperationLog log = new OperationLog(); log.add(LogType.MSG_BENCH, 0); byte[] buf = new byte[1024*1024*10]; new Random().nextBytes(buf); Passphrase passphrase = new Passphrase("a"); int numRepeats = 5; long totalTime = 0; SignEncryptResult encryptResult; int i = 0; do { SignEncryptOperation op = new SignEncryptOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, i*(50/numRepeats), (i+1)*(50/numRepeats), 100), mCancelled); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); data.setSymmetricPassphrase(passphrase); data.setSymmetricEncryptionAlgorithm(SymmetricKeyAlgorithmTags.AES_128); SignEncryptParcel input = SignEncryptParcel.createSignEncryptParcel(data.build(), buf); encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); log.add(encryptResult, 1); log.add(LogType.MSG_BENCH_ENC_TIME, 2, String.format("%.2f", encryptResult.getResults().get(0).mOperationTime / 1000.0)); totalTime += encryptResult.getResults().get(0).mOperationTime; } while (++i < numRepeats); long encryptionTime = totalTime / numRepeats; totalTime = 0; i = 0; do { DecryptVerifyResult decryptResult; PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, 50 +i*(50/numRepeats), 50 +(i+1)*(50/numRepeats), 100)); PgpDecryptVerifyInputParcel.Builder builder = PgpDecryptVerifyInputParcel.builder() .setInputBytes(encryptResult.getResultBytes()) .setAllowSymmetricDecryption(true); decryptResult = op.execute(builder.build(), CryptoInputParcel.createCryptoInputParcel(passphrase)); log.add(decryptResult, 1); log.add(LogType.MSG_BENCH_DEC_TIME, 2, String.format("%.2f", decryptResult.mOperationTime / 1000.0)); totalTime += decryptResult.mOperationTime; } while (++i < numRepeats); long decryptionTime = totalTime / numRepeats; totalTime = 0; int iterationsFor100ms; try { PGPDigestCalculatorProvider digestCalcProvider = new JcaPGPDigestCalculatorProviderBuilder() .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(); PBEDataDecryptorFactory decryptorFactory = new JcePBEDataDecryptorFactoryBuilder( digestCalcProvider).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( "".toCharArray()); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; int iterations = 0; while (iterations < 255 && totalTime < 100) { iterations += 1; S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, iterations); totalTime = System.currentTimeMillis(); decryptorFactory.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k); totalTime = System.currentTimeMillis() -totalTime; if ((iterations % 10) == 0) { log.add(LogType.MSG_BENCH_S2K_FOR_IT, 1, Integer.toString(iterations), Long.toString(totalTime)); } } iterationsFor100ms = iterations; } catch (PGPException e) { Timber.e(e, "internal error during benchmark"); log.add(LogType.MSG_INTERNAL_ERROR, 0); return new BenchmarkResult(BenchmarkResult.RESULT_ERROR, log); } log.add(LogType.MSG_BENCH_S2K_100MS_ITS, 1, Integer.toString(iterationsFor100ms)); log.add(LogType.MSG_BENCH_ENC_TIME_AVG, 1, String.format("%.2f", encryptionTime/1000.0)); log.add(LogType.MSG_BENCH_DEC_TIME_AVG, 1, String.format("%.2f", decryptionTime/1000.0)); log.add(LogType.MSG_BENCH_SUCCESS, 0); return new BenchmarkResult(BenchmarkResult.RESULT_OK, log); } BenchmarkOperation(Context context, KeyWritableRepository databaseInteractor, Progressable
            progressable); }
BenchmarkOperation extends BaseOperation<BenchmarkInputParcel> { @NonNull @Override public BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel, CryptoInputParcel cryptoInputParcel) { OperationLog log = new OperationLog(); log.add(LogType.MSG_BENCH, 0); byte[] buf = new byte[1024*1024*10]; new Random().nextBytes(buf); Passphrase passphrase = new Passphrase("a"); int numRepeats = 5; long totalTime = 0; SignEncryptResult encryptResult; int i = 0; do { SignEncryptOperation op = new SignEncryptOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, i*(50/numRepeats), (i+1)*(50/numRepeats), 100), mCancelled); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); data.setSymmetricPassphrase(passphrase); data.setSymmetricEncryptionAlgorithm(SymmetricKeyAlgorithmTags.AES_128); SignEncryptParcel input = SignEncryptParcel.createSignEncryptParcel(data.build(), buf); encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); log.add(encryptResult, 1); log.add(LogType.MSG_BENCH_ENC_TIME, 2, String.format("%.2f", encryptResult.getResults().get(0).mOperationTime / 1000.0)); totalTime += encryptResult.getResults().get(0).mOperationTime; } while (++i < numRepeats); long encryptionTime = totalTime / numRepeats; totalTime = 0; i = 0; do { DecryptVerifyResult decryptResult; PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, 50 +i*(50/numRepeats), 50 +(i+1)*(50/numRepeats), 100)); PgpDecryptVerifyInputParcel.Builder builder = PgpDecryptVerifyInputParcel.builder() .setInputBytes(encryptResult.getResultBytes()) .setAllowSymmetricDecryption(true); decryptResult = op.execute(builder.build(), CryptoInputParcel.createCryptoInputParcel(passphrase)); log.add(decryptResult, 1); log.add(LogType.MSG_BENCH_DEC_TIME, 2, String.format("%.2f", decryptResult.mOperationTime / 1000.0)); totalTime += decryptResult.mOperationTime; } while (++i < numRepeats); long decryptionTime = totalTime / numRepeats; totalTime = 0; int iterationsFor100ms; try { PGPDigestCalculatorProvider digestCalcProvider = new JcaPGPDigestCalculatorProviderBuilder() .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(); PBEDataDecryptorFactory decryptorFactory = new JcePBEDataDecryptorFactoryBuilder( digestCalcProvider).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( "".toCharArray()); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; int iterations = 0; while (iterations < 255 && totalTime < 100) { iterations += 1; S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, iterations); totalTime = System.currentTimeMillis(); decryptorFactory.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k); totalTime = System.currentTimeMillis() -totalTime; if ((iterations % 10) == 0) { log.add(LogType.MSG_BENCH_S2K_FOR_IT, 1, Integer.toString(iterations), Long.toString(totalTime)); } } iterationsFor100ms = iterations; } catch (PGPException e) { Timber.e(e, "internal error during benchmark"); log.add(LogType.MSG_INTERNAL_ERROR, 0); return new BenchmarkResult(BenchmarkResult.RESULT_ERROR, log); } log.add(LogType.MSG_BENCH_S2K_100MS_ITS, 1, Integer.toString(iterationsFor100ms)); log.add(LogType.MSG_BENCH_ENC_TIME_AVG, 1, String.format("%.2f", encryptionTime/1000.0)); log.add(LogType.MSG_BENCH_DEC_TIME_AVG, 1, String.format("%.2f", decryptionTime/1000.0)); log.add(LogType.MSG_BENCH_SUCCESS, 0); return new BenchmarkResult(BenchmarkResult.RESULT_OK, log); } BenchmarkOperation(Context context, KeyWritableRepository databaseInteractor, Progressable
            progressable); @NonNull @Override BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel,
                                     CryptoInputParcel cryptoInputParcel); }
BenchmarkOperation extends BaseOperation<BenchmarkInputParcel> { @NonNull @Override public BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel, CryptoInputParcel cryptoInputParcel) { OperationLog log = new OperationLog(); log.add(LogType.MSG_BENCH, 0); byte[] buf = new byte[1024*1024*10]; new Random().nextBytes(buf); Passphrase passphrase = new Passphrase("a"); int numRepeats = 5; long totalTime = 0; SignEncryptResult encryptResult; int i = 0; do { SignEncryptOperation op = new SignEncryptOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, i*(50/numRepeats), (i+1)*(50/numRepeats), 100), mCancelled); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); data.setSymmetricPassphrase(passphrase); data.setSymmetricEncryptionAlgorithm(SymmetricKeyAlgorithmTags.AES_128); SignEncryptParcel input = SignEncryptParcel.createSignEncryptParcel(data.build(), buf); encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); log.add(encryptResult, 1); log.add(LogType.MSG_BENCH_ENC_TIME, 2, String.format("%.2f", encryptResult.getResults().get(0).mOperationTime / 1000.0)); totalTime += encryptResult.getResults().get(0).mOperationTime; } while (++i < numRepeats); long encryptionTime = totalTime / numRepeats; totalTime = 0; i = 0; do { DecryptVerifyResult decryptResult; PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(mContext, mKeyRepository, new ProgressScaler(mProgressable, 50 +i*(50/numRepeats), 50 +(i+1)*(50/numRepeats), 100)); PgpDecryptVerifyInputParcel.Builder builder = PgpDecryptVerifyInputParcel.builder() .setInputBytes(encryptResult.getResultBytes()) .setAllowSymmetricDecryption(true); decryptResult = op.execute(builder.build(), CryptoInputParcel.createCryptoInputParcel(passphrase)); log.add(decryptResult, 1); log.add(LogType.MSG_BENCH_DEC_TIME, 2, String.format("%.2f", decryptResult.mOperationTime / 1000.0)); totalTime += decryptResult.mOperationTime; } while (++i < numRepeats); long decryptionTime = totalTime / numRepeats; totalTime = 0; int iterationsFor100ms; try { PGPDigestCalculatorProvider digestCalcProvider = new JcaPGPDigestCalculatorProviderBuilder() .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(); PBEDataDecryptorFactory decryptorFactory = new JcePBEDataDecryptorFactoryBuilder( digestCalcProvider).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( "".toCharArray()); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; int iterations = 0; while (iterations < 255 && totalTime < 100) { iterations += 1; S2K s2k = new S2K(HashAlgorithmTags.SHA1, iv, iterations); totalTime = System.currentTimeMillis(); decryptorFactory.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k); totalTime = System.currentTimeMillis() -totalTime; if ((iterations % 10) == 0) { log.add(LogType.MSG_BENCH_S2K_FOR_IT, 1, Integer.toString(iterations), Long.toString(totalTime)); } } iterationsFor100ms = iterations; } catch (PGPException e) { Timber.e(e, "internal error during benchmark"); log.add(LogType.MSG_INTERNAL_ERROR, 0); return new BenchmarkResult(BenchmarkResult.RESULT_ERROR, log); } log.add(LogType.MSG_BENCH_S2K_100MS_ITS, 1, Integer.toString(iterationsFor100ms)); log.add(LogType.MSG_BENCH_ENC_TIME_AVG, 1, String.format("%.2f", encryptionTime/1000.0)); log.add(LogType.MSG_BENCH_DEC_TIME_AVG, 1, String.format("%.2f", decryptionTime/1000.0)); log.add(LogType.MSG_BENCH_SUCCESS, 0); return new BenchmarkResult(BenchmarkResult.RESULT_OK, log); } BenchmarkOperation(Context context, KeyWritableRepository databaseInteractor, Progressable
            progressable); @NonNull @Override BenchmarkResult execute(BenchmarkInputParcel consolidateInputParcel,
                                     CryptoInputParcel cryptoInputParcel);  }