1
0
mirror of https://github.com/google/uuid.git synced 2025-09-16 09:16:30 +02:00

benchmarks for Parse(), New(), String(), URN(), MarshalJSON, UnmarshalJSON()

There are no parse modifications yet.  This allows a benchmark to be
established for later commits.
This commit is contained in:
Bryan Matsuo
2015-10-10 13:59:31 -07:00
parent cccd189d45
commit baeec2195c
2 changed files with 68 additions and 0 deletions

View File

@@ -30,3 +30,32 @@ func TestJSON(t *testing.T) {
t.Errorf("got %#v, want %#v", s2, s1) t.Errorf("got %#v, want %#v", s2, s1)
} }
} }
func BenchmarkUUID_MarshalJSON(b *testing.B) {
x := &struct {
UUID UUID `json:"uuid"`
}{}
x.UUID = Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
if x.UUID == nil {
b.Fatal("invalid uuid")
}
for i := 0; i < b.N; i++ {
js, err := json.Marshal(x)
if err != nil {
b.Fatalf("marshal json: %#v (%v)", js, err)
}
}
}
func BenchmarkUUID_UnmarshalJSON(b *testing.B) {
js := []byte(`{"uuid":"f47ac10b-58cc-0372-8567-0e02b2c3d479"}`)
var x *struct {
UUID UUID `json:"uuid"`
}
for i := 0; i < b.N; i++ {
err := json.Unmarshal(js, &x)
if err != nil {
b.Fatalf("marshal json: %#v (%v)", js, err)
}
}
}

39
uuid_test.go Executable file → Normal file
View File

@@ -388,3 +388,42 @@ func TestBadRand(t *testing.T) {
t.Errorf("unexecpted duplicates, got %q\n", uuid1) t.Errorf("unexecpted duplicates, got %q\n", uuid1)
} }
} }
func BenchmarkParse(b *testing.B) {
for i := 0; i < b.N; i++ {
uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
if uuid == nil {
b.Fatal("invalid uuid")
}
}
}
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
New()
}
}
func BenchmarkUUID_String(b *testing.B) {
uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
if uuid == nil {
b.Fatal("invalid uuid")
}
for i := 0; i < b.N; i++ {
if uuid.String() == "" {
b.Fatal("invalid uuid")
}
}
}
func BenchmarkUUID_URN(b *testing.B) {
uuid := Parse("f47ac10b-58cc-0372-8567-0e02b2c3d479")
if uuid == nil {
b.Fatal("invalid uuid")
}
for i := 0; i < b.N; i++ {
if uuid.URN() == "" {
b.Fatal("invalid uuid")
}
}
}