Giter Site home page Giter Site logo

humanoid's Introduction

humanoid's People

Contributors

mallardduck avatar muer avatar robthree avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

humanoid's Issues

Feature Request: Laravel (Eloquent Model) Adapter

Would be really cool to have a Laravel adapter that provides a trait to integrate the HumanoID to Eloquent Models. I'm thinking of caching the generated ID in the database, but adding a creating method to the boot function of a model could be used to automatically generate and save a HumanoID.

The adapter could also bind the instance as a singleton in the Laravel service container to save CPU costs.

Missing from packagist?

Hey @RobThree - Is this package still something you'd like to maintain?

I haven't tried it yet, but it seems useful for a use-case I have. However when I tried to pull it in I noticed it's missing from packagist. Not sure if this was intentional or an oversight so figured I should ask. If you don't wish to publish and maintain it I may fork it and do so myself. Or I'd be happy to throw PRs your way to address getting it ready for packagist.

If you would like some help preparing and shipping a release I'd also highly suggest giving a real consideration to renaming it. Many frameworks have "UrlGenerator" features that do vastly different things than this package. I think taking inspiration from a similar library on NPM (TS) called zoo-ids might be a good fit.

Adding more variety / "randomness"

I haven't looked at the code to be honest and my math and big brain skills are lacking but it seems like the current system goes down the last word list then second last sequentially so you can tell the similar IDs because they will be

Gold-monkey
Gold-zebra
Gold-pig

Etc

Would it be computationally difficult or difficult to add a way of parsing through the list in order for each item so red-monkey, blue-zebra,pink-pig etc

Is it production ready?

Hello! Love the name! Definitely seeing the use for this, so am wondering if you consider it ready to ship to production. Cheers!

Docs Improvement: Add installation command and namespace to readme

I have to feature requests for the readme:

Add the composer installation command for easy copy and paste

composer require robthree/humanoid

Add the proper namespace in the example, had to look at the source to see that I needed the following:

use RobThree\HumanoID\HumanoIDs;

Edit: While looking at the source I found the spaceIdGenerator which I think is cooler. Something to add to the readme as well?

Bug: Handle PHP_INT_MAX overflow

Sorry for all the tickets I'm creating, but I am loving the package so far and want to help improve it :)

This bug has a pretty low chance of surfacing in actual use but the fix is simple enough so I thought I'd mention it.

Description:
Supplying a value larger than a signed 64bit integer to the create method causes a type error.

Steps to reproduce:

$generator = HumanoIDs::spaceIdGenerator();
$id = $generator->create(PHP_INT_MAX + 1);

Which throws the following error:

RobThree\HumanoID\HumanoID::create(): Argument #1 ($id) must be of type int, float given,

Expected output:
Since https://github.com/RobThree/HumanoID/blob/master/src/HumanoID.php#L116 checks for negative values and throws an InvalidArgumentException, something similar would be expected.

However, since the error happens before the check even happens I'm not sure how it would be implemented without changing the argument type. And it seems counter productive to allow floats just to throw an exception that floats are not allowed.

Benchmarks & How to properly bind a Singleton in the Laravel service container

Benchmarks using Laravel 9, PHP 8, (16 cores @ 3.60GHz)

About:

Sorry for any bad formatting. This was just a quick test I did and thought I would share :)

Test 1: Initializing generator outside the loop

$zooIdGen = RobThree\HumanoID\HumanoIDs::zooIdGenerator();
while ($i <= 1000) {
  $zooId = $zooIdGen->create($i);
  echo $zooId . '<br>';
  $i++;
}

Result: 10 000 loops took 1.3485219478607 seconds
Average: 0.13485219 milliseconds per iteration
Peak CPU utilization: ~12%

Test 2: Initializing the generator in each loop

while ($i <= 1000) {
  $zooIdGen = RobThree\HumanoID\HumanoIDs::zooIdGenerator();
  $zooId = $zooIdGen->create($i);
  echo $zooId . '<br>';
  $i++;
}

Result: Crashed after 615 iterations
Peak CPU utilazation: ~27%

Eventually finished with the following results
Result: 1000 loops took 22.121620178223 seconds
Average: 22.12162018 milliseconds per iteration

Test 3: Initializing the generator as a singleton in the service container

// In AppServiceProvider
$this->app->singleton('HumanoIDs', function ($app) {
  return new HumanoIDs($app->make(HumanoIDs::class));
});

// Benchmark
while ($i <= 10000) {
  $zooId = app('HumanoIDs')->generator->create($i);
  echo $zooId . '<br>';
  $i++;
}

Result: 10000 loops took 22.320996999741 seconds
Average: 2.2320997 milliseconds per iteration
Peak CPU utilazation: ~21%

This actually suprised me as I thought the result between test 1 and 3 would be much closer. This makes me think that I need to bind the actual generator to the service container instead.

Test 4: Initializing the generator in the service container by binding the generator method

// In AppServiceProvider
$this->app->singleton('HumanoIDGenerator', function ($app) {
  return new HumanoIDGenerator($app->make(HumanoIDGenerator::class));
});

// Intermediary class:
class HumanoIDGenerator
{
  public \RobThree\HumanoID\HumanoID $generator;
  
  public function __construct()
  {
    $this->generator = \RobThree\HumanoID\HumanoIDs::zooIdGenerator();
  }
}

// Benchmark
while ($i <= 10000) {
  $zooId = app('HumanoIDGenerator')->generator->create($i);
  echo $zooId . '<br>';
  $i++;
}

Result: 10000 loops took 1.5630679130554 seconds
Average: 0.15630679 milliseconds per iteration
Peak CPU utilazation: ~12% for a short spike

This is just a tiny tiny bit slower than the first test, which is just what I initially expected.

Conclusion

When using Laravel, make sure to bind the actual generator to the service container and not just the main class.

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.