2
2

More than 1 year has passed since last update.

LocustとseleniumをDockerで構築する

Last updated at Posted at 2022-07-08

はじめに

研究の実験としてLocustを使った負荷試験を行ったのですが、WordPressの決済プラグインであるStripeがREST APIに対応していませんでした。そのためseleniumで直接クレジットカードの番号などを入力する必要があるました。
今回はseleniumとlocustをDockerで実行する方法を記述していきます!

前提

  • Dockerが入っていること!
  • Docker Hubのアカウントを作っていること!

Dockerfile

FROM locustio/locust@sha256:78148a8b40d012f29d7f274a72e347552e373d740275b7e2f455e70e11c4e9b1
USER root

RUN apt-get update

RUN apt-get install -y vim less psmisc

RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN apt update
RUN apt install google-chrome-stable -y
RUN apt install openjdk-11-jdk -y

RUN wget https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.0.0-beta-4/selenium-server-4.0.0-beta-4.jar

RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install locust-plugins
RUN pip install webdriver-manager
RUN pip install chromedriver-binary-auto
RUN pip install Faker

ENV PATH $PATH:/usr/local/lib/python3.8/site-packages/chromedriver_binary
RUN echo $PATH

# java実行のために初期化
ENTRYPOINT [""]

今回必要なこととして,seleniumがpipでインストールするだけでなくselenium server本体が必要です.
seleniumが動かなかったので本当にわからなかったです(下記の記事が私を救ってくれました
https://qiita.com/itomaki_kokoro/items/1d06811c6c0799827d14

docker-compose.yaml

docker-compose.yaml
version: '3'

services:
  master:
    build:
      context: .
      dockerfile: Dockerfile
    tty: true
    container_name: locust_master
    ports:
      - "8089:8089"
    volumes:
      - ./:/mnt/locust
    command: >
      sh -c '
        java -jar selenium-server-4.0.0-beta-4.jar standalone &
        locust -f /mnt/locust/locustfile.py --master -H http://master:8089
      '

  worker:
    build:
      context: .
      dockerfile: Dockerfile
    tty: true
    container_name: locust_worker
    volumes:
      - ./:/mnt/locust
    command: >
      sh -c '
        java -jar selenium-server-4.0.0-beta-4.jar standalone &
        locust -f /mnt/locust/locustfile.py --worker --master-host master
      '

このコードは上記のqiita記事を丸コピさせていただきました...
私が書いたdocker-compose.yamlが動かなかったので 😇

locustfile.py

locustfileはlocust-pluginのexampleにあるコードを使わさせていただきました.
自分の環境に合わせて変更が必要です
https://github.com/SvenskaSpel/locust-plugins/blob/master/examples/webdriver_ex.py

実行

docker-compose up 

上記を実行すると
image.png

動きました!!!
改めてQiitaを書いてくれた先駆者の方に感謝です.
https://qiita.com/itomaki_kokoro/items/1d06811c6c0799827d14

おわりに

今回はDockerでseleniumとlocustを実行する方法を記述しました.
英語で調べても情報がなかったりしたのですが,Qiitaがあって本当に助かりました.本当にありがとうございます!!

私を救ってくれた参考記事

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2