compare_v1.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var assert = require('assert'),
  2. nodeuuid = require('../uuid'),
  3. uuidjs = require('uuid-js'),
  4. libuuid = require('uuid').generate,
  5. util = require('util'),
  6. exec = require('child_process').exec,
  7. os = require('os');
  8. // On Mac Os X / macports there's only the ossp-uuid package that provides uuid
  9. // On Linux there's uuid-runtime which provides uuidgen
  10. var uuidCmd = os.type() === 'Darwin' ? 'uuid -1' : 'uuidgen -t';
  11. function compare(ids) {
  12. console.log(ids);
  13. for (var i = 0; i < ids.length; i++) {
  14. var id = ids[i].split('-');
  15. id = [id[2], id[1], id[0]].join('');
  16. ids[i] = id;
  17. }
  18. var sorted = ([].concat(ids)).sort();
  19. if (sorted.toString() !== ids.toString()) {
  20. console.log('Warning: sorted !== ids');
  21. } else {
  22. console.log('everything in order!');
  23. }
  24. }
  25. // Test time order of v1 uuids
  26. var ids = [];
  27. while (ids.length < 10e3) ids.push(nodeuuid.v1());
  28. var max = 10;
  29. console.log('node-uuid:');
  30. ids = [];
  31. for (var i = 0; i < max; i++) ids.push(nodeuuid.v1());
  32. compare(ids);
  33. console.log('');
  34. console.log('uuidjs:');
  35. ids = [];
  36. for (var i = 0; i < max; i++) ids.push(uuidjs.create(1).toString());
  37. compare(ids);
  38. console.log('');
  39. console.log('libuuid:');
  40. ids = [];
  41. var count = 0;
  42. var last = function() {
  43. compare(ids);
  44. }
  45. var cb = function(err, stdout, stderr) {
  46. ids.push(stdout.substring(0, stdout.length-1));
  47. count++;
  48. if (count < max) {
  49. return next();
  50. }
  51. last();
  52. };
  53. var next = function() {
  54. exec(uuidCmd, cb);
  55. };
  56. next();