Best Python code snippet using locust
app.py
Source:app.py
...152 def __init__(self, spawn_rate='', tot_users='', time=''):153 self.spawn_rate = spawn_rate154 self.tot_users = tot_users155 self.time = time156 def create_web_ui(self):157 env = Environment(user_classes=[ReqUrl])158 env.create_local_runner()159 env.create_web_ui(self.ip, self.port)160 webbrowser.open('http://localhost:8089/')161 gevent.spawn(stats_printer(env.stats))162 gevent.spawn(stats_history, env.runner)163 env.runner.start(int(self.tot_users), int(self.spawn_rate))164 gevent.spawn_later(int(self.time), lambda: env.runner.quit())165 env.runner.greenlet.join()166 env.web_ui.stop()167class MainGui(QWidget, Ui_Gui):168 def __init__(self):169 super(MainGui, self).__init__()170 self.setupUi(self)171 self.startButton.setStyleSheet('QPushButton{background-color: qlineargradient(spread:pad, x1:1, y1:0.074, '172 'x2:0, '173 'y2:0.966227, stop:0 rgba(0, 0, 0, 255), stop:0.9375 rgba(10, 29, 2, 255)); '174 'border-radius: 10px;padding: 5px;color: rgb(255, 255, 255);font: 12pt '175 '\"Courier\"; '176 'border-bottom: 2px outset black;}'177 'QPushButton:pressed{padding: 1px -1px -1px 1px;border-bottom: 0 inset black;}'178 'QPushButton:hover{background-color: #4000FF;}')179 self.setWindowTitle('Testes')180 self.insertCount.setPlaceholderText('Pico')181 # self.setWindowIcon(QIcon(os.path.join(os.getcwd(), 'teste.png')))182 def run_teste(self):183 req = ReqUrl184 req.url = self.insertUrl.text()185 run = RunLocust(spawn_rate=self.insertUsers.text(), tot_users=self.insertCount.text(),186 time=self.insertTime.text())187 run.create_web_ui()188 def start_operations(self):189 self.startButton.clicked.connect(self.run_teste)190if __name__ == '__main__':191 app = QApplication([])192 ui = MainGui()193 ui.start_operations()194 ui.show()195 sys.exit(app.exec_())196"""197# Setup the environment and runner198env = Environment(user_classes=[ReqUrl])199env.create_local_runner()200# Start web UI201env.create_web_ui('127.0.0.1', 8089)202webbrowser.open('http://localhost:8089/')203# start a greenlet that periodically outputs the current stats204# Greenlets são tipo micro processos que executam uma tarefa205gevent.spawn(stats_printer(env.stats)) # Gevent - são tipo corotinas que são funções que podem206# suspender sua execução antes de atingir um retorno207# Save the stats208gevent.spawn(stats_history, env.runner)209# Start the test210env.runner.start(1, spawn_rate=10)211# stop the runner in 60 seconds212gevent.spawn_later(60, lambda: env.runner.quit())213# Wait for the greelets214env.runner.greenlet.join()215# Stop the web server...
locust_as_library.py
Source:locust_as_library.py
...5setup_logging("INFO", None)6def create_env(user_class, ip_address="127.0.0.1"):7 env = Environment(user_classes=[user_class])8 env.create_local_runner()9 env.create_web_ui(ip_address, 8089)10 gevent.spawn(stats_printer(env.stats))11 gevent.spawn(stats_history, env.runner)12 return env13def start_test(env, user_count=1, spawn_rate=10, test_time=60):14 env.runner.start(user_count, spawn_rate=spawn_rate)15 gevent.spawn_later(test_time, lambda: env.runner.quit())16 env.runner.greenlet.join()...
main.py
Source:main.py
...4 # setup Environment and Runner5 env = Environment(user_classes=[MongoSampleUser])6 env.create_local_runner()7 # start a WebUI instance8 env.create_web_ui("127.0.0.1", 8089)9 # start the test10 env.runner.start(1, spawn_rate=1)11 # wait for the greenlets12 env.runner.greenlet.join()13 # stop the web server for good measures...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!