|
// fiber_pure_test.wren - Test pure Fiber performance without I/O
|
|
|
|
foreign class Host {
|
|
foreign static signalDone()
|
|
}
|
|
|
|
var mainFiber = Fiber.new {
|
|
System.print("=== Pure Fiber Performance Test ===\n")
|
|
|
|
// Test 1: Minimal yield overhead
|
|
System.print("Test 1: Minimal Fiber.yield() cost")
|
|
var iterations = 100000
|
|
var start = System.clock
|
|
for (i in 1..iterations) {
|
|
Fiber.yield()
|
|
}
|
|
var elapsed = System.clock - start
|
|
System.print(" %(iterations) yields: %(elapsed)s")
|
|
System.print(" Rate: %(iterations/elapsed) yields/sec")
|
|
System.print(" Per yield: %(elapsed/iterations * 1000000) microseconds\n")
|
|
|
|
// Test 2: Fiber creation cost
|
|
System.print("Test 2: Fiber creation overhead")
|
|
iterations = 10000
|
|
start = System.clock
|
|
for (i in 1..iterations) {
|
|
var f = Fiber.new { }
|
|
}
|
|
elapsed = System.clock - start
|
|
System.print(" %(iterations) fiber creations: %(elapsed)s")
|
|
System.print(" Rate: %(iterations/elapsed) fibers/sec")
|
|
System.print(" Per fiber: %(elapsed/iterations * 1000) milliseconds\n")
|
|
|
|
// Test 3: Fiber with work
|
|
System.print("Test 3: Fiber with actual work")
|
|
var counter = 0
|
|
var workFiber = Fiber.new {
|
|
for (i in 1..10000) {
|
|
counter = counter + 1
|
|
if (i % 100 == 0) Fiber.yield()
|
|
}
|
|
}
|
|
|
|
start = System.clock
|
|
while (!workFiber.isDone) {
|
|
workFiber.call()
|
|
}
|
|
elapsed = System.clock - start
|
|
System.print(" Processed %(counter) items in %(elapsed)s")
|
|
System.print(" Rate: %(counter/elapsed) items/sec\n")
|
|
|
|
// Test 4: Multiple fibers cooperating
|
|
System.print("Test 4: Multiple fibers round-robin")
|
|
var fibers = []
|
|
var shared = 0
|
|
for (i in 1..100) {
|
|
fibers.add(Fiber.new {
|
|
for (j in 1..100) {
|
|
shared = shared + 1
|
|
Fiber.yield()
|
|
}
|
|
})
|
|
}
|
|
|
|
start = System.clock
|
|
var done = false
|
|
while (!done) {
|
|
done = true
|
|
for (f in fibers) {
|
|
if (!f.isDone) {
|
|
f.call()
|
|
done = false
|
|
}
|
|
}
|
|
}
|
|
elapsed = System.clock - start
|
|
System.print(" 100 fibers x 100 yields = %(shared) operations in %(elapsed)s")
|
|
System.print(" Rate: %(shared/elapsed) ops/sec\n")
|
|
|
|
// Test 5: Producer-consumer pattern
|
|
System.print("Test 5: Producer-Consumer pattern")
|
|
var queue = []
|
|
var produced = 0
|
|
var consumed = 0
|
|
|
|
var producer = Fiber.new {
|
|
for (i in 1..1000) {
|
|
queue.add(i)
|
|
produced = produced + 1
|
|
if (queue.count > 10) Fiber.yield()
|
|
}
|
|
}
|
|
|
|
var consumer = Fiber.new {
|
|
while (consumed < 1000) {
|
|
if (queue.count > 0) {
|
|
var item = queue.removeAt(0)
|
|
consumed = consumed + 1
|
|
} else {
|
|
Fiber.yield()
|
|
}
|
|
}
|
|
}
|
|
|
|
start = System.clock
|
|
while (!producer.isDone || !consumer.isDone) {
|
|
if (!producer.isDone) producer.call()
|
|
if (!consumer.isDone) consumer.call()
|
|
}
|
|
elapsed = System.clock - start
|
|
System.print(" Produced: %(produced), Consumed: %(consumed)")
|
|
System.print(" Time: %(elapsed)s")
|
|
System.print(" Rate: %(consumed/elapsed) items/sec\n")
|
|
|
|
// Test 6: Deep call stack
|
|
System.print("Test 6: Deep recursion with yields")
|
|
var depth = 0
|
|
var maxDepth = 1000
|
|
var recursive
|
|
recursive = Fiber.new {
|
|
depth = depth + 1
|
|
if (depth < maxDepth) {
|
|
Fiber.yield()
|
|
recursive.call()
|
|
}
|
|
}
|
|
|
|
start = System.clock
|
|
while (!recursive.isDone) {
|
|
recursive.call()
|
|
}
|
|
elapsed = System.clock - start
|
|
System.print(" Recursion depth %(maxDepth): %(elapsed)s")
|
|
System.print(" Per level: %(elapsed/maxDepth * 1000) milliseconds\n")
|
|
|
|
System.print("=== Analysis ===")
|
|
System.print("Based on these results, Wren fibers are:")
|
|
var yieldMicros = elapsed/iterations * 1000000
|
|
if (yieldMicros < 1) {
|
|
System.print(" EXCELLENT - Sub-microsecond yields")
|
|
} else if (yieldMicros < 10) {
|
|
System.print(" GOOD - Single-digit microsecond yields")
|
|
} else if (yieldMicros < 50) {
|
|
System.print(" MODERATE - Tens of microseconds per yield")
|
|
} else {
|
|
System.print(" SLOW - Over 50 microseconds per yield")
|
|
System.print(" This suggests FFI overhead or implementation issues")
|
|
}
|
|
|
|
Host.signalDone()
|
|
while(true) { Fiber.yield() }
|
|
}
|