zoukankan      html  css  js  c++  java
  • md5随机性验证

    本测试旨在测试md5编码之后的数据hash随机性,以验证ZCache各节点数据分布的均匀性

    测试代码如下

    package main
    
    import (
        "crypto/md5"
        "encoding/hex"
        "fmt"
        "math/rand"
        "sort"
        "strconv"
    )
    
    func main() {
    
        var lenArray []int64 = []int64{16384, 32768, 65536}
        for _, testLen := range lenArray {
            gCount := make([]int64, testLen)
            for i := 0; i < 10000000; i++ {
                msg := randSeq(10)
                idx, err := GetHashIndex(msg, testLen)
                if err != nil {
                    continue
                }
                if 0 == i%1000 {
                    //fmt.Println("curIndex ", i)
                }
                gCount[idx]++
            }
    
            var i int64 = 0
            var max int64 = 0
            var min int64 = 10000000
            var floatCountArr []float64
            for ; i < testLen; i++ {
                if gCount[i] > max {
                    max = gCount[i]
                }
                if gCount[i] < min {
                    min = gCount[i]
                }
                strCount := strconv.FormatInt(gCount[i], 10)
                floatCount, _ := strconv.ParseFloat(strCount, 64)
                floatCountArr = append(floatCountArr, floatCount)
            }
    
            sort.Float64s(floatCountArr)
            fmt.Println("Len", testLen, "Max", uint64(floatCountArr[testLen-1]), "Min", uint64(floatCountArr[0]), "Mid", uint64(floatCountArr[testLen/2]))
        }
    
    }
    func GetHashIndex(msg string, size int64) (int64, error) {
        msgByte := Md5Encode(msg)
        msgByte = msgByte[0 : len(msgByte)/2-1]
        data, err := ByteToInt(msgByte)
        if err != nil {
            return -1, err
        }
        data = data % size
        return data, nil
    }
    
    func Md5Encode(msg string) []byte {
        Md5Inst := md5.New()
        Md5Inst.Write([]byte(msg))
        result := Md5Inst.Sum([]byte(""))
        return result
    }
    
    func ByteToInt(msg []byte) (int64, error) {
        encodedStr := "0x" + hex.EncodeToString(msg)
        data, err := strconv.ParseInt(encodedStr, 0, 64)
        if err != nil {
            return -1, err
        }
        return data, nil
    }
    
    var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    
    func randSeq(n int) string {
        b := make([]rune, n)
        for i := range b {
            b[i] = letters[rand.Intn(len(letters))]
        }
        return string(b)
    }
    
    
    测试结果
    Len 2 Max 5000044 Min 4999956 Mid 5000044
    Len 4 Max 2500974 Min 2499376 Mid 2500112
    Len 8 Max 1253127 Min 1247961 Mid 1249557
    Len 16 Max 626016 Min 623809 Mid 625199
    Len 32 Max 313527 Min 311180 Mid 312571
    Len 64 Max 157193 Min 155134 Mid 156302
    Len 128 Max 78929 Min 77217 Mid 78124
    Len 256 Max 39737 Min 38492 Mid 39078
    Len 512 Max 19983 Min 19036 Mid 19526
    Len 1024 Max 10125 Min 9455 Mid 9768
    Len 2048 Max 5172 Min 4659 Mid 4882
    Len 4096 Max 2611 Min 2248 Mid 2441
    Len 8192 Max 1339 Min 1080 Mid 1221
    Len 16384 Max 720 Min 511 Mid 610
    Len 32768 Max 382 Min 223 Mid 305
    Len 65536 Max 207 Min 106 Mid 152
  • 相关阅读:
    c#调用dll,::CoInitialize(NULL)出错
    使用 Anthem.NET 的常见回调(Callback)处理方式小结
    主题和皮肤学习
    得到任意网页源代码 (利用WebClient和WebRequest类)
    HTML marquee标签详解
    制作一个简单的天气预报
    CSS中的类class和标识id选择符(.和#号)
    String split '.'
    Map 的 clear() 方法会清空 Map对象
    sqLite 执行查询语句时报错__及SimpleCursorAdapter
  • 原文地址:https://www.cnblogs.com/zengyjun/p/10133350.html
Copyright © 2011-2022 走看看