f4b6a3 / uuid-creator

UUID Creator is a Java library for generating Universally Unique Identifiers.
MIT License
431 stars 44 forks source link

Provide clock parameter for COMB generators #60

Closed dmurat closed 2 years ago

dmurat commented 2 years ago

For easier testing with generated UUIDs, it would be great to have getShortPrefixComb, getPrefixComb, getShortSuffixComb and getSuffixComb variants that accept java.time.Clock parameter. Tnx.

fabiolimace commented 2 years ago

I like it.

I will replace System.currentTimeMillis() with SystemClock.millis(). They have the same performance in benchmarks.

All JDK versions from 8 to 14 have the same implementation for the method SystemClock.millis() as follows:

    @Override
    public long millis() {
        return System.currentTimeMillis();
    }

List of SystemClock.millis() implementations:

fabiolimace commented 2 years ago

Now the COMB factories have constructors with a Clock parameter for tests.

public final class PrefixCombFactory extends AbstRandomBasedFactory {

    public PrefixCombFactory(Clock clock) {
        this(new DefaultRandomFunction(), clock);
    }

    public PrefixCombFactory(Random random, Clock clock) {
        this(getRandomFunction(random), clock);
    }

    public PrefixCombFactory(RandomFunction randomFunction, Clock clock) {
        super(UuidVersion.VERSION_RANDOM_BASED, randomFunction);
        this.clock = clock;
    }

The performance before and after the change is the same.

BEFORE:

Benchmark                     Mode  Cnt     Score    Error   Units
UuidCreator_PrefixComb       thrpt    5  2704,653 ± 53,146  ops/ms
UuidCreator_ShortPrefixComb  thrpt    5  2091,576 ± 51,236  ops/ms

AFTER:

Benchmark                     Mode  Cnt     Score    Error   Units
UuidCreator_PrefixComb       thrpt    5  2745,823 ± 53,917  ops/ms
UuidCreator_ShortPrefixComb  thrpt    5  2116,982 ± 27,708  ops/ms

Also added clock parameters to DefaultTimeFunction and WindowsTimeFunction for tests. The performance is the same too.

fabiolimace commented 2 years ago

Released version v4.4.1.

dmurat commented 2 years ago

Thank you, sir. You are very kind and fast :-)