func BenchmarkDecodeBinaryDecimal(b *testing.B) { wbuf := writeBuffer{bytecount: metric.NewCounter(metric.Metadata{Name: ""})} expected := new(parser.DDecimal) s := "-1728718718271827121233.1212121212" if _, ok := expected.SetString(s); !ok { b.Fatalf("could not set %q on decimal", s) } wbuf.writeBinaryDatum(expected, nil) rbuf := readBuffer{msg: wbuf.wrapped.Bytes()} plen, err := rbuf.getUint32() if err != nil { b.Fatal(err) } bytes, err := rbuf.getBytes(int(plen)) if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { b.StartTimer() got, err := decodeOidDatum(oid.T_numeric, formatBinary, bytes) b.StopTimer() if err != nil { b.Fatal(err) } else if got.Compare(expected) != 0 { b.Fatalf("expected %s, got %s", expected, got) } } }
func benchmarkWriteDecimal(b *testing.B, format formatCode) { dec := new(parser.DDecimal) s := "-1728718718271827121233.1212121212" if _, ok := dec.SetString(s); !ok { b.Fatalf("could not set %q on decimal", format) } benchmarkWriteType(b, dec, formatText) }
func TestBinaryDecimal(t *testing.T) { defer leaktest.AfterTest(t)() testBinaryDatumType(t, "decimal", func(val string) parser.Datum { dec := new(parser.DDecimal) if _, ok := dec.SetString(val); !ok { t.Fatalf("could not set %q on decimal", val) } return dec }) }
func BenchmarkWriteBinaryDecimal(b *testing.B) { buf := writeBuffer{bytecount: metric.NewCounter(metric.Metadata{Name: ""})} dec := new(parser.DDecimal) s := "-1728718718271827121233.1212121212" if _, ok := dec.SetString(s); !ok { b.Fatalf("could not set %q on decimal", s) } // Warm up the buffer. buf.writeBinaryDatum(dec, nil) buf.wrapped.Reset() b.ResetTimer() for i := 0; i < b.N; i++ { b.StartTimer() buf.writeBinaryDatum(dec, nil) b.StopTimer() buf.wrapped.Reset() } }
func TestRandomBinaryDecimal(t *testing.T) { defer leaktest.AfterTest(t)() if *generateBinaryCmd == "" { t.Skip("disabled") } fields := strings.Fields(*generateBinaryCmd) if len(fields) < 1 { t.Fatal("expected generate-binary arguments") } name, args := fields[0], fields[1:] var tests []binaryTest randutil.SeedForTests() for { var b bytes.Buffer for n := rand.Intn(50); n >= 0; n-- { fmt.Fprint(&b, rand.Intn(10)) } v := b.String() { z := strings.Repeat("0", rand.Intn(10)) pos := rand.Intn(len(v) + 1) v = v[:pos] + z + v[pos:] } if rand.Intn(2) == 0 { pos := rand.Intn(len(v) + 1) v = v[:pos] + "." + v[pos:] } cmdargs := append(args, `"`+v+`"`) out, err := exec.Command(name, cmdargs...).CombinedOutput() if err != nil { t.Log(string(out)) t.Fatal(err) } if err := json.Unmarshal(out, &tests); err != nil { t.Fatal(err) } if len(tests) != 1 { t.Fatal("expected 1 test") } test := tests[0] buf := writeBuffer{bytecount: metric.NewCounter(metric.Metadata{})} dec := new(parser.DDecimal) if _, ok := dec.SetString(test.In); !ok { t.Fatalf("could not set %q on decimal", test.In) } buf.writeBinaryDatum(dec, time.UTC) if buf.err != nil { t.Fatal(buf.err) } if got := buf.wrapped.Bytes(); !bytes.Equal(got, test.Expect) { t.Errorf("%q:\n\t%v found,\n\t%v expected", test.In, got, test.Expect) } else if datum, err := decodeOidDatum(oid.T_numeric, formatBinary, got[4:]); err != nil { t.Errorf("%q: unable to decode %v: %s", test.In, got[4:], err) } else if dec.Compare(datum) != 0 { t.Errorf("%q: expected %s, got %s", test.In, dec, datum) } } }