Last active
September 9, 2019 08:22
-
-
Save Arkiant/f116e43fbb554bfb24e78d779a3cfd0f to your computer and use it in GitHub Desktop.
Performance test to choose a uuid library
This file contains hidden or 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
| package benchuuid | |
| import ( | |
| "testing" | |
| "github.com/lithammer/shortuuid" | |
| "github.com/segmentio/ksuid" | |
| "github.com/sony/sonyflake" | |
| "github.com/google/uuid" | |
| "github.com/marstr/guid" | |
| "github.com/teris-io/shortid" | |
| ) | |
| // Performance test to choose a uuid library | |
| // ----------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| // https://github.com/google/uuid c01d7cf6-ec3f-47f0-9556-a5d6e9009a43 UUIDv4 | |
| // github.com/sony/sonyflake 20f8707d600010 ~6 bytes of time (10 ms) + 1 byte sequence + 2 bytes machine id | |
| // github.com/segmentio/ksuid 0pPKHjWprnVxGH7dEsAoXX2YQv 4 bytes of time (seconds) + 16 random bytes | |
| // https://github.com/lithammer/shortuuid dwRQAc68PhHQh4BUnrNsoS UUIDv4 or v5, encoded in a more compact way | |
| // github.com/marstr/guid f47ac10b-58cc-0372-8567-0e02b2c3d479 - | |
| // github.com/teris-io/shortid ulATDpcWgN - - | |
| // PC | |
| // ----------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| // Windows 10 Enterprise 64 bits | |
| // Processor: i5-8250 CPU @ 1.60GHz 1.80GHz | |
| // Memory RAM: 8,00GB | |
| // Results | |
| // ----------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| // BenchmarkGoogleUUID-8 5000000 281 ns/op 16 B/op 1 allocs/op | |
| // BenchmarkSonyflakeUUID-8 500 2278076 ns/op 18851 B/op 72 allocs/op | |
| // BenchmarkSegmentioUUID-8 5000000 284 ns/op 0 B/op 0 allocs/op | |
| // BenchmarkLithammerShortuuid-8 200000 6905 ns/op 2953 B/op 136 allocs/op | |
| // BenchmarkMarstrGuuid-8 3000000 540 ns/op 32 B/op 2 allocs/op | |
| // BenchmarkTerisIOShortid-8 1000000 1203 ns/op 311 B/op 11 allocs/op | |
| func BenchmarkGoogleUUID(b *testing.B) { | |
| b.ReportAllocs() | |
| for i := 0; i < b.N; i++ { | |
| _, err := uuid.NewRandom() | |
| if err != nil { | |
| b.Errorf("Error generating uuid with google/uuid: %v", err) | |
| } | |
| } | |
| } | |
| func BenchmarkTerisIOShortid(b *testing.B) { | |
| b.ReportAllocs() | |
| for i := 0; i < b.N; i++ { | |
| _, err := shortid.Generate() | |
| if err != nil { | |
| b.Errorf("Error generating uuid with google/uuid: %v", err) | |
| } | |
| } | |
| } | |
| func BenchmarkSonyflakeUUID(b *testing.B) { | |
| b.ReportAllocs() | |
| for i := 0; i < b.N; i++ { | |
| sf := sonyflake.NewSonyflake(sonyflake.Settings{}) | |
| _, err := sf.NextID() | |
| if err != nil { | |
| b.Errorf("Error generating uuid with google/uuid: %v", err) | |
| } | |
| } | |
| } | |
| func BenchmarkSegmentioUUID(b *testing.B) { | |
| b.ReportAllocs() | |
| for i := 0; i < b.N; i++ { | |
| _, err := ksuid.NewRandom() | |
| if err != nil { | |
| b.Errorf("Error generating uuid with google/uuid: %v", err) | |
| } | |
| } | |
| } | |
| func BenchmarkLithammerShortuuid(b *testing.B) { | |
| b.ReportAllocs() | |
| for i := 0; i < b.N; i++ { | |
| shortuuid.New() | |
| } | |
| } | |
| func BenchmarkMarstrGuuid(b *testing.B) { | |
| b.ReportAllocs() | |
| for i := 0; i < b.N; i++ { | |
| guid.NewGUID() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment