性能优化 #

一、性能优化概述 #

text
┌─────────────────────────────────────────────────────────────┐
│                    性能优化策略                              │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  并行执行                    缓存优化                        │
│  ┌─────────────────┐        ┌─────────────────┐            │
│  │ • needs依赖     │        │ • 依赖缓存      │            │
│  │ • 并行Job       │        │ • 编译缓存      │            │
│  │ • 矩阵构建      │        │ • 分布式缓存    │            │
│  └─────────────────┘        └─────────────────┘            │
│                                                              │
│  构建优化                    资源管理                        │
│  ┌─────────────────┐        ┌─────────────────┐            │
│  │ • 镜像优化      │        │ • Runner配置    │            │
│  │ • 增量构建      │        │ • 并发控制      │            │
│  │ • 多阶段构建    │        │ • 超时设置      │            │
│  └─────────────────┘        └─────────────────┘            │
│                                                              │
└─────────────────────────────────────────────────────────────┘

二、并行执行 #

1. needs依赖(DAG) #

传统顺序执行:

yaml
stages:
  - build
  - test
  - deploy

build:
  stage: build
  script: npm run build

test:
  stage: test
  script: npm test

deploy:
  stage: deploy
  script: echo "Deploying..."

使用needs优化:

yaml
stages:
  - build
  - test
  - deploy

build_a:
  stage: build
  script: npm run build:a

build_b:
  stage: build
  script: npm run build:b

test_a:
  stage: test
  needs: [build_a]
  script: npm run test:a

test_b:
  stage: test
  needs: [build_b]
  script: npm run test:b

deploy:
  stage: deploy
  needs: [test_a, test_b]
  script: echo "Deploying..."

2. 并行Job #

yaml
test:
  parallel: 5
  script:
    - echo "Running test $CI_NODE_INDEX of $CI_NODE_TOTAL"

3. 矩阵并行 #

yaml
test:
  parallel:
    matrix:
      - NODE_VERSION: [16, 18, 20]
        OS: [linux, windows, macos]
  image: node:${NODE_VERSION}
  tags:
    - ${OS}
  script:
    - npm test

4. 并行服务测试 #

yaml
test_services:
  parallel:
    matrix:
      - SERVICE: [api, web, worker]
  script:
    - cd services/$SERVICE
    - npm test

三、缓存优化 #

1. 依赖缓存 #

yaml
cache:
  key:
    files:
      - package-lock.json
  paths:
    - node_modules/

install:
  script:
    - npm ci

2. 多级缓存 #

