# Exercise: Measure Latency
#
# Little's Law: L = λW
# Before we can apply the law, we need to measure W (latency).
#
# W = the average time an item spends in the system.
# Here, we'll time a single item through a worker:
#   send item → worker processes (sleeps 10ms) → receive result
#
# `time.now` returns a millisecond timestamp. The idiomatic pattern
# for measuring elapsed time is to take a timestamp before the work,
# take another after, and subtract the start from the end.
#
# Example:
#   time.now ...work... time.now swap i.-
#
# Your task: Receive the result and calculate elapsed time.
# The elapsed time should be at least 10 milliseconds.
# When done, delete the marker line below.

# I AM NOT DONE

: test-measure-latency ( -- )
    chan.make   # work channel
    chan.make   # results channel

    # Spawn worker: receive item, sleep 10ms, send to results
    2dup [ over chan.receive drop 10 time.sleep-ms swap chan.send drop drop ] strand.spawn drop
    drop drop

    # Record start time
    time.now

    # Send work item
    rot 1 swap chan.send drop

    # Your code here:
    # - receive the result from the results channel (the second
    #   channel on the stack) and discard the value itself
    # - take another timestamp, subtract the start, and leave only
    #   the elapsed time on the stack
    # Stack at this point: ( results start )
    drop drop 0

    # Do not edit below this line
    # Elapsed time should be at least 10ms
    10 >= test.assert
;
