跳转至

部署和维护 / 基准性能测试

本文主要介绍如何对 DataFlux Func 进行基准性能测试。

1. 前言

本文中的所有示例结果均在以下硬件环境中测试所得:

说明
计算机 HP ProBook 笔记本电脑
CPU AMD Ryzen 5 7530U with Radeon Graphics / 4.5 GHz
内存 32 GB / 3200 MHz (0.3 ns)
操作系统 Ubuntu 22.04.5 LTS

2. Benchmark 测试包

下载 Benchmark.zip,并导入 DataFlux Func

Benchmark 测试包包含如下内容:

内容 说明
脚本集 benchmark
函数 benchmark__main.hello_world 直接返回 "ok" 的空函数,用于并发测试
函数 benchmark__main.json_dump 进行 JSON 的序列化 / 反序列化,用于性能测试
函数 benchmark__main.calc_pi 计算圆周率,用于性能测试
同步 API benchmark-hello-world 用于并发测试
定时任务 benchmark-hello-world 用于测试任务调度性能
定时任务 benchmark-compute-pi 用于测试计算性能(计算圆周率)
定时任务 benchmark-json-dump-and-load 用于测试计算性能(JSON 的序列化 / 反序列化)

3. 执行测试

导入 Benchmark 测试包后,由于定时任务由于自动运行,稍等几分钟即可从任务记录中查看结果

而 DataFlux Func 的并发性能测试则需要借助其他工具进行

3.1 测试任务调度性能

导入 Benchmark 测试包后稍等几分钟,即可在「定时任务」中的「Hello, World」任务记录查看结果

一般来说,每次任务耗时都应该在 10 毫秒以内

cron-job-hello-world.png

3.2 测试计算性能

导入 Benchmark 测试包后稍等几分钟,即可在「定时任务」中的「Compute pi」和「JSON dump and load」任务记录查看结果

就「前言」中的测试环境来说,每次任务耗时都在 1 秒左右:

  • 计算圆周率「Compute pi」

cron-job-compute-pi.png

  • JSON 的序列化 / 反序列化「JSON dump and load」

cron-job-json-dump-and-load.png

3.3 测试函数 API 并发性能

测试并发性能时,可以使用 ab (ApacheBench)工具进行测试

如尚未安装 ab,可以使用如下命令安装:

Bash
1
apt-get install apache2-utils
Bash
1
yum install httpd-tools

使用 ab 对测试包中的「Hello, World」进行调用测试,命令如下:

Bash
1
ab -c 10 -n 5000 -k http://localhost:8088/api/v1/sync/benchmark-hello-world
Bash
1
ab -c 10 -n 5000 -k http://{DataFlux Func IP 或域名}:8088/api/v1/sync/benchmark-hello-world

就「前言」中的测试环境来说,空函数的并发在 1000 左右(Requests per second):

ab 测试输出
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# ab -c 10 -n 5000 -k http://localhost:8088/api/v1/sync/benchmark-hello-world
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 500 requests
Completed 1000 requests
...
Completed 5000 requests
Finished 5000 requests


Server Software:
Server Hostname:        localhost
Server Port:            8088

Document Path:          /api/v1/sync/benchmark-hello-world
Document Length:        13 bytes

Concurrency Level:      10
Time taken for tests:   4.694 seconds
Complete requests:      5000
Failed requests:        0
Keep-Alive requests:    5000
Total transferred:      3045525 bytes
HTML transferred:       65000 bytes
Requests per second:    1065.17 [#/sec] (mean)
Time per request:       9.388 [ms] (mean)
Time per request:       0.939 [ms] (mean, across all concurrent requests)
Transfer rate:          633.60 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     5    9   1.8      9      29
Waiting:        4    9   1.8      9      29
Total:          5    9   1.8      9      29

Percentage of the requests served within a certain time (ms)
  50%      9
  66%     10
  75%     10
  80%     10
  90%     12
  95%     13
  98%     14
  99%     15
  100%     29 (longest request)

4. 影响性能的原因

由于任务的实际执行内容由实际调用的脚本决定,如遇到让任务执行耗时长,可以检查代码中是否存在以下问题:

4.1 大量 print(...)

为了调试方便,有人可能会在脚本中直接将 DB 中数据、API 返回的数据全部通过 print(...) 输出

由于 DataFlux Func 会自动记录任务执行的 print(...) 内容,因此这样的操作会导致极大的性能损耗

建议在代码投入生产后,仅保留必要的 print(...) 输出,且避免大段文本的输出

4.2 外部系统响应慢

当在脚本中查询外部数据库、调用 API 接口(包括执行 DQL 查询等)时,如遇到网络卡顿,或者单纯就是对方系统响应慢,也会导致任务执行耗时长。

这与 DataFlux Func 基本无关,应联系这些外部系统负责人来改善响应速度。

以下是一个简单的测试处理耗时的示例代码:

Python
1
2
3
4
5
6
7
import time
import requests

def test():
    t1 = time.time()
    requests.get('http://github.com')
    print(f'Cost: {time.time() -  t1} s')