Add a fast method to generate identifiers #22

This commit is contained in:
Fabio Lima 2022-10-22 15:01:18 -03:00
parent b5a7d8db95
commit ebc145f52a
5 changed files with 76 additions and 11 deletions

View File

@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file.
Nothing unreleased. Nothing unreleased.
## [5.1.0] - 2022-10-22
Add a fast method to generate identifiers. #22
## [5.0.2] - 2022-09-17 ## [5.0.2] - 2022-09-17
Rewrite docs. #21 Rewrite docs. #21
@ -298,8 +302,9 @@ Project created as an alternative Java implementation of [ULID spec](https://git
- Added `LICENSE` - Added `LICENSE`
- Added test cases - Added test cases
[unreleased]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.0.2...HEAD [unreleased]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.1.0...HEAD
[5.0.2]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.0.0...ulid-creator-5.0.2 [5.0.2]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.0.2...ulid-creator-5.1.0
[5.0.2]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.0.1...ulid-creator-5.0.2
[5.0.1]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.0.0...ulid-creator-5.0.1 [5.0.1]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-5.0.0...ulid-creator-5.0.1
[5.0.0]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-4.2.1...ulid-creator-5.0.0 [5.0.0]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-4.2.1...ulid-creator-5.0.0
[4.2.1]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-4.2.0...ulid-creator-4.2.1 [4.2.1]: https://github.com/f4b6a3/ulid-creator/compare/ulid-creator-4.2.0...ulid-creator-4.2.1

View File

@ -43,7 +43,7 @@ Add these lines to your `pom.xml`.
<dependency> <dependency>
<groupId>com.github.f4b6a3</groupId> <groupId>com.github.f4b6a3</groupId>
<artifactId>ulid-creator</artifactId> <artifactId>ulid-creator</artifactId>
<version>5.0.2</version> <version>5.1.0</version>
</dependency> </dependency>
``` ```
See more options in [maven.org](https://search.maven.org/artifact/com.github.f4b6a3/ulid-creator). See more options in [maven.org](https://search.maven.org/artifact/com.github.f4b6a3/ulid-creator).
@ -125,6 +125,14 @@ Sequence of Monotonic ULIDs:
### More Examples ### More Examples
Create a quick ULID:
```java
Ulid ulid = Ulid.fast();
```
---
Create a ULID from a canonical string (26 chars): Create a ULID from a canonical string (26 chars):
```java ```java
@ -253,16 +261,19 @@ This section shows benchmarks comparing `UlidCreator` to `UUID.randomUUID()`.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
THROUGHPUT (operations/msec) Mode Cnt Score Error Units THROUGHPUT (operations/msec) Mode Cnt Score Error Units
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
UUID_randomUUID thrpt 5 3459,889 ± 98,257 ops/ms UUID_randomUUID thrpt 5 3459,889 ± 98,257 ops/ms (1.00)
UUID_randomUUID_toString thrpt 5 3148,298 ± 159,507 ops/ms UUID_randomUUID_toString thrpt 5 3148,298 ± 159,507 ops/ms
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
UlidCreator_getUlid thrpt 5 4276,614 ± 11,069 ops/ms Ulid_fast thrpt 5 34523,147 ± 1022,114 ops/ms (9.98)
Ulid_fast_toString thrpt 5 19161,375 ± 662,563 ops/ms
- - - - - - - - - - - - - - - - - - - - - - - - - - -
UlidCreator_getUlid thrpt 5 4276,614 ± 11,069 ops/ms (1.23)
UlidCreator_getUlid_toString thrpt 5 3645,088 ± 85,478 ops/ms UlidCreator_getUlid_toString thrpt 5 3645,088 ± 85,478 ops/ms
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
UlidCreator_getMonotonicUlid thrpt 5 32921,698 ± 1286,983 ops/ms UlidCreator_getMonotonicUlid thrpt 5 32921,698 ± 1286,983 ops/ms (9.51)
UlidCreator_getMonotonicUlid_toString thrpt 5 18541,252 ± 710,281 ops/ms UlidCreator_getMonotonicUlid_toString thrpt 5 18541,252 ± 710,281 ops/ms
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
Total time: 00:02:01 Total time: 00:02:41
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
``` ```

View File

@ -37,6 +37,16 @@ public class Throughput {
return UUID.randomUUID().toString(); return UUID.randomUUID().toString();
} }
@Benchmark
public Ulid Ulid_fast() {
return Ulid.fast();
}
@Benchmark
public String Ulid_fast_toString() {
return Ulid.fast().toString();
}
@Benchmark @Benchmark
public Ulid UlidCreator_getUlid() { public Ulid UlidCreator_getUlid() {
return UlidCreator.getUlid(); return UlidCreator.getUlid();
@ -46,7 +56,7 @@ public class Throughput {
public String UlidCreator_getUlid_toString() { public String UlidCreator_getUlid_toString() {
return UlidCreator.getUlid().toString(); return UlidCreator.getUlid().toString();
} }
@Benchmark @Benchmark
public Ulid UlidCreator_getMonotonicUlid() { public Ulid UlidCreator_getMonotonicUlid() {
return UlidCreator.getMonotonicUlid(); return UlidCreator.getMonotonicUlid();

View File

@ -26,12 +26,13 @@ package com.github.f4b6a3.ulid;
import java.io.Serializable; import java.io.Serializable;
import java.time.Instant; import java.time.Instant;
import java.util.SplittableRandom;
import java.util.UUID; import java.util.UUID;
/** /**
* A class that represents ULIDs. * A class that represents ULIDs.
* <p> * <p>
* ULID is 128-bit value that has two components: * ULID is a 128-bit value that has two components:
* <ul> * <ul>
* <li><b>Time component</b>: a number of milliseconds since 1970-01-01 (Unix * <li><b>Time component</b>: a number of milliseconds since 1970-01-01 (Unix
* epoch). * epoch).
@ -237,6 +238,27 @@ public final class Ulid implements Serializable, Comparable<Ulid> {
this.lsb = long1; this.lsb = long1;
} }
/**
* Returns a fast new ULID.
* <p>
* This static method is a quick alternative to {@link UlidCreator#getUlid()}.
* <p>
* It employs {@link SplittableRandom} which works very well, although not
* cryptographically strong.
* <p>
* Security-sensitive applications that require a cryptographically secure
* pseudo-random generator should use {@link UlidCreator#getUlid()}.
*
* @return a ULID
* @see {@link SplittableRandom}
* @since 5.1.0
*/
public static Ulid fast() {
final long time = System.currentTimeMillis();
final SplittableRandom random = new SplittableRandom();
return new Ulid((time << 16) | (random.nextLong() & 0xffffL), random.nextLong());
}
/** /**
* Converts a UUID into a ULID. * Converts a UUID into a ULID.
* *
@ -677,7 +699,7 @@ public final class Ulid implements Serializable, Comparable<Ulid> {
* Compares two ULIDs as unsigned 128-bit integers. * Compares two ULIDs as unsigned 128-bit integers.
* <p> * <p>
* The first of two ULIDs is greater than the second if the most significant * The first of two ULIDs is greater than the second if the most significant
* byte in which they differ is greater for the first UUID. * byte in which they differ is greater for the first ULID.
* *
* @param that a ULID to be compared with * @param that a ULID to be compared with
* @return -1, 0 or 1 as {@code this} is less than, equal to, or greater than * @return -1, 0 or 1 as {@code this} is less than, equal to, or greater than

View File

@ -15,7 +15,7 @@ import java.util.UUID;
import org.junit.Test; import org.junit.Test;
public class UlidTest { public class UlidTest extends UlidFactoryTest {
private static final int DEFAULT_LOOP_MAX = 1_000; private static final int DEFAULT_LOOP_MAX = 1_000;
@ -577,6 +577,23 @@ public class UlidTest {
} }
} }
@Test
public void testUlidFast() {
Ulid[] list = new Ulid[DEFAULT_LOOP_MAX];
long startTime = System.currentTimeMillis();
for (int i = 0; i < DEFAULT_LOOP_MAX; i++) {
list[i] = Ulid.fast();
}
long endTime = System.currentTimeMillis();
checkNullOrInvalid(list);
checkUniqueness(list);
checkCreationTime(list, startTime, endTime);
}
public static Ulid fromString(String string) { public static Ulid fromString(String string) {
long time = 0; long time = 0;