fork(2) download
  1. <?php
  2.  
  3. function snowflake_test($maxIds = 100)
  4. {
  5. $sequences = [];
  6. $ids = [];
  7. $start = microtime(true);
  8.  
  9. for ($i = 0; $i < $maxIds; $i++) {
  10. $timestamp = floor(floor(microtime(true) * 1000) - 1325376000000);
  11.  
  12. if (!isset($sequences[$timestamp]))
  13. $sequences[$timestamp] = 0;
  14. else
  15. $sequences[$timestamp]++;
  16.  
  17. $ids[] = snowflake($timestamp, 0, $sequences[$timestamp]);
  18. }
  19. $end = microtime(true);
  20.  
  21. $count = count($ids);
  22.  
  23. $collisions = $count - count(array_unique($ids));
  24. $lapses = 0;
  25. foreach ($ids as $i => $id) {
  26. if (isset($ids[$i+1]) && $ids[$i+1] <= $id) {
  27. $lapses++;
  28. }
  29. }
  30.  
  31. echo 'Generated ' . $count . ' IDs. It took ' . round($end - $start, 5) . 's, that\'s ' . floor($count/($end - $start)) . ' IDs/sec(don\'t take this seriously...)' . "\r\n";
  32. echo 'Found ' . $collisions . ' collisions(repeated IDs).' . "\r\n";
  33. echo 'Found ' . $lapses . ' lapses(nextId <= currentId).' . "\r\n\r\n";
  34. if ($maxIds <= 100)
  35. print_r($ids);
  36. }
  37.  
  38. function snowflake($timestamp = null, $machine = 0, $sequence = 0)
  39. {
  40. if ($timestamp === null)
  41. $timestamp = floor(floor(microtime(true) * 1000) - 1325376000000);
  42.  
  43. $hi = (int)($timestamp / pow(2, 10));
  44. $lo = (int)($timestamp * pow(2, 22));
  45.  
  46. $lo = $lo | ($machine << 12) | $sequence;
  47. $hex = pack('N2', $hi, $lo);
  48. $unpacked = unpack('H*', $hex);
  49.  
  50. $hexdec = ['0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15];
  51. $dec = 0;
  52. for ($i = strlen($unpacked[1]) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) {
  53. $factor = $hexdec[$unpacked[1][$i]];
  54. $dec = bcadd($dec, bcmul($factor, $e));
  55. }
  56. return (string)$dec;
  57. }
  58.  
  59. snowflake_test(100);
Success #stdin #stdout 0.03s 52472KB
stdin
Standard input is empty
stdout
Generated 100 IDs. It took 0.00767s, that's 13043 IDs/sec(don't take this seriously...)
Found 0 collisions(repeated IDs).
Found 0 lapses(nextId <= currentId).

Array
(
    [0] => 532260564940881920
    [1] => 532260564940881921
    [2] => 532260564940881922
    [3] => 532260564940881923
    [4] => 532260564940881924
    [5] => 532260564940881925
    [6] => 532260564940881926
    [7] => 532260564940881927
    [8] => 532260564940881928
    [9] => 532260564940881929
    [10] => 532260564940881930
    [11] => 532260564945076224
    [12] => 532260564945076225
    [13] => 532260564945076226
    [14] => 532260564945076227
    [15] => 532260564945076228
    [16] => 532260564945076229
    [17] => 532260564945076230
    [18] => 532260564945076231
    [19] => 532260564945076232
    [20] => 532260564945076233
    [21] => 532260564945076234
    [22] => 532260564945076235
    [23] => 532260564945076236
    [24] => 532260564945076237
    [25] => 532260564949270528
    [26] => 532260564949270529
    [27] => 532260564949270530
    [28] => 532260564949270531
    [29] => 532260564949270532
    [30] => 532260564949270533
    [31] => 532260564949270534
    [32] => 532260564949270535
    [33] => 532260564949270536
    [34] => 532260564949270537
    [35] => 532260564949270538
    [36] => 532260564949270539
    [37] => 532260564949270540
    [38] => 532260564953464832
    [39] => 532260564953464833
    [40] => 532260564953464834
    [41] => 532260564953464835
    [42] => 532260564953464836
    [43] => 532260564953464837
    [44] => 532260564953464838
    [45] => 532260564953464839
    [46] => 532260564953464840
    [47] => 532260564953464841
    [48] => 532260564953464842
    [49] => 532260564953464843
    [50] => 532260564953464844
    [51] => 532260564957659136
    [52] => 532260564957659137
    [53] => 532260564957659138
    [54] => 532260564957659139
    [55] => 532260564957659140
    [56] => 532260564957659141
    [57] => 532260564957659142
    [58] => 532260564957659143
    [59] => 532260564957659144
    [60] => 532260564957659145
    [61] => 532260564957659146
    [62] => 532260564957659147
    [63] => 532260564957659148
    [64] => 532260564961853440
    [65] => 532260564961853441
    [66] => 532260564961853442
    [67] => 532260564961853443
    [68] => 532260564961853444
    [69] => 532260564961853445
    [70] => 532260564961853446
    [71] => 532260564961853447
    [72] => 532260564961853448
    [73] => 532260564961853449
    [74] => 532260564961853450
    [75] => 532260564961853451
    [76] => 532260564961853452
    [77] => 532260564961853453
    [78] => 532260564966047744
    [79] => 532260564966047745
    [80] => 532260564966047746
    [81] => 532260564966047747
    [82] => 532260564966047748
    [83] => 532260564966047749
    [84] => 532260564966047750
    [85] => 532260564966047751
    [86] => 532260564966047752
    [87] => 532260564966047753
    [88] => 532260564966047754
    [89] => 532260564966047755
    [90] => 532260564966047756
    [91] => 532260564970242048
    [92] => 532260564970242049
    [93] => 532260564970242050
    [94] => 532260564970242051
    [95] => 532260564970242052
    [96] => 532260564970242053
    [97] => 532260564970242054
    [98] => 532260564970242055
    [99] => 532260564970242056
)