BenchPwh.kt (2284B)
1 /* 2 * This file is part of LibEuFin. 3 * Copyright (C) 2024-2025 Taler Systems S.A. 4 5 * LibEuFin is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation; either version 3, or 8 * (at your option) any later version. 9 10 * LibEuFin is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General 13 * Public License for more details. 14 15 * You should have received a copy of the GNU Affero General Public 16 * License along with LibEuFin; see the file COPYING. If not, see 17 * <http://www.gnu.org/licenses/> 18 */ 19 20 package tech.libeufin.bank.cli 21 22 import com.github.ajalt.clikt.core.CliktCommand 23 import com.github.ajalt.clikt.core.Context 24 import com.github.ajalt.clikt.parameters.groups.provideDelegate 25 import tech.libeufin.bank.bankConfig 26 import tech.libeufin.bank.logger 27 import tech.libeufin.common.TalerCmd 28 import tech.libeufin.common.crypto.PwCrypto 29 30 class BenchPwh : TalerCmd("bench-pwh") { 31 override fun help(context: Context) = "Benchmark password hashing algorithm and configuration" 32 33 override fun run() = cliCmd(logger) { 34 val pwCrypto = bankConfig(config).pwCrypto 35 36 when (pwCrypto) { 37 is PwCrypto.Bcrypt -> println("Benching bcrypt with cost=${pwCrypto.cost} for 10s") 38 } 39 40 val start = System.currentTimeMillis() 41 val stop = start + 10000 // 10s 42 var count = 0 43 44 while (true) { 45 val now = System.currentTimeMillis() 46 if (now < stop) { 47 val password = (0..10).map { 48 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#".random() 49 }.joinToString("") 50 pwCrypto.hashpw(password) 51 count ++ 52 } else { 53 val elapsed = (now-start).toDouble() 54 val perSec = count.toDouble() / (elapsed / 1000.0) 55 val iterTime = elapsed / count.toDouble() 56 println("hash password in ${String.format("%.0f", iterTime)}ms ${String.format("%.2f", perSec)} H/s") 57 break 58 } 59 } 60 } 61 }