全部产品
Search
文档中心

云数据库 MongoDB:MongoDB SSL加密连接对实例性能影响测试

更新时间:Oct 30, 2024

本文介绍如何测试SSL加密对云数据库 MongoDB 版实例的性能影响。

测试环境

创建ECS实例和云数据库 MongoDB 版副本集实例。如何创建,请参见创建副本集实例通过控制台使用ECS实例(快捷版)

本次测试的实例配置如下:

  • MongoDB实例

    • 实例架构:副本集

    • 主备节点数:三节点

    • 只读节点数:一只读节点

    • 实例规格:1核2 GB(通用型)

    • 实例版本:MongoDB 4.2

  • ECS实例

    • 实例规格:ecs.e-c1m2.large(2核4 GB)

测试方法

导入测试数据

本文使用YCSB导入1000000条数据,数据默认导入到ycsb数据库里的usertable集合中。导入数据的命令如下。

./bin/ycsb load mongodb -s -p workload=site.ycsb.workloads.CoreWorkload -p recordcount=1000000 -p mongodb.url="mongodb://root:******@dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717,dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717,dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondary&replicaSet=mgset-82894753" -threads 8

YCSB生成的集合结构如下。

{
  "_id": "user1352498093671118016",
  "field1": "BinData(0,\"L1s/KS8+MlYhISUuIz8qNVx9PTRuNiooNlolPVMzJCE0ODNuP15zLjB6NSU4LV0pPC98LkovOFttOyQkL1chNV8tOlxrKlstPkp3IzkiLTcuLU93ITRqOzEwPScsIEx5L1o5JQ==\")",
  "field0": "BinData(0,\"I0I/NDAmNUkrLFA3PEA5IEI3KC4qKEJ1LCc6JV8rJlo7KUR3MDRqMk95KjhoIlUjLy1qMls9OFU7PTpuKTZsNlUpMVYvNSI4KDlwK1trIUpnMlInKSAmNDg8LSpmOl85PkozMA==\")",
  "field7": "BinData(0,\"NzxyPjUkNiYsOEp1MUIpIDA0M1U/JE1nMjMmODhkIko/LEh1NSRqKl1jKDBqJjMqMiw8PSc6KjgsNEwrLFkpKFh9OzQ+MS86LiB4P1oxLVxjIiQgMCdsM0AhO0QzNTFqI1dpMw==\")",
  "field6": "BinData(0,\"MVhpIj1uI0RlJ1AtP0t/LkAxMVp9MlU3KFJpK0Z3M0M/OE55IFBhPyA6Njp+L1tnJjh4Iic4K1F/Oi9gKz44PFFpPE5jNydqKUJrPyV4KDY2LDd0Nic6PC9wPDhgNykwOj10Iw==\")",
  "field9": "BinData(0,\"OEA/NDRwLilwLzVoMz5oIzVuLStoPlF9NVU5MVd3PDUkPV0nPl07OU05LkYnKDQ8L0VrIkwlJDg+NCRuIitoNjV4PkM9NCt0LyE4PCFkKUI/LVwxKjdsMkgnICsgKFQrJit8Lg==\")",
  "field8": "BinData(0,\"P0J/P0ozMCBwIjJwP0IpJUMlPkhhIkI3MU91OjUmMFM9NEBhJD8+OEdpJDh2OyxmMCQkKUgnJjYoKC0kKVMnKTQuODp6JjVwKSRwI1FhJy88PVAhPiE2OCMsKlYvJF1xOEUrJA==\")",
  "field3": "BinData(0,\"Mjc6KE4zJTEuJCJ4P18lNCZ4NkAtKTAwLFUvMDEsOzUkIEs3MVEzKT4mL1Q5IVF9N0N3O1snIUZ1L0k9IUY7IV8nNiQ+KTJ0I097Llg1MjE0PCQ4KDV2KzYsIDtwNzhuKkQrMA==\")",
  "field2": "BinData(0,\"Jit6L0olLTByJzpuPygyKSE8Li9iK00xIig4PVQhKTBqLkI/O0N9Izs+NVk/ISVmIy14Nko3NyxoI1Z1Pyo4JVt/MylsK1t9PS9yP1kzJFMjPF43P1l5P1xtPDAqLF57IiAsPQ==\")",
  "field5": "BinData(0,\"NSd2NiskPzcmITU4Oz58NzliP0ZpOz5wKiI0OVhlNDx0PlV1OllnNzJiOyMmOF45IEg7OD90KUZnP0g5OUJzK0M/OyN4O15lPzA6LTUgNUw9NDVoL0QrOTssISQ2KCw6KUJlKg==\")",
  "field4": "BinData(0,\"NDNkKjFuPyxsJzEmNzMqLVJzL0YpMTNwMjs8MTRiNEYtLE05P0I9NEU1LS4yNEFpNjUsOUs9JEs1IU5jOF5vIjpiNTMuKSY6K0s1LDp2JEp9KyFiIS1+J1U/LkoxMEp5OzJiIw==\")"
}

运行测试脚本

由于YCSB不支持在SSL连接方式下进行压测,因此本文中将采用测试脚本进行压测。脚本具体内容如下。

import time
import pymongo
import datetime
import logging as log
from functools import wraps

log.basicConfig(
    level=log.INFO, format='%(asctime)s - [line %(lineno)d] : %(levelname)s - %(message)s')
console = log.StreamHandler()
console.setLevel(log.INFO)
formatter = log.Formatter(
    '%(asctime)s - [line %(lineno)d] : %(levelname)s - %(message)s')
