mattn / go-mastodon

mastodon client for golang
MIT License
603 stars 89 forks source link

Optimize streaming #50

Closed 178inaba closed 7 years ago

178inaba commented 7 years ago

This PR provides streaming optimization and testing.

Benchmark of parse:

func BenchmarkAtoi(b *testing.B) {
    s := " 1234567"
    for i := 0; i < b.N; i++ {
        i, err := strconv.Atoi(strings.TrimSpace(s))
        _ = int64(i)
        if err != nil {
            b.Fatal(err)
        }
    }
}

func BenchmarkParseInt(b *testing.B) {
    s := " 1234567"
    for i := 0; i < b.N; i++ {
        _, err := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
        if err != nil {
            b.Fatal(err)
        }
    }
}

func BenchmarkUnmarshal(b *testing.B) {
    s := " 1234567"
    for i := 0; i < b.N; i++ {
        var i int64
        err := json.Unmarshal([]byte(s), &i)
        if err != nil {
            b.Fatal(err)
        }
    }
}
BenchmarkAtoi-8         30000000            52.6 ns/op
BenchmarkParseInt-8     30000000            48.0 ns/op
BenchmarkUnmarshal-8     3000000           445 ns/op
coveralls commented 7 years ago

Coverage Status

Coverage increased (+3.007%) to 84.226% when pulling c88e2df363e5c9bade8322b6020e4cc100b0d208 on 178inaba:streaming into 70b3dbf5488859738d02a5f741072ce0359bb90d on mattn:master.

178inaba commented 7 years ago

In addition, we fixed to close the channel using defer on the sender side. The user can terminate processing by simply canceling the context.

mattn commented 7 years ago

:1st_place_medal: great