欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java和Go性能的简单比较(mysql query)

程序员文章站 2022-03-06 08:49:50
...

通过简单地查询公司表(comp)的100条数据来直观地比较语言的性能。

环境:笔记本64位win10, jdk 1.8, go 1.11, 本机10.1.15-MariaDB。

 

1. java代码

package main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 连续查询100条comp,测试时间:
 */
public class TestSqlStress {

    public static void main(String[] args) throws Exception {
        long start0 = System.currentTimeMillis();
        String url = "jdbc:mysql://127.0.0.1:3306/testdb?connectionCollation=utf8mb4_general_ci&serverTimezone=Asia/Shanghai&user=root&password=1";
        Connection cn = DriverManager.getConnection(url);

        long start = System.currentTimeMillis();

        String query = "SELECT id,sync_key,name,logo_url,capital,found_date,staff,tel,addr,intro,\n" +
                "            IFNULL(tag_codes,\"\") 'tag_codes', IFNULL(tag_names,\"\") 'tag_names',\n" +
                "            Unix_timestamp(create_time) 'create_time', is_disable, \n" +
                "            IFNULL(Unix_timestamp(disable_time),0) 'disable_time', IFNULL(disable_remark, \"\") 'disable_remark'\n" +
                "        FROM comp\n" +
                "        LIMIT 100";
        Statement stmt = cn.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        long id;
        String sync_key, name, logo_url,capital,found_date,staff,tel,addr,intro;
        String tag_codes, tag_names;
        long create_time, is_disable, disable_time;
        String disable_remark;

        List arr = new ArrayList(200);
        while (rs.next()) {
            id = rs.getLong(1);
            sync_key = rs.getString(2);
            name = rs.getString(3);
            logo_url = rs.getString(4);
            capital = rs.getString(5);
            found_date = rs.getString(6);
            staff = rs.getString(7);
            tel = rs.getString(8);
            addr = rs.getString(9);
            intro = rs.getString(10);
            tag_codes = rs.getString(11);
            tag_names = rs.getString(12);
            create_time = rs.getLong(13);
            is_disable = rs.getLong(14);
            disable_time = rs.getLong(15);
            disable_remark = rs.getString(16);

            Map map = new HashMap();
            map.put("id", id);
            map.put("sync_key", sync_key);
            map.put("name", name);
            map.put("logo_url", logo_url);
            map.put("capital", capital);
            map.put("found_date", found_date);
            map.put("staff", staff);
            map.put("tel", tel);
            map.put("addr", addr);
            map.put("intro", intro);
            map.put("tag_codes", tag_codes);
            map.put("tag_names", tag_names);
            map.put("create_time", create_time);
            map.put("is_disable", is_disable);
            map.put("disable_time", disable_time);
            map.put("disable_remark", disable_remark);
            arr.add(map);
        }

        System.out.println("query took " + (System.currentTimeMillis() - start) + " ms");

        String str = arr.toString();
        System.out.println("str len=" + str.length());

        rs.close();
        stmt.close();
        cn.close();
        System.out.println("main took " + (System.currentTimeMillis() - start0) + " ms");
    }

}

 

2. Go代码

// 连续查询100条comp,测试时间
package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "time"

    _ "github.com/ziutek/mymysql/godrv"
)

func main() {
    start0 := time.Now()
    defer func() {
        fmt.Println("main took", time.Since(start0).Nanoseconds() / 1000000, "ms")
    }()

    db, err := sql.Open("mymysql", "tcp:127.0.0.1:3306*testdb/root/1")
    if err != nil { panic(err) }
    defer db.Close()

    start := time.Now()

    query := `
        SELECT id,sync_key,name,logo_url,capital,found_date,staff,tel,addr,intro,
            IFNULL(tag_codes,"") 'tag_codes', IFNULL(tag_names,"") 'tag_names',
            Unix_timestamp(create_time) 'create_time', is_disable, 
            IFNULL(Unix_timestamp(disable_time),0) 'disable_time', IFNULL(disable_remark, "") 'disable_remark'
        FROM comp
        LIMIT 100
    `
    rows, err := db.Query(query)
    if err != nil { panic(err) }
    defer rows.Close()

    var id int64
    var sync_key, name, logo_url,capital,found_date,staff,tel,addr,intro string
    var tag_codes, tag_names string
    var create_time, is_disable, disable_time int64
    var disable_remark string

    arr := make([]map[string]interface{}, 0)
    for rows.Next() {
        err = rows.Scan(&id, &sync_key, &name, &logo_url, &capital, &found_date, &staff, &tel, &addr, &intro,
            &tag_codes, &tag_names, 
            &create_time, &is_disable, &disable_time,
            &disable_remark)
        if err != nil { panic(err) }

        ma := map[string]interface{} {
            "id": id,
            "sync_key": sync_key,
            "name": name,
            "logo_url": logo_url,
            "capital": capital,
            "found_date": found_date,
            "staff": staff,
            "tel": tel,
            "addr": addr,
            "intro": intro,
            "tag_codes": tag_codes,
            "tag_names": tag_names,
            "create_time": create_time,
            "is_disable": is_disable,
            "disable_time": disable_time,
            "disable_remark": disable_remark,
        }
        arr = append(arr, ma)
    }
    err = rows.Err()  // rows.Next 的错误
    if err != nil { panic(err) }

    fmt.Println("query took", time.Since(start).Nanoseconds() / 1000000, "ms")

    buf, err := json.Marshal(arr)
    if err != nil { panic("json marshal error: " + err.Error()) }
    fmt.Println("json len=", len(string(buf)))
}

 

3. 测试结果

sql_stress Go (ziutek/mymysql v1.5.4)

    query took 3 ms

    json len= 90520

    main took 6 ms

sql_stress Java

    -- mysql-connector-java-5.1.25

        query took 18 ms

        str len=50869

        main took 389 ms

    -- mysql-connector-java-8.0.15

        query took 38 ms

        str len=50869

        main took 473 ms

 

4. 结论分析

首先令人吃惊的是Go查询仅用了3毫秒,而Java快的也用了18毫秒。

其次mysql 官方的jdbc driver 从5.1升级到8.0后性能反而下降一倍左右:从18毫秒慢到了38毫秒。

 

初步看来,Go除了代码简洁外,运行性能也是令人放心的。

 

相关标签: Go Java