Jenkins

คู่มือนี้อธิบายวิธีผสาน LoadFocus JMeter API Client กับ Jenkins สำหรับการทดสอบประสิทธิภาพอัตโนมัติ

ขั้นตอนการตั้งค่า

1. จัดเก็บ Credentials ใน Jenkins

ก่อนอื่น จัดเก็บ LoadFocus API credentials อย่างปลอดภัยใน Jenkins:

  1. นำทางไปที่ Jenkins Dashboard > Manage Jenkins > Manage Credentials
  2. เลือก credential domain ที่เหมาะสม (เช่น global)
  3. คลิก "Add Credentials"
  4. เพิ่ม credentials ต่อไปนี้:
    • Kind: Secret text
    • Scope: Global
    • Secret: API key ของ LoadFocus
    • ID: loadfocus-api-key
    • Description: LoadFocus API Key
  5. ทำซ้ำสำหรับ team ID ด้วย ID: loadfocus-team-id

2. สร้าง Jenkins Pipeline

สร้าง Jenkinsfile ใน repository ของคุณ:

pipeline {
agent {
docker {
image 'node:16-alpine'
}
}
environment {
LOADFOCUS_API_KEY = credentials('loadfocus-api-key')
LOADFOCUS_TEAM_ID = credentials('loadfocus-team-id')
}
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Performance Test') {
steps {
sh 'npm install -g @loadfocus/loadfocus-api-client'
sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'
sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'
sh '''
loadfocus-api jmeter run-test \
--name "Jenkins_${JOB_NAME}_${BUILD_NUMBER}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
'''
archiveArtifacts artifacts: 'performance_results.json', fingerprint: true
}
}
stage('Deploy') {
when {
expression {
return currentBuild.resultIsBetterOrEqualTo('SUCCESS')
}
}
steps {
echo 'Deploying...'
}
}
}
post {
always {
cleanWs()
}
}
}

เคล็ดลับสำหรับการผสาน Jenkins

  1. การจัดการ Timeout: ตั้ง timeouts สำหรับทดสอบประสิทธิภาพที่ใช้เวลานาน
  2. การรันแบบมีเงื่อนไข: รันทดสอบประสิทธิภาพเฉพาะบน branches ที่กำหนด
  3. การทดสอบตามกำหนดเวลา: รันทดสอบประสิทธิภาพตามกำหนดเวลา
  4. ทดสอบแบบมีพารามิเตอร์: อนุญาตให้ปรับแต่งพารามิเตอร์ทดสอบ

สำหรับข้อมูลเพิ่มเติม ดู เอกสาร Jenkins และ เอกสาร LoadFocus API Client