Izvođenje JMeter testova u OpenShiftu pomoću Jenkinsovog cjevovoda

Pozdrav!

U ovom članku želim podijeliti jedan od načina za pokretanje JMeter testova performansi u OpenShiftu koristeći Jenkins kao automatizaciju. Prvo ćemo napraviti sve potrebne korake (izrada ImageStreams, BuildConfig, Job itd.) u ručnom načinu rada. Nakon toga, napišimo Jenkinsov cjevovod.

Kao početnu točku trebali bismo imati:

  1. pokrenut OpenShift (v3.11) klaster
  2. Jenkins poslužitelj s konfiguriranim vjerodajnicama za rad u OpenShiftu
  3. datoteku apache-jmeter-5.2.tgz

Kao testovi bit će jednostavno HTTP Request na ya.ru u jednom toku.

Stvaranje projekta u OpenShiftu

Počnimo stvaranjem novog okruženja. Kreirajmo perftest okružen timom:

$ oc new-project perftest --display-name="Performance Tests" --description="Performance Tests - JMeter"

Automatski ćemo biti prebačeni u novonastalo okruženje perftest, provjerimo je li tako:

$ oc project
Using project "perftest" on server "https://127.0.0.1:8443".

Stvaranje pohrane

Izvješća o ispitivanju bit će pohranjena zajedno s web poslužiteljem i jmeter-meter'mjesto - /jmeter/reports.

Bolje je sada kreirati Storaje jer će POD-ovi biti vezani za njih jmeter-web и jmeter-master.

Detaljnije informacije o skladištenju pronaći ćete u službenoj dokumentaciji Trajno skladištenje.

Kreirajmo yaml datoteke za PV и PVC.

pv.yaml

$ tee pv.yaml<<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jmeter-reports
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: glusterfs-cluster
    path: /jmeter/reports
    readOnly: false
  persistentVolumeReclaimPolicy: Retain
EOF

pvc.yaml

$ tee pvc.yaml<<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jmeter-reports
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
EOF

Kreirajmo PV и PVC okružen OpenShiftom:

$ oc create -f pv.yaml -n perftest
$ oc create -f pvc.yaml -n perftest

Provjera statusa za PVC:

$ oc get pvc -n perftest
NAME             STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS        AGE
jmeter-reports   Bound     pvc-b0e5f152-db4b-11ea-a497-566f75280024   10Gi       RWX            glusterfs-storage   8m

Evo kako će to izgledati u GUI-u:

Izvođenje JMeter testova u OpenShiftu pomoću Jenkinsovog cjevovoda

Stvaranje JMeter osnovne slike

Prijeđimo na stvaranje ImageStream и BuildConfig.

Sve potrebne podatke možete pronaći u dokumentaciji - Gradnje i tokovi slika.

Strategija izgradnje imidža je Docker iz lokalnog izvora.

Kreirajmo osnovnu sliku jmeter-base, koji će biti osnova za jmeter-master.

dockerfile

FROM openjdk:8u212-jdk

ARG JMETER_VER="5.2"
ENV JMETER_HOME /jmeter/apache-jmeter-$JMETER_VER
ENV PATH $JMETER_HOME/bin:$PATH

RUN mkdir -p /jmeter/results 
    && mkdir /jmeter/tests

WORKDIR /jmeter

COPY apache-jmeter-$JMETER_VER.tgz .

RUN tar -xzf $JMETER_HOME.tgz 
    && rm $JMETER_HOME.tgz 
    && ls -la

RUN sed -i s/#server.rmi.ssl.disable=false/server.rmi.ssl.disable=true/ $JMETER_HOME/bin/jmeter.properties

EXPOSE 60000

je.yaml

$ tee is.yaml<<EOF
apiVersion: v1
kind: ImageStream
metadata:
  labels:
    build: jmeter-base
  name: jmeter-base
EOF

bc.yaml

$ tee bc.yaml<<EOF
apiVersion: v1
kind: BuildConfig
metadata:
  name: jmeter-base
spec:
  failedBuildsHistoryLimit: 5
  nodeSelector: null
  output:
    to:
      kind: ImageStreamTag
      name: 'jmeter-base:latest'
  postCommit: {}
  resources: {}
  runPolicy: Serial
  source:
    binary: {}
    type: Binary
  strategy:
    dockerStrategy:
      from:
        kind: ImageStreamTag
        name: 'openjdk:8u212-jdk'
    type: Docker
  successfulBuildsHistoryLimit: 5
EOF

Kreirajmo objekte IS и BC:

$ oc create -f is.yaml -n perftest
$ oc create -f bc.yaml -n perftest

Sada sastavimo osnovnu sliku jmeter-base:

$ oc start-build jmeter-base -n perftest --from-dir=. --follow

JMeter WEB

jmeter-web Ovo je Apache web poslužitelj. Njegova je zadaća pružiti direktorij s rezultatima ispitivanja za pregled.

Pripremljeno Dockerfile i konfiguracijsku datoteku httpd.conf. Za direktivu DocumentRoot skup vrijednosti /jmeter/reports, tj. direktorij u koji se spremaju rezultati testa.

dockerfile

$ tee Dockerfile<<EOF
FROM httpd:2.4

COPY httpd.conf /usr/local/apache2/conf/httpd.conf
RUN chmod -R 777 /usr/local/apache2/logs

EXPOSE 8080

CMD ["httpd", "-D", "FOREGROUND"]
EOF

je.yaml

$ tee is.yaml<<EOF
apiVersion: v1
kind: ImageStream
metadata:
  generation: 1
  labels:
    build: jmeter-web
  name: jmeter-web
EOF

bc.yaml

$ tee bc.yaml<<EOF
apiVersion: v1
kind: BuildConfig
metadata:
  name: jmeter-web
spec:
  failedBuildsHistoryLimit: 5
  nodeSelector: null
  output:
    to:
      kind: ImageStreamTag
      name: 'jmeter-web:latest'
  runPolicy: Serial
  source:
    binary: {}
    type: Binary
  strategy:
    dockerStrategy:
      from:
        kind: ImageStreamTag
        name: 'httpd:2.4'
    type: Docker
  successfulBuildsHistoryLimit: 5
EOF

Kreirajmo ImageStream и BuildConfig objekti:

$ oc create -f is.yaml -n perftest
$ oc create -f bc.yaml -n perftest

Prikupljanje slike iz Dockerfile:

$ oc start-build jmeter-web -n perftest --from-dir=. --follow

dc.yaml

$ tee dc.yaml<<EOF
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: jmeter-web
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: jmeter-web
    spec:
      containers:
        - image: 172.30.1.1:5000/perftest/jmeter-web
          name: jmeter-web
          volumeMounts:
            - mountPath: /jmeter/reports
              name: jmeter-reports
          ports:
            - containerPort: 80
              protocol: TCP
            - containerPort: 8080
              protocol: TCP
      volumes:
        - name: jmeter-reports
          persistentVolumeClaim:
            claimName: jmeter-reports
EOF

sc.yaml

$ tee sc.yaml<<EOF
apiVersion: v1
kind: Service
metadata:
  labels:
    app: jmeter-web
  name: jmeter-web
spec:
  ports:
    - name: 8080-tcp
      port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    deploymentconfig: jmeter-web
  sessionAffinity: None
  type: ClusterIP
EOF

Kreirajmo objekte Service и DeploymentConfig:

$ oc create -f sc.yaml -n perftest
$ oc create -f dc.yaml -n perftest

Jmeter-majstor

Počnimo s implementacijom Apache web poslužitelja.

Ovo je Dockerfile jmeter-master'a, na temelju jmeter-base, koji će pokrenuti testove i spremiti rezultate u pohranu.

dockerfile

Dockerfile za jmeter-master, na temelju jmeter-base.

FROM jmeter-base

ARG JMETER_VER="5.2"
ENV JMETER_HOME /jmeter/apache-jmeter-$JMETER_VER
ENV PATH $JMETER_HOME/bin:$PATH

