Skip to content

Commit

Permalink
Benchmark (#8)
Browse files Browse the repository at this point in the history
* benchmark id marshal json

* benchmark id unmarshal json

* benchmark encode with various length

* benchmark decode

* test marshal id using json

* test marshal id in struct
  • Loading branch information
indrasaputra committed Oct 11, 2020
1 parent d5fcc52 commit 47783d1
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
85 changes: 85 additions & 0 deletions hashids_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package hashids_test

import (
"testing"

"github.com/indrasaputra/hashids"
)

func BenchmarkID_MarshalJSON(b *testing.B) {
id := hashids.ID(66)
for i := 0; i < b.N; i++ {
id.MarshalJSON()
}
}

func BenchmarkID_UnmarshalJSON(b *testing.B) {
var id hashids.ID
for i := 0; i < b.N; i++ {
id.UnmarshalJSON([]byte(`"J4r0MA20No"`))
}
}

func BenchmarkHashID_Encode(b *testing.B) {
b.Run("length: 5", func(b *testing.B) {
hash, _ := hashids.NewHashID(5, "common-salt")
for i := 0; i < b.N; i++ {
hash.Encode(hashids.ID(i))
}
})

b.Run("length: 10", func(b *testing.B) {
hash, _ := hashids.NewHashID(10, "common-salt")
for i := 0; i < b.N; i++ {
hash.Encode(hashids.ID(i))
}
})

b.Run("length: 15", func(b *testing.B) {
hash, _ := hashids.NewHashID(15, "common-salt")
for i := 0; i < b.N; i++ {
hash.Encode(hashids.ID(i))
}
})

b.Run("length: 20", func(b *testing.B) {
hash, _ := hashids.NewHashID(20, "common-salt")
for i := 0; i < b.N; i++ {
hash.Encode(hashids.ID(i))
}
})
}

func BenchmarkHashID_Decode(b *testing.B) {
b.Run("length: 5", func(b *testing.B) {
hash, _ := hashids.NewHashID(5, "common-salt")
res, _ := hash.Encode(100)
for i := 0; i < b.N; i++ {
hash.Decode(res)
}
})

b.Run("length: 10", func(b *testing.B) {
hash, _ := hashids.NewHashID(10, "common-salt")
res, _ := hash.Encode(100)
for i := 0; i < b.N; i++ {
hash.Decode(res)
}
})

b.Run("length: 15", func(b *testing.B) {
hash, _ := hashids.NewHashID(15, "common-salt")
res, _ := hash.Encode(100)
for i := 0; i < b.N; i++ {
hash.Decode(res)
}
})

b.Run("length: 20", func(b *testing.B) {
hash, _ := hashids.NewHashID(20, "common-salt")
res, _ := hash.Encode(100)
for i := 0; i < b.N; i++ {
hash.Decode(res)
}
})
}
36 changes: 36 additions & 0 deletions hashids_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hashids_test

import (
"encoding/json"
"testing"

"github.com/indrasaputra/hashids"
Expand Down Expand Up @@ -98,6 +99,41 @@ func TestID_UnmarshalJSON(t *testing.T) {
})
}

func TestID_MarshalAndUnmarshal(t *testing.T) {
t.Run("ID gets back to original ID when unmarshal after marshal", func(t *testing.T) {
ids := []hashids.ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200, 1000, 10000, 100000, 1000000, 10000000, 100000000}
for _, id := range ids {
res, err := json.Marshal(id)
assert.Nil(t, err)

var tmp hashids.ID
err = json.Unmarshal(res, &tmp)
assert.Nil(t, err)
assert.Equal(t, id, tmp)
}
})

t.Run("ID on struct gets back to original ID when unmarshal after marshal", func(t *testing.T) {
type Product struct {
ID hashids.ID `json:"id"`
Name string `json:"name"`
}

ids := []hashids.ID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200, 1000, 10000, 100000, 1000000, 10000000, 100000000}
for _, id := range ids {
prod := Product{id, "product's name"}
res, err := json.Marshal(prod)
assert.Nil(t, err)

var tmp Product
err = json.Unmarshal(res, &tmp)
assert.Nil(t, err)
assert.Equal(t, id, tmp.ID)
assert.Equal(t, prod, tmp)
}
})
}

func TestNewHashID(t *testing.T) {
t.Run("successfully create an instance of HashID", func(t *testing.T) {
tables := []struct {
Expand Down

0 comments on commit 47783d1

Please sign in to comment.