-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
d5fcc52
commit 47783d1
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters