Advertisement
Guest User

Untitled

a guest
Sep 25th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jslint node: true, plusplus: true*/
  2. 'use strict';
  3. var RAND_MAX = 9999,
  4.     users = {},
  5.     current_active = 0;
  6.  
  7. function get_random_id() {
  8.     return Math.floor(Math.random() * RAND_MAX);
  9. }
  10.  
  11. function do_logout(cb) {
  12.     var id = get_random_id();
  13.     if (users.hasOwnProperty(id)) {
  14.         delete users[id];
  15.         current_active--;   // Would fire event here on delete of key
  16.     }
  17.     cb();
  18.     process.nextTick(function () {
  19.         do_logout(cb);
  20.     });
  21. }
  22.  
  23. function do_login(cb) {
  24.     var time = +new Date(),
  25.         id = get_random_id();
  26.     if (!users.hasOwnProperty(id)) {
  27.         current_active++;   // Would fire event here on insert of key
  28.     }
  29.     users[id] = time;
  30.     cb();
  31.     process.nextTick(function () {
  32.         do_login(cb);
  33.     });
  34. }
  35.  
  36. function run_benchmark() {
  37.     var start = new Date(),
  38.         done = 0,
  39.         last = 0;
  40.     function cb(v) {
  41.         done++;
  42.     }
  43.     setInterval(function () {
  44.         var x, doneTime = +new Date() - 10000;
  45.         for (x in users) {
  46.             if (users.hasOwnProperty(x) && users[x] < doneTime) {
  47.                 delete users[x];
  48.                 current_active--;   // Would fire event here on expire
  49.             }
  50.         }
  51.     }, 500);
  52.     setInterval(function () {
  53.         console.log('Throughput:', (done - last), 'r/s, currently active:', current_active);
  54.         last = done;
  55.     }, 1000);
  56.     do_logout(cb);
  57.     do_login(cb);
  58. }
  59.  
  60. run_benchmark();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement