Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-safe conversion for line-protocol Addfield #43

Closed
arukiidou opened this issue Sep 26, 2023 · 2 comments · Fixed by #45
Closed

Type-safe conversion for line-protocol Addfield #43

arukiidou opened this issue Sep 26, 2023 · 2 comments · Fixed by #45
Milestone

Comments

@arukiidou
Copy link

arukiidou commented Sep 26, 2023

Use Case

  • I want asserure that AddField() without panicking.
  • avoiding reflect.
  • Please show me the error while I am editing in VSCode.

Expected behavior

  • use unsupported types, then compile error.
func example() {
	p := NewPoint("measurement", map[string]string{}, map[string]interface{}{"measurement": "air"}, time.Now())
        // unsupported type.
	p.AddFieldFromValue("✅ Compile error.", cmplx.Inf())
        // supported type.
	p.AddFieldFromValue("✅ This is safe.", NewValueFromInt(255))
}

Actual behavior

  • use unsupported types, then panic.
func example() {
	p := NewPoint("measurement", map[string]string{}, map[string]interface{}{"measurement": "air"}, time.Now())
        // unsupported type.
	p.AddField("❌ Will panic.", cmplx.Inf())
        // supported type.
	p.AddField("❓This is OK, but really?", 255)
}

Additional info

NewValueFrom

func (m *Point) AddFieldFromValue(k string, v lineprotocol.Value) *Point {}
func NewValueFromInt[I Integer](v I) lineprotocol.Value {}

(Retracted)NewFieldFrom

func (m *Point) AddFieldRaw(k string, v Field) *Point {}
func NewFieldFromInt[I Integer](v I) Field {}

Why not line-protocol?

@bednar
Copy link
Member

bednar commented Sep 27, 2023

Hi @arukiidou,

Thank you for raising the issue and submitting your PR. I have a preference for Variant A.

Best regards

@arukiidou
Copy link
Author

arukiidou commented Oct 1, 2023

benchmarking

✅ This is not only type safe, lower memory allocation and performance improvement.

go test -bench . -benchmem
cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
// p.AddField("float64", f)
BenchmarkAddField-12                     3476425               344.5 ns/op           152 B/op          9 allocs/op
// p.AddFieldFromValue("float64", NewValueFromFloat(f))
BenchmarkNewValueFromTyped-12            9434280               121.2 ns/op            48 B/op          2 allocs/op
// p.AddFieldFromValue("float64", NewValueFromNative(f))
BenchmarkNewValueFromNative-12           4411726               269.3 ns/op           112 B/op          7 allocs/op
func BenchmarkAddField(b *testing.B) {
	// cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
	// BenchmarkAddPoint-12		         	 3410827	       345.9 ns/op	     152 B/op	       9 allocs/op
	// BenchmarkNewValueFromTyped-12    	 9870652	       122.6 ns/op	      48 B/op	       2 allocs/op
	// BenchmarkNewValueFromNative-12    	 4241845	       282.0 ns/op	     112 B/op	       7 allocs/op
	p := NewPoint(
		"bench",
		map[string]string{
			"id":        "10ad=",
			"ven=dor":   "AWS",
			`host"name`: `ho\st "a"`,
			`x\" x`:     "a b",
		},
		map[string]interface{}{},
		time.Unix(60, 70))

	f := float64(80.1234567)
	ints := int64(-1234567890)
	u := uint64(12345677890)
	s := string(`six, "seven", eight`)
	bt := []byte(`six=seven\, eight`)
	bl := bool(false)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		p.AddField("float64", f)
		p.AddField("int64", ints)
		p.AddField("uint64", u)
		p.AddField("string", s)
		p.AddField("bytes", bt)
		p.AddField("bool", bl)
	}
}

func BenchmarkNewValueFromTyped(b *testing.B) {
	// BenchmarkNewValueFromTyped-12    	 9870652	       122.6 ns/op	      48 B/op	       2 allocs/op
	// BenchmarkNewValueFromNative-12    	 4241845	       282.0 ns/op	     112 B/op	       7 allocs/op
	p := NewPoint(
		"bench",
		map[string]string{
			"id":        "10ad=",
			"ven=dor":   "AWS",
			`host"name`: `ho\st "a"`,
			`x\" x`:     "a b",
		},
		map[string]interface{}{},
		time.Unix(60, 70))

	f := float64(80.1234567)
	ints := int64(-1234567890)
	u := uint64(12345677890)
	s := string(`six, "seven", eight`)
	bt := []byte(`six=seven\, eight`)
	bl := bool(false)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		p.AddFieldFromValue("float64", NewValueFromFloat(f))
		p.AddFieldFromValue("int64", NewValueFromInt(ints))
		p.AddFieldFromValue("uint64", NewValueFromUInt(u))
		p.AddFieldFromValue("string", NewValueFromString(s))
		p.AddFieldFromValue("bytes", NewValueFromString(bt))
		p.AddFieldFromValue("bool", NewValueFromBoolean(bl))
	}
}

func BenchmarkNewValueFromNative(b *testing.B) {
	// BenchmarkNewValueFromNative-12    	 4241845	       282.0 ns/op	     112 B/op	       7 allocs/op
	p := NewPoint(
		"bench",
		map[string]string{
			"id":        "10ad=",
			"ven=dor":   "AWS",
			`host"name`: `ho\st "a"`,
			`x\" x`:     "a b",
		},
		map[string]interface{}{},
		time.Unix(60, 70))

	f := float64(80.1234567)
	ints := int64(-1234567890)
	u := uint64(12345677890)
	s := string(`six, "seven", eight`)
	bt := []byte(`six=seven\, eight`)
	bl := bool(false)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		p.AddFieldFromValue("float64", NewValueFromNative(f))
		p.AddFieldFromValue("int64", NewValueFromNative(ints))
		p.AddFieldFromValue("uint64", NewValueFromNative(u))
		p.AddFieldFromValue("string", NewValueFromNative(s))
		p.AddFieldFromValue("bytes", NewValueFromNative(bt))
		p.AddFieldFromValue("bool", NewValueFromNative(bl))
	}
}

AddField

image

NewValueFrom-Typed

image

NewValueFrom-Native

image

@bednar bednar mentioned this issue Oct 11, 2023
6 tasks
@bednar bednar closed this as completed in #45 Nov 3, 2023
@bednar bednar added this to the 0.4.0 milestone Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants