예제 #1
0
func BenchmarkParseTestdata(b *testing.B) {
	b.ReportAllocs()
	b.StopTimer()

	f, err := os.Open("./testdata/code.json.gz")
	require.Nil(b, err)

	gz, err := gzip.NewReader(f)
	require.Nil(b, err)

	codeJSON, err := ioutil.ReadAll(gz)
	require.Nil(b, err)

	parser := bari.NewParser(&cyclingReader{data: string(codeJSON)})
	ch := make(chan bari.Event)

	b.StartTimer()

	go func() {
		parser.Parse(ch)
	}()

	for i := 0; i < b.N; i++ {
		for j := 0; j < 396995; j++ {
			<-ch
		}
	}

	b.SetBytes(int64(len(codeJSON)))
}
예제 #2
0
func ExampleParser_Parse_multi() {
	const data = `{"foo": "bar"}{"bar": true}`

	parser := bari.NewParser(strings.NewReader(data))
	ch := make(chan bari.Event)

	go func() {
		parser.Parse(ch)
		close(ch)
	}()

	for ev := range ch {
		fmt.Println(ev.Type, ev.Value)
	}
	// Output:
	// ObjectStartEvent <nil>
	// ObjectKeyEvent <nil>
	// StringEvent foo
	// ObjectValueEvent <nil>
	// StringEvent bar
	// ObjectEndEvent <nil>
	// ObjectStartEvent <nil>
	// ObjectKeyEvent <nil>
	// StringEvent bar
	// ObjectValueEvent <nil>
	// BooleanEvent true
	// ObjectEndEvent <nil>
}
예제 #3
0
func TestParse(t *testing.T) {
	for i, c := range testCases {
		parser := bari.NewParser(strings.NewReader(c.data))
		ch := make(chan bari.Event)

		go func() {
			parser.Parse(ch)
			close(ch)
		}()

		for _, evt := range c.events {
			ev := <-ch
			fmt.Printf("case %d: `%s` %+v\n", i, c.data, ev)
			ck(t, ev, evt.typ, evt.value, evt.err)
		}
	}
}
예제 #4
0
func TestParseTestdata(t *testing.T) {
	f, err := os.Open("./testdata/code.json.gz")
	require.Nil(t, err)

	gz, err := gzip.NewReader(f)
	require.Nil(t, err)

	parser := bari.NewParser(gz)
	ch := make(chan bari.Event)

	go func() {
		parser.Parse(ch)
		close(ch)
	}()

	for ev := range ch {
		require.Nil(t, ev.Error)
	}
}
예제 #5
0
func BenchmarkParseStringWithUnicodeChars(b *testing.B) {
	b.ReportAllocs()
	b.StopTimer()

	const data = `{"foo": "\u265e\u2602"}`
	parser := bari.NewParser(&cyclingReader{data: data})
	ch := make(chan bari.Event)

	b.StartTimer()

	go func() {
		parser.Parse(ch)
	}()

	for i := 0; i < b.N; i++ {
		for j := 0; j < 6; j++ {
			<-ch
		}
	}

	b.SetBytes(int64(len(data)))
}
예제 #6
0
func BenchmarkParseMultiObjectStream(b *testing.B) {
	b.ReportAllocs()
	b.StopTimer()

	const data = `{"foo": "bar"}{"foo": "bar"}{"foo": "bar"}{"foo": "bar"}`
	parser := bari.NewParser(&cyclingReader{data: data})
	ch := make(chan bari.Event)

	b.StartTimer()

	go func() {
		parser.Parse(ch)
	}()

	for i := 0; i < b.N; i++ {
		for j := 0; j < 6; j++ {
			<-ch
		}
	}

	b.SetBytes(int64(len(data)))
}