yaml
cache:
  - key: npm-${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  - key: npm-global
    paths:
      - .npm/

3. 缓存策略 #

yaml
install:
  stage: install
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: push
  script:
    - npm ci

test:
  stage: test
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: pull
  script:
    - npm test

4. 分布式缓存 #

toml
[[runners]]
  [runners.cache]
    Type = "s3"
    Path = "runner-cache"
    Shared = true
    [runners.cache.s3]
      ServerAddress = "s3.amazonaws.com"
      BucketName = "gitlab-runner-cache"

四、构建优化 #

1. 镜像优化 #

使用精简镜像:

yaml
job_name:
  image: node:18-alpine
  script:
    - npm test

2. 多阶段构建 #

Dockerfile:

dockerfile
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

3. 增量构建 #

yaml
variables:
  DOCKER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

build:
  script:
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest -t $DOCKER_IMAGE .
    - docker push $DOCKER_IMAGE

4. 并行构建 #

yaml
build:
  parallel:
    matrix:
      - ARCH: [amd64, arm64]
  script:
    - docker build --platform linux/$ARCH -t $CI_REGISTRY_IMAGE:$ARCH .
    - docker push $CI_REGISTRY_IMAGE:$ARCH

manifest:
  needs: [build]
  script:
    - docker manifest create $CI_REGISTRY_IMAGE:latest \
        $CI_REGISTRY_IMAGE:amd64 \
        $CI_REGISTRY_IMAGE:arm64
    - docker manifest push $CI_REGISTRY_IMAGE:latest

五、资源管理 #

1. Runner并发 #

toml
concurrent = 10

[[runners]]
  limit = 5
  request_concurrency = 3

2. Job超时 #

yaml
long_job:
  script:
    - ./long-script.sh
  timeout: 2h

3. 资源限制 #

yaml
job_name:
  tags:
    - docker
  variables:
    KUBERNETES_CPU_LIMIT: "1"
    KUBERNETES_MEMORY_LIMIT: "2Gi"
  script:
    - npm test

4. 超时控制 #

yaml
variables:
  FF_USE_NEW_BASH_EVAL_STRATEGY: "true"

job_name:
  script:
    - npm test
  timeout: 30m

六、流水线优化 #

1. 减少Stage数量 #

优化前:

yaml
stages:
  - lint
  - build
  - test-unit
  - test-integration
  - test-e2e
  - deploy

优化后:

yaml
stages:
  - test
  - build
  - deploy

lint:
  stage: test
  script: npm run lint

test-unit:
  stage: test
  script: npm run test:unit

test-integration:
  stage: test
  script: npm run test:integration

test-e2e:
  stage: test
  script: npm run test:e2e

2. 条件执行 #

yaml
test:
  rules:
    - changes:
        - src/**/*
        - test/**/*
  script:
    - npm test

3. 快速失败 #

yaml
lint:
  script: npm run lint
  allow_failure: false

test:
  script: npm test
  needs: [lint]

4. 跳过不必要Job #

yaml
deploy:
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      changes:
        - src/**/*
  script:
    - echo "Deploying..."

七、优化技巧 #

1. 使用alpine镜像 #

yaml
job_name:
  image: node:18-alpine
  script:
    - npm test

2. 减少artifacts大小 #

yaml
build:
  artifacts:
    paths:
      - dist/
    exclude:
      - dist/*.map
      - dist/*.log
    expire_in: 1 week

3. 使用before_script优化 #

yaml
default:
  before_script:
    - npm ci --prefer-offline

test:
  script:
    - npm test

build:
  script:
    - npm run build

4. 合并Job #

优化前:

yaml
lint:
  script: npm run lint

test:
  script: npm test

优化后:

yaml
test:
  script:
    - npm run lint
    - npm test

八、性能监控 #

1. 流水线时长 #

Settings → CI/CD → General pipelines:

  • 查看平均流水线时长
  • 识别慢Job

2. Runner监控 #

toml
listen_address = ":9252"

3. Job日志分析 #

yaml
job_name:
  script:
    - echo "Start time: $(date)"
    - npm test
    - echo "End time: $(date)"

九、优化示例 #

优化前 #

yaml
stages:
  - install
  - lint
  - build
  - test-unit
  - test-integration
  - test-e2e
  - deploy

install:
  stage: install
  image: node:18
  script:
    - npm install

lint:
  stage: lint
  image: node:18
  script:
    - npm install
    - npm run lint

build:
  stage: build
  image: node:18
  script:
    - npm install
    - npm run build

test-unit:
  stage: test-unit
  image: node:18
  script:
    - npm install
    - npm run test:unit

test-integration:
  stage: test-integration
  image: node:18
  script:
    - npm install
    - npm run test:integration

test-e2e:
  stage: test-e2e
  image: node:18
  script:
    - npm install
    - npm run test:e2e

deploy:
  stage: deploy
  image: alpine:latest
  script:
    - echo "Deploying..."

优化后 #

yaml
stages:
  - test
  - build
  - deploy

cache:
  key:
    files:
      - package-lock.json
  paths:
    - node_modules/

.node_template:
  image: node:18-alpine
  before_script:
    - npm ci --prefer-offline

lint:
  extends: .node_template
  stage: test
  script:
    - npm run lint

test-unit:
  extends: .node_template
  stage: test
  script:
    - npm run test:unit

test-integration:
  extends: .node_template
  stage: test
  script:
    - npm run test:integration

test-e2e:
  extends: .node_template
  stage: test
  script:
    - npm run test:e2e

build:
  extends: .node_template
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

deploy:
  stage: deploy
  image: alpine:latest
  script:
    - echo "Deploying..."
  only:
    - main

十、最佳实践 #

1. 使用needs实现DAG #

yaml
test:
  needs: [build]

2. 合理使用缓存 #

yaml
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

3. 使用精简镜像 #

yaml
image: node:18-alpine

4. 并行执行测试 #

yaml
test:
  parallel: 5

5. 条件执行Job #

yaml
deploy:
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

下一步 #

现在你已经掌握了性能优化,接下来让我们学习 Node.js项目CI/CD

最后更新:2026-03-28