pmouawad commented on a change in pull request #674: URL: https://github.com/apache/jmeter/pull/674#discussion_r748263252
########## 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: What will be the impact of this ? ########## File path: src/core/src/main/resources/org/apache/jmeter/resources/messages.properties ########## @@ -828,6 +828,10 @@ post_body_raw=Body Data post_files_upload=Files Upload post_thread_group_title=tearDown Thread Group previous=Previous +precisethreadgroup=Precise Thread Group Review comment: Rename to "Open Model Thread Group" ? ########## File path: xdocs/usermanual/component_reference.xml ########## @@ -5186,7 +5187,15 @@ Note that the throughput value should not be changed too often during a test </component> <component name="Precise Throughput Timer" index="§-num;.6.5" width="573" height="407" screenshot="timers/precise_throughput_timer.png"> -<description><p>This timer introduces variable pauses, calculated to keep the total throughput (e.g. in terms of samples per minute) as close as possible to a give figure. Of course the throughput will be lower if the server is not capable of handling it, or if other timers, or if there's not enough threads, or time-consuming test elements prevent it.</p> +<description><p>This timer introduces variable pauses, calculated to keep the total throughput (e.g. in terms of samples per minute) as close as possible to a give figure. The timer does not generate threads, so the resulting throughput will be lower if the server is not capable of handling it, or if other timers add too big delays, or if there's not enough threads, or time-consuming test elements prevent it.</p> Review comment: typo: given figure ########## File path: src/core/src/main/kotlin/org/apache/jmeter/threads/precise/ThreadScheduleProcessGenerator.kt ########## @@ -0,0 +1,81 @@ +/* + * 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.threads.precise.ThreadScheduleStep.ArrivalsStep +import org.apache.jmeter.threads.precise.ThreadScheduleStep.RateStep +import java.util.Random + +internal class ThreadScheduleProcessGenerator( Review comment: Would it be possible to add some javadoc explaining this class ? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
