Fix read timing and test case

This commit is contained in:
Masaaki Goshima 2020-12-07 11:40:03 +09:00
parent 59f5713178
commit 699ab766f5
3 changed files with 11 additions and 4 deletions

View File

@ -14,6 +14,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
@ -373,9 +374,11 @@ func BenchmarkCodeDecoder(b *testing.B) {
buf.WriteByte('\n') buf.WriteByte('\n')
buf.WriteByte('\n') buf.WriteByte('\n')
if err := dec.Decode(&r); err != nil { if err := dec.Decode(&r); err != nil {
if err != io.EOF {
b.Fatal("Decode:", err) b.Fatal("Decode:", err)
} }
} }
}
}) })
b.SetBytes(int64(len(codeJSON))) b.SetBytes(int64(len(codeJSON)))
} }
@ -390,8 +393,10 @@ func BenchmarkUnicodeDecoder(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if err := dec.Decode(&out); err != nil { if err := dec.Decode(&out); err != nil {
if err != io.EOF {
b.Fatal("Decode:", err) b.Fatal("Decode:", err)
} }
}
r.Seek(0, 0) r.Seek(0, 0)
} }
} }
@ -414,10 +419,12 @@ func BenchmarkDecoderStream(b *testing.B) {
} }
x = nil x = nil
if err := dec.Decode(&x); err != nil || x != 1.0 { if err := dec.Decode(&x); err != nil || x != 1.0 {
if err != io.EOF {
b.Fatalf("Decode: %v after %d", err, i) b.Fatalf("Decode: %v after %d", err, i)
} }
} }
} }
}
func BenchmarkCodeUnmarshal(b *testing.B) { func BenchmarkCodeUnmarshal(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()

View File

@ -61,7 +61,6 @@ const (
// read data from r beyond the JSON values requested. // read data from r beyond the JSON values requested.
func NewDecoder(r io.Reader) *Decoder { func NewDecoder(r io.Reader) *Decoder {
s := newStream(r) s := newStream(r)
s.read()
return &Decoder{ return &Decoder{
s: s, s: s,
} }

View File

@ -26,6 +26,7 @@ func newStream(r io.Reader) *stream {
return &stream{ return &stream{
r: r, r: r,
bufSize: initBufSize, bufSize: initBufSize,
buf: []byte{nul},
} }
} }