comparison big.js @ 38:44005d894d1b

Made progress display on big test more time-based.
author Atul Varma <varmaa@toolness.com>
date Tue, 14 Apr 2009 10:13:46 -0700
parents e136ddfc6175
children eba5866b6adc
comparison
equal deleted inserted replaced
37:cf2122f596c8 38:44005d894d1b
1 var MAX_WORD_LENGTH = 10; 1 var MAX_WORD_LENGTH = 10;
2 var LEXICON_SIZE = 200; 2 var LEXICON_SIZE = 200;
3 var MIN_DOCUMENT_LENGTH = 250; 3 var MIN_DOCUMENT_LENGTH = 250;
4 var MAX_DOCUMENT_LENGTH = 500; 4 var MAX_DOCUMENT_LENGTH = 500;
5 var CORPUS_SIZE = 1000; 5 var CORPUS_SIZE = 1000;
6 var UI_LOCK_LIMIT = 100;
7 var UI_BREATHE_TIME = 10;
6 8
7 // Returns a random integer between min and max 9 // Returns a random integer between min and max
8 // Using Math.round() will give you a non-uniform distribution! 10 // Using Math.round() will give you a non-uniform distribution!
9 function getRandomInt(min, max) 11 function getRandomInt(min, max)
10 { 12 {
60 } while (i - iAtStart < chunkSize && 62 } while (i - iAtStart < chunkSize &&
61 i < CORPUS_SIZE); 63 i < CORPUS_SIZE);
62 if (i == CORPUS_SIZE) 64 if (i == CORPUS_SIZE)
63 db.put(docs, cb); 65 db.put(docs, cb);
64 else 66 else
65 progress(i / CORPUS_SIZE, makeNextDocument); 67 progress("make-documents", i / CORPUS_SIZE, makeNextDocument);
66 } 68 }
67 69
68 makeNextDocument(); 70 makeNextDocument();
69 } 71 }
70 72
77 LEXICON_SIZE + " words, using a corpus of " + 79 LEXICON_SIZE + " words, using a corpus of " +
78 CORPUS_SIZE + " documents, each of which is " + 80 CORPUS_SIZE + " documents, each of which is " +
79 MIN_DOCUMENT_LENGTH + " to " + MAX_DOCUMENT_LENGTH + 81 MIN_DOCUMENT_LENGTH + " to " + MAX_DOCUMENT_LENGTH +
80 " words long."); 82 " words long.");
81 83
84 function makeProgress(func) {
85 var lastDate = new Date();
86 function progress(phase, percent, resume) {
87 var currDate = new Date();
88 if (currDate - lastDate > UI_LOCK_LIMIT) {
89 lastDate = currDate;
90 func.call(this, phase, percent);
91 window.setTimeout(resume, UI_BREATHE_TIME);
92 } else
93 window.setTimeout(resume, 0);
94 }
95
96 return progress;
97 }
98
82 function start() { 99 function start() {
83 BrowserCouch.get( 100 BrowserCouch.get(
84 "big", 101 "big",
85 function(db) { 102 function(db) {
86 if (db.getLength() == 0) { 103 if (db.getLength() == 0) {
87 db.wipe(function() { 104 db.wipe(function() {
88 makeCorpus( 105 makeCorpus(
89 db, 106 db,
90 function(percent, resume) { 107 makeProgress(
91 status.textContent = ("building new corpus (" + 108 function(phase, percent) {
92 Math.floor(percent * 100) + 109 status.textContent = ("building new corpus (" +
93 "%)"); 110 Math.floor(percent * 100) +
94 window.setTimeout(resume, 5); 111 "%)");
95 }, 112 }),
96 20, 113 25,
97 run 114 run
98 ); 115 );
99 }); 116 });
100 } else 117 } else
101 run(); 118 run();
111 var sum = 0; 128 var sum = 0;
112 for (var i = 0; i < values.length; i++) 129 for (var i = 0; i < values.length; i++)
113 sum += values[i]; 130 sum += values[i];
114 return sum; 131 return sum;
115 }, 132 },
116 chunkSize: 5, 133 chunkSize: 25,
117 progress: function(phase, percent, resume) { 134 progress: makeProgress(
118 percent = Math.floor(percent * 100); 135 function(phase, percent) {
119 var msg = phase + " (" + percent + "%)"; 136 percent = Math.floor(percent * 100);
120 status.textContent = msg; 137 var msg = phase + " (" + percent + "%)";
121 window.setTimeout(resume, 5); 138 status.textContent = msg;
122 }, 139 }),
123 finished: function(aResult) { 140 finished: function(aResult) {
124 status.textContent = "Done."; 141 status.textContent = "Done.";
125 142
126 ModuleLoader.require( 143 ModuleLoader.require(
127 "JSON", 144 "JSON",