📄 hsqltimer.java
字号:
// * computed moment of actual average periodicity// * experienced so far.// */// public String toString() {// return this.name// + "["// + "runs: " + runs + ", "// + "actual avg. period: " + getAveragePeriod()// + "]";// }// }//// static class Stats {// double min;// double max;// double pk;// double sk;// double vk;// long n;// boolean initialized;// boolean sample;//// void addDataPoint(double x) {//// double xi;// double xsi;// long nm1;//// xi = x;//// if (!initialized) {// n = 1;// pk = xi;// sk = xi;// min = xi;// max = xi;// vk = 0.0;// initialized = true;//// return;// }//// n++;//// nm1 = (n - 1);// xsi = (sk - (xi * nm1));// vk += ((xsi * xsi) / n) / nm1;// sk += xi;//// if (xi != 0) {// pk *= xi;// }//// max = Math.max(max, xi);// min = Math.min(min, xi);// }//// double getMin() {// return initialized ? min : Double.NaN;// }//// double getMax() {// return initialized ? max : Double.NaN;// }//// double getGeometricMean() {// return initialized ? Math.pow(pk, 1/(double)n) : Double.NaN;// }//// double getVariance() {//// if (!initialized) {// return Double.NaN;// }//// return sample ? (n == 1) ? Double.NaN// : (vk / (double) (n - 1))// : (vk / (double) (n));// }//// double getStdDev() {//// if (!initialized) {// return Double.NaN;// }//// return sample ? (n == 1) ? Double.NaN// : (Math.sqrt(vk// / (double) (n - 1)))// : (Math.sqrt(vk / (double) (n)));// }// }//// /**// * Runs the HsqlTimer tests.// * @param args Currently unused// */// public static void main(String[] args) {// // number of tasks to queue// int taskCount = 10;// // period, as a multiple of computed system-specific avg. sync time// double periodMultiplier = 1.4D;// // how long to run the timer, in milliseconds// long duration = 2800;//// test(taskCount, periodMultiplier, duration);// }//// /**// * Runs the HsqlTimer and java.util.Timer tests using the given// * arguments. <p>// *// * @param taskCount the number of WriteAndSync tasks to add// * @param periodMultiplier the period with with to schedule// * the tasks, as a multiple of the computed, system-specific// * average sync time.// * @param duration The number of milliseconds that the foreground// * Thread should sleep while the specified number of WriteAndSync// * tasks are running in the background thread// */// public static void test(final int taskCount,// final double periodMultiplier,// final long duration) {//// System.out.println();// System.out.println("****************************************");// System.out.println("* org.hsqldb.lib.HsqlTimer tests *");// System.out.println("****************************************");// System.out.println();//// System.out.println("Computing system-specific avg. sync time.");// System.out.println("Please wait...");//// double avgSyncTime = avgSyncTime(500, new byte[256]);// double minAvgPeriod = (taskCount * avgSyncTime);// long period = Math.round(avgSyncTime * periodMultiplier);//// System.out.println();// System.out.println("System-specific avg. sync time : " + avgSyncTime + " ms.");// System.out.println("Requested task count : " + taskCount);// System.out.println("Requested task period : " + period + " ms." );// System.out.println("Min. avg. period (0 starved) : " + minAvgPeriod + " ms." );// System.out.println("Requested test duration : " + duration + " ms.");//// if (period <= minAvgPeriod || minAvgPeriod >= duration) {// double idealAvgRuns = (duration / minAvgPeriod);//// System.out.println("Idealized avg. runs / task : " + (float)idealAvgRuns);// } else {// double remainingDuration = (duration - minAvgPeriod);// double remainingRuns = (remainingDuration / period);// double idealAvgRuns = (1D + remainingRuns);//// System.out.println("Theoretical first cycle time : " + minAvgPeriod);// System.out.println("Remaining duration : " + remainingDuration);// System.out.println("Remaining runs : " + remainingRuns);// System.out.println("Idealized avg. runs per task : " + idealAvgRuns);// System.out.println("(1 + (requested duration");// System.out.println(" - theor. first cycle time");// System.out.println(" ) / requested period)");// }//// testJavaUtilTimer(taskCount, period, duration);// testHsqlTimer(taskCount, period, duration);// }////// /**// * Runs the java.util.Timer test using the given arguments. <p>// *// * @param taskCount the number of WriteAndSync tasks to add// * @param periodMultiplier the period with with to schedule// * the tasks, as a multiple of the computed, system-specific// * average sync time.// * @param duration The number of milliseconds that the foreground// * Thread should sleep while the specified number of WriteAndSync// * tasks are running in the background thread// */// public static void testJavaUtilTimer(final int taskCount,// final long period,// final long duration) {//// System.out.println();// System.out.println("****************************************");// System.out.println("* java.util.Timer *");// System.out.println("****************************************");// System.out.println();//// WriteAndSyncTask.serial = 0;//// final java.util.Timer timer = new java.util.Timer();// final WriteAndSyncTask[] tasks = new WriteAndSyncTask[taskCount];//// for (int i = 0; i < taskCount; i++) {// tasks[i] = new WriteAndSyncTask();// timer.scheduleAtFixedRate(tasks[i], 0, period);// }//// final long start = now();//// try {// Thread.sleep(duration);// } catch (Exception e) {// e.printStackTrace();// }//// for (int i = 0; i < tasks.length; i++) {// tasks[i].cancel();// }//// timer.cancel();//// final long elapsed = now() - start;//// System.out.println("Actual test duration: " + elapsed + " ms.");// System.out.println();//// printTaskStats(tasks);// }//// /**// * Runs the HsqlTimer test using the given arguments. <p>// *// * @param taskCount the number of WriteAndSync tasks to add// * @param periodMultiplier the period with with to schedule// * the tasks, as a multiple of the computed, system-specific// * average sync time.// * @param duration The number of milliseconds that the foreground// * Thread should sleep while the specified number of WriteAndSync// * tasks are running in the background thread// */// public static void testHsqlTimer(final int taskCount,// final long period,// final long duration) {//// System.out.println();// System.out.println("****************************************");// System.out.println("* org.hsqldb.lib.HsqlTimer *");// System.out.println("****************************************");// System.out.println();//// WriteAndSyncTask.serial = 0;//// final HsqlTimer timer = new HsqlTimer();// final WriteAndSyncTask[] tasks = new WriteAndSyncTask[taskCount];// final Object[] ttasks = new Object[taskCount];//// for (int i = 0; i < taskCount; i++) {// tasks[i] = new WriteAndSyncTask();// ttasks[i] = timer.schedulePeriodicallyAfter(0, period, tasks[i], true);// }//// final long start = now();//// try {// Thread.sleep(duration);// } catch (Exception e) {// e.printStackTrace();// }//// final Thread timerThread = timer.getThread();//// for (int i = 0; i < taskCount; i++) {// timer.cancel(ttasks[i]);// }//// try {// timerThread.join();// } catch (Exception e) {// e.printStackTrace();// }//// final long elapsed = now() - start;//// System.out.println("Actual test duration: " + elapsed + " ms.");// System.out.println();//// printTaskStats(tasks);//// }//// static void printTaskStats(WriteAndSyncTask[] tasks) {// float avgTotal = 0;// int avgCount = 0;// int starved = 0;// int runs = 0;// Stats periodStats = new Stats();// Stats runStats = new Stats();//// for (int i = 0; i < tasks.length; i++) {// if (tasks[i].runs > 1) {// double avgPeriod = tasks[i].getAveragePeriod();// periodStats.addDataPoint(avgPeriod);// avgTotal += avgPeriod;// avgCount++;// }// runs += tasks[i].runs;// if (tasks[i].runs == 0) {// starved++;// }// runStats.addDataPoint(tasks[i].runs);// tasks[i].release();// }//// float periodAvg = (avgTotal / avgCount);// float periodMax = (float) periodStats.getMax();// int periodMaxCnt = 0;// float periodMin = (float) periodStats.getMin();// int periodMinCnt = 0;// float periodRange = (periodMax - periodMin);// float periodStddev = (float)periodStats.getStdDev();// float periodGMean = (float)periodStats.getGeometricMean();// float periodStddevR = (periodRange / periodStddev);//// float runsAvg = (runs / (float)tasks.length);// int runsMin = Math.round((float)runStats.getMin());// int runsMinCnt = 0;// int runsMax = Math.round((float)runStats.getMax());// int runsMaxCnt = 0;// int runsRange = (runsMax - runsMin);// float runsStddev = (float) runStats.getStdDev();// float runsGMean = (float) runStats.getGeometricMean();// float runsStddevR = (runsRange / runsStddev);//// for (int i = 0; i < tasks.length; i++) {// double avgPeriod = tasks[i].getAveragePeriod();//// if (avgPeriod == periodMin) {// periodMinCnt++;// }//// if (avgPeriod == periodMax) {// periodMaxCnt++;// }//// if (tasks[i].runs == runsMin) {// runsMinCnt++;// }//// if (tasks[i].runs == runsMax) {// runsMaxCnt++;// }// }//// System.out.println("------------------------");// System.out.println("Starved tasks (runs = 0): " + starved + " (" + ((100*starved)/tasks.length) + "%)");// System.out.println("------------------------");// System.out.println("Period :");// System.out.println("------------------------");// System.out.println("Average : " + periodAvg);// System.out.println("~Minimum (count/runs) : " + periodMin + " (" + periodMinCnt + "/" + tasks.length + ")");// System.out.println("~Maximum (count/runs) : " + periodMax + " (" + periodMaxCnt + "/" + tasks.length + ")");// System.out.println("~Range : " + periodRange);// System.out.println("Geometric mean : " + periodGMean);// System.out.println("Stddev : " + periodStddev);// System.out.println("~Range/Stddev : " + periodStddevR);// System.out.println("------------------------");// System.out.println("Runs :");// System.out.println("------------------------");// System.out.println("Average : " + runsAvg);// System.out.println("Minimum (count/runs) : " + runsMin + " (" + runsMinCnt + "/" + tasks.length + ")");// System.out.println("Maximum (count/runs) : " + runsMax + " (" + runsMaxCnt + "/" + tasks.length + ")");// System.out.println("Range : " + runsRange);// System.out.println("Geometric mean : " + runsGMean);// System.out.println("Stddev : " + runsStddev);// System.out.println("Range/Stddev : " + runsStddevR);// }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -