You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jmeter.apache.org by GitBox <gi...@apache.org> on 2021/11/12 13:23:55 UTC

[GitHub] [jmeter] vlsi commented on a change in pull request #674: WIP: Precise Thread Group

vlsi commented on a change in pull request #674:
URL: https://github.com/apache/jmeter/pull/674#discussion_r748282567



##########
File path: src/core/src/main/kotlin/org/apache/jmeter/threads/precise/PreciseThreadGroup.kt
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.threads.precise
+
+import org.apache.jmeter.control.Controller
+import org.apache.jmeter.engine.StandardJMeterEngine
+import org.apache.jmeter.gui.GUIMenuSortOrder
+import org.apache.jmeter.testelement.property.TestElementProperty
+import org.apache.jmeter.threads.AbstractThreadGroup
+import org.apache.jmeter.threads.JMeterContextService
+import org.apache.jmeter.threads.JMeterThread
+import org.apache.jmeter.threads.JMeterThreadMonitor
+import org.apache.jmeter.threads.ListenerNotifier
+import org.apache.jmeter.threads.TestCompilerHelper
+import org.apache.jorphan.collections.ListedHashTree
+import org.slf4j.LoggerFactory
+import java.io.Serializable
+import java.lang.Thread.sleep
+import java.util.Random
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.ExecutorService
+import java.util.concurrent.Executors
+import java.util.concurrent.Future
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.atomic.AtomicReference
+import kotlin.math.roundToLong
+
+@GUIMenuSortOrder(1)
+class PreciseThreadGroup : AbstractThreadGroup(),
+    Serializable, Controller, JMeterThreadMonitor, TestCompilerHelper {
+    companion object {
+        private val log = LoggerFactory.getLogger(PreciseThreadGroup::class.java)
+        const val SCHEDULE = "PreciseThreadGroup.schedule"
+        const val RANDOM_SEED = "PreciseThreadGroup.random_seed"
+    }
+
+    private lateinit var executorService: ExecutorService
+
+    private val threadStarterFuture = AtomicReference<Future<*>?>()
+    private val activeThreads = ConcurrentHashMap<JMeterThread, Future<*>>()
+
+    var scheduleString: String
+        get() = getPropertyAsString(SCHEDULE)
+        set(value) {
+            setProperty(SCHEDULE, value)
+        }
+
+    val randomSeed: Long get() = getPropertyAsLong(RANDOM_SEED)
+
+    var randomSeedString: String
+        get() = getPropertyAsString(RANDOM_SEED)
+        set(value) {
+            setProperty(RANDOM_SEED, value)
+        }
+
+    init {
+        setProperty(TestElementProperty(MAIN_CONTROLLER, PreciseThreadGroupController()))
+    }
+
+    private class ThreadsStarter(
+        private val testStartTime: Long,
+        private val executorService: ExecutorService,
+        private val activeThreads: MutableMap<JMeterThread, Future<*>>,
+        private val gen: ThreadScheduleProcessGenerator,
+        private val jmeterThreadFactory: (threadNumber: Int) -> JMeterThread,
+    ) : Runnable {
+        override fun run() {
+            log.info("Thread starting init")
+            val endTime = (testStartTime + gen.totalDuration).roundToLong()
+            try {
+                var threadNumber = 0
+                while (gen.hasNext()) {
+                    val nextDelay = testStartTime + (gen.next() * 1000).roundToLong() - System.currentTimeMillis()
+                    if (nextDelay > 0) {
+                        sleep(nextDelay)
+                    }
+                    val jmeterThread = jmeterThreadFactory(threadNumber++)
+                    jmeterThread.endTime = endTime
+                    activeThreads[jmeterThread] = executorService.submit {
+                        Thread.currentThread().name = jmeterThread.threadName
+                        jmeterThread.run()
+                    }
+                }
+            } finally {
+                // No more actions will be scheduled, let awaitTermination to see the completion
+                executorService.shutdown()
+            }
+            log.info("Thread starting done")
+        }
+    }
+
+    override fun start(
+        threadGroupIndex: Int,
+        notifier: ListenerNotifier,
+        threadGroupTree: ListedHashTree,
+        engine: StandardJMeterEngine
+    ) {
+        try {
+            val jMeterContext = JMeterContextService.getContext()
+            val variables = jMeterContext.variables
+            val schedule = scheduleString
+            log.info("Starting PreciseThreadGroup#{} with schedule {}", threadGroupIndex, schedule)
+            val parsedSchedule = ThreadSchedule(schedule)
+            val seed = randomSeed
+            val rnd = if (seed == 0L) Random() else Random(seed)
+            val gen = ThreadScheduleProcessGenerator(rnd, parsedSchedule)
+            val testStartTime = JMeterContextService.getTestStartTime()
+            executorService = Executors.newCachedThreadPool()
+            val starter = ThreadsStarter(testStartTime, executorService, activeThreads, gen) { threadNumber ->
+                val clonedTree = cloneTree(threadGroupTree)
+                makeThread(engine, this, notifier, threadGroupIndex, threadNumber, clonedTree, variables)
+            }
+            threadStarterFuture.set(executorService.submit(starter))
+        } catch (e: Throwable) {
+            log.error("Unable to start thread group", e)
+        }
+    }
+
+
+    override fun threadFinished(thread: JMeterThread?) {
+        activeThreads.remove(thread)
+    }
+
+    override fun addNewThread(delay: Int, engine: StandardJMeterEngine?): JMeterThread {
+        TODO("Will not be implemented")
+    }
+
+    override fun stopThread(threadName: String?, now: Boolean): Boolean {
+        TODO("Will not be implemented")

Review comment:
       No impact. The method is not really used, and it should not exist in the abstract thread group in the first place




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jmeter.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org