1

我正在学习rust,最近在尝试使用Sqlx库连接数据库。无论我是否使用池,连接速度始终至少为 2s;但是,当我使用 Java 连接器(mariadb.com 提供的 mariadb-java-client 包)连接到 mariadb 时不到 1 秒,我不太明白为什么会这样。我之前用过C#的MysqlConnector,连接mariadb至少需要2s。最后,我还尝试了 C Connector、C++ Connector 和 ODBC Connector,它们也比 Java Connector 慢。

kotlin 示例代码(使用 mariadb-java-client 包):

fun main() {
    var timeStart = LocalTime.now().toNanoOfDay()
    Class.forName(dbClsName)
    var timeUsed = LocalTime.now().toNanoOfDay() - timeStart
    println("load class took time = $timeUsed ns")
    val p = Properties()
    p["user"] = userName
    p["password"] = password

    timeStart = LocalTime.now().toNanoOfDay()
    val conn = DriverManager.getConnection(connStr, p)
    timeUsed = LocalTime.now().toNanoOfDay() - timeStart
    val secs = timeUsed.toDouble() / 1e9
    println("connect successed! took time = $timeUsed ns, almost $secs s")

    timeStart = LocalTime.now().toNanoOfDay()
    val stmt = conn.createStatement()
    val rst = stmt.executeQuery("SHOW DATABASES;")
    timeUsed = LocalTime.now().toNanoOfDay() - timeStart
    println("query took time = $timeUsed ns")
    var i = 0
    while (rst.next()){
        i++
        println("result${i}:\n\t${rst.getString(1)}")
    }
    conn.close()
}

kotlin 代码结果:

load class took time = 15631300 ns
connect successed! took time = 101765000 ns, almost 0.101765 s
query took time = 0 ns
...

Rust 示例代码(使用 sqlx):

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let mut time_start = chrono::Local::now();
    let mut db_conn = MySqlConnection::connect(CONN_STR).await?;
    let duration_conn = chrono::Local::now() - time_start;
    println!(
        "connect db took time = {}ns, almost {}s",
        duration_conn.num_nanoseconds().unwrap_or_default(),
        duration_conn.num_seconds()
    );

    time_start = chrono::Local::now();
    let results = sqlx::query("SHOW DATABASES")
        .map(|row: MySqlRow| row.get::<String, usize>(0))
        .fetch_all(&mut db_conn)
        .await?;
    let query_duration = chrono::Local::now() - time_start;
    println!(
        "query db took time = {}ns, almost {}s",
        query_duration.num_nanoseconds().unwrap_or_default(),
        query_duration.num_seconds()
    );

    for row_str in results {
        println!("{}", row_str)
    }
    Ok(())
}

Rust 样本结果:

connect db took time = 2045829800ns, almost 2s
query db took time = 1324300ns, almost 0s
...

C# 示例代码(使用 MysqlConnector):

public static void Main() {
    var time_start = DateTime.UtcNow;
    using var conn = new MySqlConnection(connStr);
    var time_takes = DateTime.UtcNow - time_start;
    Console.WriteLine($"consturct connection finished; took time = {time_takes.Ticks}ticks, 
        almost {(long)time_takes.TotalSeconds}s");
    time_start = DateTime.UtcNow;
    conn.Open();
    time_takes = DateTime.UtcNow - time_start;
    Console.WriteLine($"open mysql connection finished; take time = {time_takes.Ticks}ticks, 
        almost {(long)time_takes.TotalSeconds}s");

    using var query_command = new MySqlCommand("SHOW DATABASES;", conn);
    using var reader_dbs = query_command.ExecuteReader();
    while (reader_dbs.Read())
    {
        Console.WriteLine(reader_dbs.GetString(0));
    }
}

C# 示例结果:

consturct connection finished; took time = 1597ticks, almost 0s
open mysql connection finished; take time = 22826302ticks, almost 2s
...

另外,请忽略我没有用基准测试库测量经过的时间,因为Java连接器与其他连接器之间的连接时间差异是肉眼可见的。

4

0 回答 0