Tieske / uuid

A pure Lua uuid generator (modified from a Rackspace module)
https://tieske.github.io/uuid/
Other
138 stars 50 forks source link

feat: new seeding technique when in ngx_lua #8

Closed thibaultcha closed 3 years ago

thibaultcha commented 8 years ago

I tested this with:

worker_processes auto;
error_log logs/error.log notice;
daemon on;

events {}

http {
  lua_shared_dict uuids 128m;

  init_worker_by_lua_block {
    local uuid = require "uuid"
    uuid.seed()
  }

  server {
    listen 9000;

    location / {
      content_by_lua_block {
        local uuid = require "uuid"
        local dict = ngx.shared.uuids

        local u = uuid()
        assert(dict:add(u, true))
        ngx.say(u)
      }
    }
  }
}

And hitting it with:

$ wrk -t8 -c100 -d60s http://localhost:9000

As expected, the original seeding technique results in conflicts as soon as a second worker is hit, and the conflicts are constant from this point on. With the PID technique, no conflicts occurred.

rohitjoshi commented 8 years ago

@thibaultCha @Tieske Thanks for this patch. I observed this issue and was looking for a fix. Have you done any performance analysis on this uuid generation? During my testing, it seem to be slower compared to native/c++ version.

thibaultcha commented 8 years ago

This module is not tuned for performance as much as it could be (string concatenation, asserts (with more concats), table creation, bit manipulation...) but at the same time, it is aimed for a pure Lua compatibility.

And yes, I don't think any Lua equivalent will beat C performance, especially considering that C bindings provide more complete uuid generation options (not only the random v4 one). The tradeoff of course is the need to have the dynamic library on your system. Here is my benchmark (LuaJIT is a custom one I wrote with as many optimizations as I could think of):

-- generating 10^6 uuids for each
lua uuid (this module): 1.837744ms 
LuaJIT uuid:            0.786335ms
C uuid:                 0.238516ms
FFI uuid:               0.109115ms
thibaultcha commented 8 years ago

(Forgot to add those benchmarks are on LuaJIT 2.1). I also just added the FFI binding at https://github.com/bungle/lua-resty-uuid which, as expected, is the fastest.

develCuy commented 8 years ago

This would be a great addition!

thibaultcha commented 8 years ago

@rohitjoshi Checkout https://github.com/thibaultCha/lua-resty-jit-uuid if performance is your concern (LuaJIT only) and you don't want to rely on libuuid. I believe it fills a different need and hope it won't be seen as fragmentation :)

rohitjoshi commented 8 years ago

@thibaultCha thanks a lot. This is perfect where it meets performance without requiring libuuid. Can it be auto initialized on first use?

thibaultcha commented 8 years ago

Hm yeah maybe! I will give that a try. Let's talk about it there instead to not go off-topic on this PR :)