WORKDIR /jmeter
COPY run.sh /jmeter/
COPY tests/*.jmx /jmeter/tests/
RUN chmod +x /jmeter/run.sh

ENTRYPOINT ["/bin/bash"]
CMD ["/jmeter/run.sh"]

trči.sh

run.sh ovo je skripta koja pokreće JMeter i sprema rezultate u direktorij files.

Svaki put kada se skripta pokrene, ona briše prethodne testove, tako da možete raditi samo s najnovijim podacima. Ali to nije problem, jer ga možete promijeniti prema svojim potrebama.

#!/bin/bash

set -e

if [ -d "/jmeter/reports/files" ]
then
    echo "Directory /jmeter/reports/files exist - OK"
else
    echo "Creating /jmeter/reports/files directory"
    mkdir /jmeter/reports/files
fi

if [ -d "/jmeter/reports/dashboards" ]
then
    echo "Directory /jmeter/reports/dashboards exist"
else
    echo "Creating /jmeter/reports/dashboards directory"
    mkdir /jmeter/reports/dashboards
fi

echo "*** JMeter START Tests ***"

for item in $(ls -1 /jmeter/tests | grep jmx)
do
    echo "*** Removing dashboard directory for $item"
    rm -rdf /jmeter/reports/dashboards/${item}*

    echo "*** Removing tests directory for $item"
    rm -rdf /jmeter/reports/files/${item}*

    echo "*** Testing a $item file ***"
    jmeter -n -t /jmeter/tests/${item} -l /jmeter/reports/files/${item}-report.jtl -e -o /jmeter/reports/dashboards/${item}-dash
done

je.yaml

$ tee is.yaml<<EOF
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
  generation: 1
  labels:
    build: jmeter-master
  name: jmeter-master
EOF

bc.yaml

$ tee bc.yaml<<EOF
apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  name: jmeter-master
spec:
  failedBuildsHistoryLimit: 5
  nodeSelector: null
  output:
    to:
      kind: ImageStreamTag
      name: 'jmeter-master:latest'
  runPolicy: Serial
  source:
    binary: {}
    type: Binary
  strategy:
    dockerStrategy:
      from:
        kind: ImageStreamTag
        name: 'jmeter-base:latest'
    type: Docker
  successfulBuildsHistoryLimit: 5
EOF

Kreirajmo IS и BC objekti:

$ oc create -f is.yaml -n perftest
$ oc create -f bc.yaml -n perftest

Sakupljamo jmeter-master slika:

$ oc start-build jmeter-master -n perftest --from-dir=. --follow

Posao

Jobkoriste se u OpenShift'e kako biste pokrenuli jedan ili više PODi jamče njihov uspješan završetak nakon izvršavanja naredbe/skripte.

$ tee job.yaml<<EOF
apiVersion: batch/v1
kind: Job
metadata:
  name: jmeter-master
  labels:
    jobName: jmeter-master
spec:
  completions: 1
  parallelism: 1
  template:
    metadata:
      name: jmeter-master
      labels:
        jobName: jmeter-master
    spec:
      containers:
        - name: jmeter-master
          image: 172.30.1.1:5000/perftest/jmeter-master:latest
          volumeMounts:
            - mountPath: /jmeter/reports
              name: jmeter-reports
          imagePullPolicy: Always
      volumes:
        - name: jmeter-reports
          persistentVolumeClaim:
            claimName: jmeter-reports
      restartPolicy: Never
      terminationGracePeriodSeconds: 30
EOF

Stvorite objekt Job:

$ oc create -f job.yaml -n perftest

Provjerimo status posla:

$ oc get jobs -n perftest
NAME            DESIRED   SUCCESSFUL   AGE
jmeter-master   1         1            5m

Izbrisati Job upotrijebimo naredbu:

$ oc delete jobs/jmeter-master -n perftest --ignore-not-found=true

Jenkinsov cjevovod

Sada automatizacija. Prođimo ponovno kroz korake:

  1. git clone
  2. oc whoami -t
  3. oc start-build ...
  4. oc delete jobs/jmeter-master
  5. oc create -f job.yaml -n perftest

Ispod je cjevovod u kojem se izvode kloniranje, brisanje i stvaranje OpenShift spremišta Job's.

#!groovy

pipeline {

    agent any

    stages {

        stage('Start Notifications') {
            steps {
                echo "Sending Email Notification"
            }
            post {
                always {
                    echo "STARTED - Performance Tests"
                    mail(to: '[email protected]', from: "[email protected]", subject: "START - Performance Tests",mimeType: "text/html", body: "<strong>START - Performance Tests</strong><br /><br />Project: Name of Project<br />Environment: PerfTest<br />Build number: ${env.BUILD_NUMBER}<br />Build URL:   ${env.BUILD_URL}"
                }
            }
        }

        stage('Git checkout') {
            steps {
                ...
            }
        }

        stage('Perf Tests') {
            steps {
                script {
                    sh '''
                        OC_CMD1="oc login -u=username -p=PASS -n=perftest 
                        --server=https://...:8443"

                        $OC_CMD1

                        OC_TOKEN=`oc whoami -t`

                        OC_CMD2="oc --token=$OC_TOKEN --server=https://...:8443 
                        start-build jmeter-master -n=perftest --from-dir=./master 
                        --follow=true"

                        OC_CMD3="oc --token=$OC_TOKEN --server=https://...:8443 
                        delete jobs/jmeter-master -n=perftest --ignore-not-found=true"

                        OC_CMD4="oc--token=$OC_TOKEN --server=https://...:8443 
                        create -f ./master/job.yaml -n=perftest"

                        $OC_CMD2
                        $OC_CMD3
                        $OC_CMD4
                    '''
                }
            }
        }

        post {
            failure {
                echo "FAILED - Performance Tests"
                mail(to: '[email protected]', from: "[email protected]", subject: "FAILED - Performance Tests",mimeType: "text/html", body: "<strong>FAILED - Performance Tests</strong><br /><br />Project: Name of Project<br />Environment: PerfTest<br />Build number: ${env.BUILD_NUMBER}<br />Build URL: ${env.BUILD_URL}"
                }

            success {
                echo "SUCCESSED - Performance Tests"
                mail(to: '[email protected]', from: "[email protected]", subject: "SUCCESSED - Performance Tests",mimeType: "text/html", body: "<strong>SUCCESSED - Performance Tests</strong><br /><br />Project: Name of Project<br />Environment: PerfTest<br />Build number: ${env.BUILD_NUMBER}<br />Build URL:   ${env.BUILD_URL}"
            }
        }

    }
}

Nakon što Pipeline završi s radom, primit ćemo obavijest e-poštom '[email protected] iz [email protected].

Klikom na vezu http://jmeter-web.127.0.0.1.nip.io/ vidjet ćemo imenik files, koji pohranjuje izvješća o ispitivanju:

Izvođenje JMeter testova u OpenShiftu pomoću Jenkinsovog cjevovoda

Sadržaj datoteke ya.HTTP.Request.jmx-report.jtk:

timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
1597311456443,569,Yandex - HTTP Request,200,Ok,Thread Group 1-1,text,true,,59449,220,1,1,https://ya.ru/,145,0,57
1597311456443,147,Yandex - HTTP Request-0,302,Found,Thread Group 1-1,,true,,478,110,1,1,http://ya.ru/,145,0,57
1597311456592,420,Yandex - HTTP Request-1,200,Ok,Thread Group 1-1,text,true,,58971,110,1,1,https://ya.ru/,370,0,259

Zaključak

Ovaj članak demonstrira jednu od opcija za izvođenje JMeter testova u okruženju OpenShift. Sve korake izvršili smo ručno, nakon čega smo kreirali Jenkins Pipeline za automatizaciju procesa izvođenja testova.

Izvori i dokumentacija

Izvor: www.habr.com

Dodajte komentar