console.setFormatter(formatter)


def time_recorder():
    def inner(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            start_time = time.time()
            result = func(*args, **kwargs)
            end_time = time.time()
            elapsed_time = (end_time - start_time) * 1000

            print_latency = kwargs.get("print_latency", False)
            return_latency = kwargs.get("return_latency", False)

            if print_latency:
                log.info(
                    f"Function {func.__name__} took {elapsed_time:.4f} millseconds to execute.")

            if return_latency:
                return result, elapsed_time
            return result
        return wrapper
    return inner


@time_recorder()
def build_client_without_ssl(**kwargs):
    client = pymongo.MongoClient(
        "mongodb://root:******@dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717,dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717,dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondary&replicaSet=mgset-82894753")
    return client


@time_recorder()
def build_client_with_ssl(**kwargs):
    client = pymongo.MongoClient(
        "mongodb://root:******@dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717,dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717,dds-bp103175eafac****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondary&replicaSet=mgset-82894753&tls=true&tlsAllowInvalidHostnames=true&tlsCAFile=/root/ApsaraDB-CA-Chain.pem")
    return client


@time_recorder()
def find_one(client, **kwargs):
    client["ycsb"]["usertable"].find_one()


def test_connection():
    client = build_client_without_ssl(print_latency=True)
    find_one(client)

    client = build_client_with_ssl(print_latency=True)
    find_one(client)


test_connection()


@time_recorder()
def test(with_ssl=False, repeat=5000, **kwargs):
    log.info(f"test, with_ssl: {with_ssl}, repeat: {repeat}")
    ssl_suffix = "without-ssl"
    if with_ssl:
        ssl_suffix = "with-ssl"
    suffix = f"{repeat}-{ssl_suffix}.log"
    only_connection_file = f"only-connection-{suffix}"
    first_query_file = f"first-query-{suffix}"
    connection_and_first_query_file = f"connection-and-first-query-{suffix}"

    only_connection_f = open(only_connection_file, 'w')
    first_query_f = open(first_query_file, 'w')
    connection_and_first_query_f = open(connection_and_first_query_file, 'w')

    for _ in range(repeat):
        client = None
        connection_latency = 0

        if with_ssl:
            client, connection_latency = build_client_with_ssl(
                return_latency=True)
        else:
            client, connection_latency = build_client_without_ssl(
                return_latency=True)

        _, query_latency = find_one(client, return_latency=True)

        only_connection_f.write(f"{connection_latency}\n")
        first_query_f.write(f"{query_latency}\n")
        connection_and_first_query_f.write(f"{connection_latency + query_latency}\n")

    only_connection_f.close()
    first_query_f.close()
    connection_and_first_query_f.close()

    log.info(f"test end, write to {only_connection_file}, {first_query_file} and {connection_and_first_query_file}")


test(False, 5000, print_latency=True)
test(True, 5000, print_latency=True)


@time_recorder()
def run(client, log_file, repeat=5000, **kwargs):
    start = time.time()
    log.info(f"start to query, repeat: {repeat}")

    with open(log_file, 'w') as f:
        for _ in range(repeat):
            latency = find_one(client, return_latency=True)
            f.write(f"{latency}\n")

    end = time.time()
    total = (end - start) * 1000
    avg = total / repeat
    log.info(
        f"end query, repeat: {repeat}, total: {total} millseconds, avg: {avg} millseconds")


client = build_client_without_ssl()
run(client, "without-ssl.log")

client = build_client_with_ssl()
run(client, "with-ssl.log")

测试时,您可以根据需求修改以下参数:

  • client:请将pymongo.MongoClient()参数括号内的连接串、用户名和密码替换为实例的真实信息。

  • repeat:运行次数,本次测试将重复运行5000次,您可以根据实际需求修改运行次数。

脚本的输出信息如下:

  • only-connection:记录创建连接的耗时。

  • first-query:记录创建连接以后第一次查询的耗时,查询为简单的find_oneclient["ycsb"]["usertable"].find_one()

  • connection-and-first-query:该项为only-connection与first-query两项之和,记录创建连接到第一次查询完成的总耗时。

  • latency:记录复用Client情况下,5000次查询的平均耗时。

测试结果

以下数据为5000次测试下的平均值。

测试项

未使用SSL连接的耗时(单位:ms)

使用SSL连接的耗时(单位:ms)

创建连接的耗时

3.7009

19.8515

创建连接后的第一次查询的耗时

20.7456

43.5884

创建连接到第一次查询完成的总耗时

24.4465

63.4399

复用Client情况下查询的平均耗时

0.7506

0.7643

经测试,使用SSL连接和未使用SSL连接的耗时主要差距在于创建连接步骤。如果复用已经建立好的连接,则使用SSL连接和不使用SSL连接方式下查询的平均耗时差距不大。造成该结果的原因主要包括以下两个方面:

  • TLS(Transport Layer Security,传输层安全性协议)双方建立连接多出来的RTT(Round-Trip Time,往返时延)。

    使用连接池(Connection Pooling)的情况下,建立连接多出来的RTT开销会被均摊,您在使用SSL连接时尽量复用已经建立好的Client,能够有效避免每次新建连接产生的TLS RTT时间。

  • 建立连接后,消息体使用加密解密算法的额外开销。

    现在CPU对消息体的加密解密速度已足够快,查询耗时可能更多体现在系统的其他方面(例如磁盘IO、队列等待等),加密解密对整体性能的影响相对来说就会变小。