Skip to content

Commit

Permalink
feat(plugin-apipost): support delay option
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Nov 18, 2024
1 parent 591f5fd commit 56867f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion plugin-apipost/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "me.leon.toolsfx"
version = "1.9.0"
version = "1.9.1"

plugins {
`java-library`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import tornadofx.*
private const val MAX_SHOW_LENGTH = 1_000_000

class ApiPostView : PluginFragment("ApiPost") {
override val version = "v1.9.0"
override val date: String = "2024-11-05"
override val version = "v1.9.1"
override val date: String = "2024-11-18"
override val author = "Leon406"
override val description = "ApiPost"

Expand All @@ -41,6 +41,7 @@ class ApiPostView : PluginFragment("ApiPost") {
private lateinit var table: TableView<HttpParams>
private var tfRepeatNum: TextField by singleAssign()
private var tfConcurrent: TextField by singleAssign()
private var tfDelay: TextField by singleAssign()
private val prettyProperty = SimpleBooleanProperty(true)
private val showJsonPath = SimpleBooleanProperty(false)
private val methods =
Expand Down Expand Up @@ -157,6 +158,11 @@ class ApiPostView : PluginFragment("ApiPost") {
prefWidth = DEFAULT_SPACING_10X
textFormatter = intTextFormatter
}
tfDelay = textfield {
promptText = "delay"
prefWidth = DEFAULT_SPACING_10X
textFormatter = intTextFormatter
}
}

hbox {
Expand Down Expand Up @@ -302,6 +308,7 @@ class ApiPostView : PluginFragment("ApiPost") {
running.value = true
val count = runCatching { tfRepeatNum.text.toInt() }.getOrDefault(1)
val concurrent = runCatching { tfConcurrent.text.toInt() }.getOrDefault(1)
val delayMillis = runCatching { tfDelay.text.toLong() }.getOrDefault(0L)
if (selectedBodyType.get() == BodyType.FORM_DATA.type) {
reqHeaders["Content-Type"] = HttpUrlUtil.APPLICATION_URL_ENCODE
}
Expand All @@ -311,6 +318,7 @@ class ApiPostView : PluginFragment("ApiPost") {
runAsync {
val start = System.currentTimeMillis()
var success = 0
val countMap: MutableMap<String, Int> = mutableMapOf()
fun req() =
if (selectedMethod.get() == "POST") {
val bodyType = bodyTypeMap[selectedBodyType.get()]
Expand Down Expand Up @@ -347,18 +355,37 @@ class ApiPostView : PluginFragment("ApiPost") {
.also {
if (it.code == 200) {
success++
countMap[it.data] = countMap[it.data]?.let { it + 1 } ?: 1
}
}
runCatching {
runBlocking { (1..count).map { async(dispatcher) { req() } }.awaitAll().last() }
runBlocking {
(1..count)
.map {
async(dispatcher) {
req().also {
if (delayMillis > 0) {
// delay 无法阻塞其他
Thread.sleep(delayMillis)
}
}
}
}
.awaitAll()
.last()
}
}
.onSuccess {
handleSuccess(it)
if (count > 1) {
ui {
primaryStage.showToast(
" time costs : ${System.currentTimeMillis() - start} ms" +
"\nsuccess/total: $success/$count",
"\nsuccess/total: $success/$count" +
"\n detail :\n${
countMap.map { "\t\tresp len: ${it.key.length} num: ${it.value}" }
.joinToString(System.lineSeparator())
}",
3000
)
}
Expand Down

0 comments on commit 56867f7

Please sign in to comment.