compare.js 1.4 KB

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