From ab67684d37a81077312df6e894f282a9b83879dd Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sat, 9 Oct 2021 18:07:22 +0200 Subject: Use coroutines instead AsyncTask in OpenSSL Speed test --- main/src/main/cpp/ovpnutil/sslspeed.c | 6 +- .../java/de/blinkt/openvpn/core/NativeUtils.java | 2 +- .../de/blinkt/openvpn/activities/OpenSSLSpeed.kt | 112 ++++++++++----------- 3 files changed, 59 insertions(+), 61 deletions(-) diff --git a/main/src/main/cpp/ovpnutil/sslspeed.c b/main/src/main/cpp/ovpnutil/sslspeed.c index be253c6f..7468e23b 100644 --- a/main/src/main/cpp/ovpnutil/sslspeed.c +++ b/main/src/main/cpp/ovpnutil/sslspeed.c @@ -80,9 +80,9 @@ typedef struct loopargs_st { #define MAX_BLOCK_SIZE 128 static unsigned char iv[2 * MAX_BLOCK_SIZE / 8]; -#define SIZE_NUM 6 +#define SIZE_NUM 7 static const int lengths[SIZE_NUM] = { - 16, 64, 256, 1024, 8 * 1024, 16 * 1024 + 16, 64, 256, 1024, 1500, 8 * 1024, 16 * 1024 }; static int testnum; @@ -247,7 +247,7 @@ jdoubleArray Java_de_blinkt_openvpn_core_NativeUtils_getOpenSSLSpeed(JNIEnv* env loopargs_t *loopargs = NULL; int loopargs_len = 1; - int async_jobs=0; + int async_jobs = 0; loopargs = malloc(loopargs_len * sizeof(loopargs_t)); memset(loopargs, 0, loopargs_len * sizeof(loopargs_t)); diff --git a/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java b/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java index d8315c9f..ecfa86ff 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java +++ b/main/src/main/java/de/blinkt/openvpn/core/NativeUtils.java @@ -32,7 +32,7 @@ public class NativeUtils { public static native String getOpenVPN3GitVersion(); public final static int[] openSSLlengths = { - 16, 64, 256, 1024, 8 * 1024, 16 * 1024 + 16, 64, 256, 1024, 1500, 8 * 1024, 16 * 1024 }; public static native double[] getOpenSSLSpeed(String algorithm, int testnum); diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt b/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt index d54cbc35..324cd881 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt @@ -8,7 +8,6 @@ package de.blinkt.openvpn.activities import android.content.Context import android.os.AsyncTask import android.os.Bundle -import android.app.Activity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -16,21 +15,23 @@ import android.widget.ArrayAdapter import android.widget.EditText import android.widget.ListView import android.widget.TextView - -import java.util.Locale -import java.util.Vector - +import androidx.lifecycle.lifecycleScope import de.blinkt.openvpn.R import de.blinkt.openvpn.core.NativeUtils import de.blinkt.openvpn.core.OpenVPNService +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.util.* class OpenSSLSpeed : BaseActivity() { - private lateinit var mCipher: EditText + private lateinit var mCipher: EditText private lateinit var mAdapter: SpeedArrayAdapter private lateinit var mListView: ListView - internal class SpeedArrayAdapter(private val mContext: Context) : ArrayAdapter(mContext, 0) { + internal class SpeedArrayAdapter(private val mContext: Context) : + ArrayAdapter(mContext, 0) { private val mInflater: LayoutInflater init { @@ -39,10 +40,10 @@ class OpenSSLSpeed : BaseActivity() { } internal data class ViewHolder( - var ciphername: TextView, - var speed: TextView, - var blocksize: TextView, - var blocksInTime: TextView + var ciphername: TextView, + var speed: TextView, + var blocksize: TextView, + var blocksInTime: TextView ) override fun getView(position: Int, v: View?, parent: ViewGroup): View { @@ -51,17 +52,22 @@ class OpenSSLSpeed : BaseActivity() { if (view == null) { view = mInflater.inflate(R.layout.speedviewitem, parent, false)!! val holder = ViewHolder( - view.findViewById(R.id.ciphername), - view.findViewById(R.id.speed), - view.findViewById(R.id.blocksize), - view.findViewById(R.id.blocksintime)) + view.findViewById(R.id.ciphername), + view.findViewById(R.id.speed), + view.findViewById(R.id.blocksize), + view.findViewById(R.id.blocksintime) + ) view.tag = holder } val holder = view.tag as ViewHolder val total = res!!.count * res.length - val size = OpenVPNService.humanReadableByteCount(res.length.toLong(), false, mContext.resources) + val size = OpenVPNService.humanReadableByteCount( + res.length.toLong(), + false, + mContext.resources + ) holder.blocksize.text = size holder.ciphername.text = res.algorithm @@ -73,11 +79,22 @@ class OpenSSLSpeed : BaseActivity() { holder.blocksInTime.setText(R.string.running_test) holder.speed.text = "-" } else { - val totalBytes = OpenVPNService.humanReadableByteCount(total.toLong(), false, mContext.resources) + val totalBytes = + OpenVPNService.humanReadableByteCount(total.toLong(), false, mContext.resources) // TODO: Fix localisation here - val blockPerSec = OpenVPNService.humanReadableByteCount((total / res.time).toLong(), false, mContext.resources) + "/s" + val blockPerSec = OpenVPNService.humanReadableByteCount( + (total / res.time).toLong(), + false, + mContext.resources + ) + "/s" holder.speed.text = blockPerSec - holder.blocksInTime.text = String.format(Locale.ENGLISH, "%d blocks (%s) in %2.1fs", res.count.toLong(), totalBytes, res.time) + holder.blocksInTime.text = String.format( + Locale.ENGLISH, + "%d blocks (%s) in %2.1fs", + res.count.toLong(), + totalBytes, + res.time + ) } return view @@ -101,10 +118,9 @@ class OpenSSLSpeed : BaseActivity() { } private fun runAlgorithms(algorithms: String) { - if (runTestAlgorithms != null) - runTestAlgorithms!!.cancel(true) - runTestAlgorithms = SpeeedTest() - runTestAlgorithms!!.execute(*algorithms.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) + lifecycleScope.launch { + runSpeedTest(algorithms) + } } @@ -117,22 +133,29 @@ class OpenSSLSpeed : BaseActivity() { var running = true } + internal suspend fun showResults(vararg values: SpeedResult) { + withContext(Dispatchers.Main) { + for (r in values) { + if (r.running) + mAdapter.add(r) + mAdapter.notifyDataSetChanged() + } + } + } - private inner class SpeeedTest : AsyncTask>() { - private var mCancel = false - - override fun doInBackground(vararg strings: String): Array { + suspend fun runSpeedTest(algorithms: String) { + withContext(Dispatchers.IO) + { val mResult = Vector() - for (algorithm in strings) { - + for (algorithm in algorithms.split(" ")) { // Skip 16b and 16k as they are not relevevant for VPN var i = 1 - while (i < NativeUtils.openSSLlengths.size - 1 && !mCancel) { + while (i < NativeUtils.openSSLlengths.size - 1) { val result = SpeedResult(algorithm) result.length = NativeUtils.openSSLlengths[i] mResult.add(result) - publishProgress(result) + showResults(result) val resi = NativeUtils.getOpenSSLSpeed(algorithm, i) if (resi == null) { result.failed = true @@ -141,36 +164,11 @@ class OpenSSLSpeed : BaseActivity() { result.time = resi[2] } result.running = false - publishProgress(result) + showResults(result) i++ } } - - return mResult.toTypedArray() - - } - - override fun onProgressUpdate(vararg values: SpeedResult) { - for (r in values) { - if (r.running) - mAdapter.add(r) - mAdapter.notifyDataSetChanged() - } - } - - override fun onPostExecute(speedResult: Array) { - - } - - override fun onCancelled(speedResults: Array) { - mCancel = true } } - companion object { - - private var runTestAlgorithms: SpeeedTest? = null - } - - } -- cgit v1.2.3