Commit e129e969 authored by Benjamin Summerton's avatar Benjamin Summerton

Trying to get the Js getTicks_internal() funciton working

parent 412591bc
......@@ -88,8 +88,17 @@ template bench*(sw: Stopwatch; body: untyped): untyped =
#== Internal Procs ==#
#====================#
when defined(macosx) or defined(js):
# This needs to choose a different getTicks_internal() function depending upon
# the target platform.
when defined(js) and not defined(nodejs):
# For browser JS
proc getTicks_internal(): Ticks =
{.emit: ["return performance.now() * 1000000;"].}
when defined(nodejs):
# For NodeJS Targets
proc getTicks_internal(): Ticks =
{.emit: ["return process.hrtime()[1];"].}
when defined(macosx):
# For OS X
from times import epochTime
......
<!DOCTYPE html>
<html>
<head>
<title>Nim Stopwatch (Browser) Test</title>
<script type="text/javascript" src="nimcache/tester.js"></script>
<style type="text/css">
</style>
</head>
<body>
</body>
</html>
......@@ -3,103 +3,118 @@ from sequtils import map
# Choose where to get the "sleep" function from
when defined(js):
proc sleep(ms: int) =
{.emit: ["function sleep(", ms, ") { var currentTime = new Date().getTime(); while (currentTime + ", ms, " >= new Date().getTime()) { } }" ].}
{.emit: ["return new Promise(resolve => setTimeout(resolve,", ms, "));"].}
else:
from os import sleep
import parseopt2
# Need the DOM for browser JS
when defined(js) and not defined(nodejs):
import dom
var
recordLaps = true
sw:Stopwatch
proc test() =
# Print a message
echo "Testing with laps ", (if recordLaps: "on." else: "off.")
# Init the stopwatch and go
sw = stopwatch(recordLaps)
echo "laps=", sw.laps(true)
echo "total=", sw.totalSecs
sw.start()
echo "laps=", sw.laps(true)
sleep(1000)
echo "total=", sw.totalSecs
sw.stop()
echo sw.secs
#echo sw.msecs
#echo sw.usecs
#echo sw.nsecs
echo "total=", sw.totalSecs
echo ""
sw.start()
sleep(500)
echo "total=", sw.totalSecs
echo sw.secs
sw.stop()
echo sw.secs
echo "total=", sw.totalSecs
echo ""
sw.start()
echo sw.secs
sleep(100)
echo sw.secs
echo sw.secs
echo sw.secs
echo "total=", sw.totalSecs
echo "lapCount=", sw.numLaps(false)
echo "lapCount=", sw.numLaps(true)
echo sw.secs
echo sw.secs
sleep(100)
sw.stop()
echo sw.secs
echo "total=", sw.totalSecs
echo ""
var sw2 = sw.clone()
echo "lapCount=", sw.numLaps
echo "L1=", sw.lap(0).secs
echo "L2=", if sw.isRecordingLaps: sw.laps()[1].secs else: 0
sw.rmLap(0)
echo "all laps=", sw.laps
echo "lapCount=", sw.numLaps
echo "----"
echo "lapCount=", sw2.numLaps
var lapsSecs = sw2.laps().map(proc(x: int64): float = secs(x))
echo "all laps(s)=", lapsSecs
echo ""
sw.reset()
echo "lapCount=", sw.numLaps
sw.start()
sleep(10)
echo "total=", sw.totalSecs
sw.restart()
echo "total=", sw.totalSecs
sleep(250)
echo "total(s)=", sw.totalSecs
echo "total(m)=", sw.totalMsecs
echo "total(u)=", sw.totalUsecs
echo "total(n)=", sw.totalNsecs
echo ""
bench(sw, sleep(1000))
echo "bench(sleep(1000))=", sw.secs
# Test with laps or no laps via an argument (non-js only)
when not defined(js):
when defined(js) and not defined(nodejs):
# Call test inside body.onload for browser JS
dom.window.onload = proc(e: dom.Event) =
test()
else:
for kind, key, val in getopt():
if (kind == cmdArgument) and (key == "nolap"):
recordLaps = false
test()
# Print a message
echo "Testing with laps ", (if recordLaps: "on." else: "off.")
# Init the stopwatch and go
sw = stopwatch(recordLaps)
echo "laps=", sw.laps(true)
echo "total=", sw.totalSecs
sw.start()
echo "laps=", sw.laps(true)
sleep(1000)
echo "total=", sw.totalSecs
sw.stop()
echo sw.secs
#echo sw.msecs
#echo sw.usecs
#echo sw.nsecs
echo "total=", sw.totalSecs
echo ""
sw.start()
sleep(500)
echo "total=", sw.totalSecs
echo sw.secs
sw.stop()
echo sw.secs
echo "total=", sw.totalSecs
echo ""
sw.start()
echo sw.secs
sleep(100)
echo sw.secs
echo sw.secs
echo sw.secs
echo "total=", sw.totalSecs
echo "lapCount=", sw.numLaps(false)
echo "lapCount=", sw.numLaps(true)
echo sw.secs
echo sw.secs
sleep(100)
sw.stop()
echo sw.secs
echo "total=", sw.totalSecs
echo ""
var sw2 = sw.clone()
echo "lapCount=", sw.numLaps
echo "L1=", sw.lap(0).secs
echo "L2=", if sw.isRecordingLaps: sw.laps()[1].secs else: 0
sw.rmLap(0)
echo "all laps=", sw.laps
echo "lapCount=", sw.numLaps
echo "----"
echo "lapCount=", sw2.numLaps
var lapsSecs = sw2.laps().map(proc(x: int64): float = secs(x))
echo "all laps(s)=", lapsSecs
echo ""
sw.reset()
echo "lapCount=", sw.numLaps
sw.start()
sleep(10)
echo "total=", sw.totalSecs
sw.restart()
echo "total=", sw.totalSecs
sleep(250)
echo "total(s)=", sw.totalSecs
echo "total(m)=", sw.totalMsecs
echo "total(u)=", sw.totalUsecs
echo "total(n)=", sw.totalNsecs
echo ""
bench(sw, sleep(1000))
echo "bench(sleep(1000))=", sw.secs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment