Giter Club home page Giter Club logo

snowflake's Introduction

🐻‍❄️❄️ Snowflake

Easy to use Kotlin library to help you generate Twitter snowflakes asynchronously

v0.1-beta | 📜 Documentation

Snowflake is a Kotlin multiplatform library to help you generate Twitter snowflakes in a single node environment or in a clustered environment. This library was majorly inspired by @bwmarrin's Go Snowflake library.

Features

  • Distributed snowflake to help you generate snowflakes on each node with the SnowflakeNodes API.
  • kotlinx.serialization support to easily (de)serialize snowflakes.
  • Simple and usable way to generate Twitter snowflakes
  • Asynchronous support via Kotlin's coroutines

Usage

View the installation guide on how to install the library in your Kotlin project.

The formatting of the snowflake is relatively the same as the ID Format that Twitter uses.

// Defaults with node 0 and epoch of Twitter's snowflake
val snowflake = Snowflake()

val id = snowflake.generate()
// => returns [org.noelware.charted.snowflake.ID]

Benchmaking

Since generating snowflakes should be fast as possible, we provide a benchmark suite in the benchmarks folder with kotlinx.benchmark

Native

> Task :benchmarks:nativeBenchmark
Running 'main' benchmarks for 'native'
native: org.noelware.charted.snowflake.benchmarks.native.SnowflakeNativeBenchmarks.generateIds
Warm-up #0: 0.00240547 ms/op
Warm-up #1: 0.00380899 ms/op
Warm-up #2: 0.00514744 ms/op
Iteration #0: 0.00553965 ms/op
Iteration #1: 0.00633540 ms/op
Iteration #2: 0.00641139 ms/op
Iteration #3: 0.00698136 ms/op
Iteration #4: 0.00878726 ms/op
  ~ 0.00681101 ms/op ±15%

native summary:
Benchmark                              Mode  Cnt  Score   Error  Units
SnowflakeNativeBenchmarks.generateIds  avgt    5  0.007 ± 0.001  ms/op

JVM

> Task :benchmarks:jvmBenchmark
Running 'main' benchmarks for 'jvm'
jvm: org.noelware.charted.snowflake.benchmarks.jvm.SnowflakeJvmBenchmarks.generateIds

Warm-up 1: 0.001 ms/op
Warm-up 2: ≈ 10⁻⁴ ms/op
Warm-up 3: ≈ 10⁻⁴ ms/op
Warm-up 4: ≈ 10⁻⁴ ms/op
Warm-up 5: ≈ 10⁻⁴ ms/op
Iteration 1: ≈ 10⁻⁴ ms/op
Iteration 2: ≈ 10⁻⁴ ms/op
Iteration 3: ≈ 10⁻⁴ ms/op
Iteration 4: ≈ 10⁻⁴ ms/op
Iteration 5: ≈ 10⁻⁴ ms/op

Warm-up 1: 0.001 ms/op
Warm-up 2: ≈ 10⁻⁴ ms/op
Warm-up 3: ≈ 10⁻⁴ ms/op
Warm-up 4: ≈ 10⁻⁴ ms/op
Warm-up 5: ≈ 10⁻⁴ ms/op
Iteration 1: ≈ 10⁻⁴ ms/op
Iteration 2: ≈ 10⁻⁴ ms/op
Iteration 3: ≈ 10⁻⁴ ms/op
Iteration 4: ≈ 10⁻⁴ ms/op
Iteration 5: ≈ 10⁻⁴ ms/op

Warm-up 1: 0.001 ms/op
Warm-up 2: ≈ 10⁻⁴ ms/op
Warm-up 3: ≈ 10⁻⁴ ms/op
Warm-up 4: ≈ 10⁻⁴ ms/op
Warm-up 5: ≈ 10⁻⁴ ms/op
Iteration 1: ≈ 10⁻⁴ ms/op
Iteration 2: ≈ 10⁻⁴ ms/op
Iteration 3: ≈ 10⁻⁴ ms/op
Iteration 4: ≈ 10⁻⁴ ms/op
Iteration 5: ≈ 10⁻⁴ ms/op

≈ 10⁻⁴ ms/op

jvm summary:
Benchmark                           Mode  Cnt   Score    Error  Units
SnowflakeJvmBenchmarks.generateIds  avgt   15  ≈ 10⁻⁴           ms/op

Installation

To install the Snowflake library, you will need to enable Noelware's Maven repository.

Gradle (Kotlin DSL)

repositories {
    maven("https://maven.noelware.org")
    mavenCentral()
}

dependencies { 
    implementation("org.noelware.charted.snowflake:snowflake:0.1-beta")
}

Gradle (Groovy DSL)

repositories {
    maven "https://maven.noelware.org"
    mavenCentral()
}

dependencies {
    implementation "org.noelware.charted.snowflake:snowflake:0.1-beta"
}

Maven

<repositories>
    <repository>
        <url>https://maven.noelware.org</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.noelware.charted.snowflake</groupId>
        <artifactId>snowflake-jvm</artifactId>
        <version>0.1-beta</version>
        <type>pom</type>
    </dependency>
</dependencies>

License

snowflake is released under the MIT License with love by Noelware. <3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.