0
0
mirror of https://github.com/schwabe/ics-openvpn.git synced 2024-09-19 19:42:29 +02:00

Use coroutines instead AsyncTask in OpenSSL Speed test

This commit is contained in:
Arne Schwabe 2021-10-09 18:07:22 +02:00
parent ca5473055a
commit ab67684d37
3 changed files with 59 additions and 61 deletions

View File

@ -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));

View File

@ -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);

View File

@ -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<SpeedResult>(mContext, 0) {
internal class SpeedArrayAdapter(private val mContext: Context) :
ArrayAdapter<SpeedResult>(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<String, SpeedResult, Array<SpeedResult>>() {
private var mCancel = false
override fun doInBackground(vararg strings: String): Array<SpeedResult> {
suspend fun runSpeedTest(algorithms: String) {
withContext(Dispatchers.IO)
{
val mResult = Vector<SpeedResult>()
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<SpeedResult>) {
}
override fun onCancelled(speedResults: Array<SpeedResult>) {
mCancel = true
}
}
companion object {
private var runTestAlgorithms: SpeeedTest? = null
}
}