mirror of
https://github.com/schwabe/ics-openvpn.git
synced 2024-09-20 03:52:27 +02:00
Use coroutines instead AsyncTask in OpenSSL Speed test
This commit is contained in:
parent
ca5473055a
commit
ab67684d37
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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,13 +15,14 @@ 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
|
||||
@ -30,7 +30,8 @@ class OpenSSLSpeed : BaseActivity() {
|
||||
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 {
|
||||
@ -54,14 +55,19 @@ class OpenSSLSpeed : BaseActivity() {
|
||||
view.findViewById(R.id.ciphername),
|
||||
view.findViewById(R.id.speed),
|
||||
view.findViewById(R.id.blocksize),
|
||||
view.findViewById(R.id.blocksintime))
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user