<commit_before>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * Licensed 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 io.gatling.core.scenario

import scala.concurrent.duration.{ Duration, FiniteDuration }

import io.gatling.core.assertion.{ Assertion, Metric }
import io.gatling.core.config.{ Protocol, Protocols }
import io.gatling.core.controller.Timings
import io.gatling.core.controller.throttle.{ ThrottlingBuilder, ThrottlingProtocol }
import io.gatling.core.pause.{ Constant, Custom, Disabled, Exponential, PauseProtocol, PauseType, UniformDuration, UniformPercentage }
import io.gatling.core.session.Expression
import io.gatling.core.structure.PopulatedScenarioBuilder

abstract class Simulation {

	private[core] var _scenarios = Seq.empty[PopulatedScenarioBuilder]
	private[core] var _globalProtocols = Protocols()
	private[core] var _assertions = Seq.empty[Assertion]
	private[core] var _maxDuration: Option[FiniteDuration] = None
	private[core] var _globalThrottling: Option[ThrottlingProtocol] = None
	private[core] var _beforeSteps: List[() => Unit] = Nil
	private[core] var _afterSteps: List[() => Unit] = Nil

	def scenarios: Seq[Scenario] = {
		require(!_scenarios.isEmpty, "No scenario set up")
		_scenarios.foreach(scn => require(!scn.scenarioBuilder.actionBuilders.isEmpty, s"Scenario ${scn.scenarioBuilder.name} is empty"))
		_scenarios.map(_.build(_globalProtocols))
	}

	def assertions = _assertions
	def timings = {
		val perScenarioThrottlings: Map[String, ThrottlingProtocol] = _scenarios
			.map(scn => scn
				.populationProtocols.getProtocol[ThrottlingProtocol]
				.map(throttling => scn.scenarioBuilder.name -> throttling)).flatten.toMap
		Timings(_maxDuration, _globalThrottling, perScenarioThrottlings)
	}

	def before(step: => Unit) {
		_beforeSteps = _beforeSteps ::: List(() => step)
	}

	def setUp(scenarios: PopulatedScenarioBuilder*) = {
		_scenarios = scenarios.toList
		new SetUp
	}

	def after(step: => Unit) {
		_afterSteps = _afterSteps ::: List(() => step)
	}

	class SetUp {

		def protocols(ps: Protocol*) = {
			_globalProtocols = _globalProtocols ++ ps
			this
		}

		def assertions(metrics: Metric[_]*) = {
			_assertions = metrics.flatMap(_.assertions)
			this
		}
<commit_msg>Enforce setUp to be called only once<commit_after>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * Licensed 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 io.gatling.core.scenario

import scala.concurrent.duration.{ Duration, FiniteDuration }

import io.gatling.core.assertion.{ Assertion, Metric }
import io.gatling.core.config.{ Protocol, Protocols }
import io.gatling.core.controller.Timings
import io.gatling.core.controller.throttle.{ ThrottlingBuilder, ThrottlingProtocol }
import io.gatling.core.pause.{ Constant, Custom, Disabled, Exponential, PauseProtocol, PauseType, UniformDuration, UniformPercentage }
import io.gatling.core.session.Expression
import io.gatling.core.structure.PopulatedScenarioBuilder

abstract class Simulation {

	private[core] var _scenarios: List[PopulatedScenarioBuilder] = Nil
	private[core] var _globalProtocols = Protocols()
	private[core] var _assertions = Seq.empty[Assertion]
	private[core] var _maxDuration: Option[FiniteDuration] = None
	private[core] var _globalThrottling: Option[ThrottlingProtocol] = None
	private[core] var _beforeSteps: List[() => Unit] = Nil
	private[core] var _afterSteps: List[() => Unit] = Nil

	def scenarios: List[Scenario] = {
		require(!_scenarios.isEmpty, "No scenario set up")
		_scenarios.foreach(scn => require(!scn.scenarioBuilder.actionBuilders.isEmpty, s"Scenario ${scn.scenarioBuilder.name} is empty"))
		_scenarios.map(_.build(_globalProtocols))
	}

	def assertions = _assertions
	def timings = {
		val perScenarioThrottlings: Map[String, ThrottlingProtocol] = _scenarios
			.map(scn => scn
				.populationProtocols.getProtocol[ThrottlingProtocol]
				.map(throttling => scn.scenarioBuilder.name -> throttling)).flatten.toMap
		Timings(_maxDuration, _globalThrottling, perScenarioThrottlings)
	}

	def before(step: => Unit) {
		_beforeSteps = _beforeSteps ::: List(() => step)
	}

	def setUp(scenarios: PopulatedScenarioBuilder*) = {
		if (!_scenarios.isEmpty)
			throw new UnsupportedOperationException("setUp can only be called once")
		_scenarios = scenarios.toList
		new SetUp
	}

	def after(step: => Unit) {
		_afterSteps = _afterSteps ::: List(() => step)
	}

	class SetUp {

		def protocols(ps: Protocol*) = {
			_globalProtocols = _globalProtocols ++ ps
			this
		}

		def assertions(metrics: Metric[_]*) = {
			_assertions = metrics.flatMap(_.assertions)
			this
		}

{'fraction_non_alphanumeric': 0.10517090271691498, 'fraction_numerical': 0.004206836108676599, 'mean_word_length': 4.987408184679958, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5452596', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1786, 'n_words': 471}