|
| 1 | +package com.atlassian.performance.tools.jiraactions.api.scenario |
| 2 | + |
| 3 | +import com.atlassian.performance.tools.jiraactions.api.SeededRandom |
| 4 | +import com.atlassian.performance.tools.jiraactions.api.WebJira |
| 5 | +import com.atlassian.performance.tools.jiraactions.api.action.Action |
| 6 | +import com.atlassian.performance.tools.jiraactions.api.action.BrowseProjectsAction |
| 7 | +import com.atlassian.performance.tools.jiraactions.api.action.CreateIssueAction |
| 8 | +import com.atlassian.performance.tools.jiraactions.api.action.SearchJqlAction |
| 9 | +import com.atlassian.performance.tools.jiraactions.api.measure.ActionMeter |
| 10 | +import com.atlassian.performance.tools.jiraactions.api.memories.IssueKeyMemory |
| 11 | +import com.atlassian.performance.tools.jiraactions.api.memories.JqlMemory |
| 12 | +import com.atlassian.performance.tools.jiraactions.api.page.IssuePage |
| 13 | +import kotlin.reflect.KClass |
| 14 | + |
| 15 | +class ActionShuffler { |
| 16 | + private class FixedJqlMemory(val jql: String) : JqlMemory { |
| 17 | + override fun observe(issuePage: IssuePage) { |
| 18 | + throw UnsupportedOperationException() |
| 19 | + } |
| 20 | + |
| 21 | + override fun recall(): String? { |
| 22 | + return jql |
| 23 | + } |
| 24 | + |
| 25 | + override fun remember(memories: Collection<String>) { |
| 26 | + throw UnsupportedOperationException() |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + companion object { |
| 31 | + fun createRandomisedScenario(seededRandom: SeededRandom, actionProportions: Map<Action, Int>, |
| 32 | + issueKeyMemoriser: Action): List<Action> { |
| 33 | + //createIssue needs a project - browserProject goes first |
| 34 | + //viewIssue needs to have issues - createIssues goes second |
| 35 | + return createRandomisedScenario(seededRandom, actionProportions, issueKeyMemoriser, BrowseProjectsAction::class, CreateIssueAction::class) |
| 36 | + } |
| 37 | + |
| 38 | + fun findIssueKeysWithJql(jira: WebJira, meter: ActionMeter, issueKeyMemory: IssueKeyMemory): SearchJqlAction { |
| 39 | + return SearchJqlAction( |
| 40 | + jira = jira, |
| 41 | + meter = meter, |
| 42 | + jqlMemory = FixedJqlMemory("project is not EMPTY"), |
| 43 | + issueKeyMemory = issueKeyMemory |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + private fun createRandomisedScenario(seededRandom: SeededRandom, actionProportions: Map<Action, Int>, |
| 48 | + issueKeyDiscoverer: Action, vararg actions: KClass<out Action>): List<Action> { |
| 49 | + val initialActions = findActions(actionProportions, *actions) |
| 50 | + |
| 51 | + val scenario: MutableList<Action> = mutableListOf() |
| 52 | + |
| 53 | + val actionProportionsToRandomise = deductActionCount(actionProportions, initialActions) |
| 54 | + actionProportionsToRandomise.entries.forEach { scenario.addMultiple(element = it.key, repeats = it.value) } |
| 55 | + scenario.shuffle(seededRandom.random) |
| 56 | + |
| 57 | + //viewIssue needs to remember isssues - issueKeyDiscoverer goes after all actions |
| 58 | + scenario.addAll(0, initialActions.plus(issueKeyDiscoverer)) |
| 59 | + return scenario |
| 60 | + } |
| 61 | + |
| 62 | + private fun findActions(actionProportions: Map<Action, Int>, vararg actions: KClass<out Action>): List<Action> { |
| 63 | + return actions |
| 64 | + .mapNotNull { findAction(actionProportions, it) } |
| 65 | + } |
| 66 | + |
| 67 | + private fun deductActionCount(actionProportions: Map<Action, Int>, actions: List<Action>): MutableMap<Action, Int> { |
| 68 | + val modifiedActionProportions = actionProportions.toMutableMap() |
| 69 | + |
| 70 | + actions.forEach { |
| 71 | + val originalCount = actionProportions.getValue(it) |
| 72 | + modifiedActionProportions[it] = originalCount-1 |
| 73 | + } |
| 74 | + return modifiedActionProportions |
| 75 | + } |
| 76 | + |
| 77 | + private fun <T : Action> findAction(actionProportions: Map<Action, Int>, kClass: KClass<T>): T? { |
| 78 | + return actionProportions.keys.find { kClass.isInstance(it) } as T? |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments