예제 #1
0
파일: json.go 프로젝트: postfix/bangarang
func NewJsonEncoder() Encoder {
	je := &JsonEncoder{
		buff: bytes.NewBuffer(make([]byte, 1024*200)),
	}
	je.enc = ffjson.NewEncoder(je.buff)
	return je
}
예제 #2
0
파일: ff_test.go 프로젝트: gooops/ffjson
func TestMarshalEncoderError(t *testing.T) {
	out := NopWriter{}
	enc := ffjson.NewEncoder(&out)
	v := GiveError{}
	err := enc.Encode(v)
	require.Error(t, err, "excpected error from encoder")
	err = enc.Encode(newLogFFRecord())
	require.NoError(t, err, "error did not clear as expected.")

	err = enc.EncodeFast(newLogRecord())
	require.Error(t, err, "excpected error from encoder on type that isn't fast")
}
예제 #3
0
파일: ff_test.go 프로젝트: gooops/ffjson
func TestMarshalEncoder(t *testing.T) {
	record := newLogFFRecord()
	out := bytes.Buffer{}
	enc := ffjson.NewEncoder(&out)
	err := enc.Encode(record)
	require.NoError(t, err)
	require.NotEqual(t, 0, out.Len(), "encoded buffer size should not be 0")

	out.Reset()
	err = enc.EncodeFast(record)
	require.NoError(t, err)
	require.NotEqual(t, 0, out.Len(), "encoded buffer size should not be 0")
}
예제 #4
0
func PerformStat() {
	go func() {
		// Grab the initial stats.
		prev := appdb.Stats()

		for {
			time.Sleep(60 * time.Second)
			stats := appdb.Stats()
			diff := stats.Sub(&prev)
			json.NewEncoder(os.Stdout).Encode(diff)
			prev = stats
		}
	}()
}
예제 #5
0
//logs to a file
func logConnFile(logC chan dnsLogEntry, opts *logOptions) {

	logger := &lumberjack.Logger{
		Filename:   opts.Filename,
		MaxSize:    opts.MaxSize, // megabytes
		MaxBackups: opts.MaxBackups,
		MaxAge:     opts.MaxAge, //days
	}

	enc := ffjson.NewEncoder(bufio.NewWriter(logger))

	for message := range logC {
		enc.Encode(message)
	}

	logger.Close()

}
예제 #6
0
파일: ff_test.go 프로젝트: gooops/ffjson
func BenchmarkMarshalJSONNativeReuse(b *testing.B) {
	record := newLogFFRecord()

	buf, err := json.Marshal(&record)
	if err != nil {
		b.Fatalf("Marshal: %v", err)
	}
	b.SetBytes(int64(len(buf)))

	enc := ffjson.NewEncoder(&NopWriter{})
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := enc.Encode(record)
		if err != nil {
			b.Fatalf("Marshal: %v", err)
		}
	}
}