|
| 1 | +import examples/benchmarks/runner |
| 2 | + |
| 3 | +import bytearray |
| 4 | +import char |
| 5 | +import io/error |
| 6 | +import io/filesystem |
| 7 | +import stream |
| 8 | + |
| 9 | + |
| 10 | +def emitEntry(tag: Int) { value: () => Unit / emit[Byte] }: Unit / emit[Byte] = { |
| 11 | + tag.show.fromString.each() |
| 12 | + do emit(61.toByte) |
| 13 | + value() |
| 14 | + do emit(01.toByte) |
| 15 | +} |
| 16 | + |
| 17 | +def emitOrder(): Unit / emit[Byte] = { |
| 18 | + emitEntry(8) { "FIX.4.4".fromString.each() } |
| 19 | + emitEntry(9) { "176".fromString.each() } |
| 20 | + emitEntry(35) { "D".fromString.each() } |
| 21 | + emitEntry(49) { "Sender".fromString.each() } |
| 22 | + emitEntry(56) { "Target".fromString.each() } |
| 23 | + emitEntry(34) { "123".fromString.each() } |
| 24 | + emitEntry(52) { "20250124-16:00:00.000".fromString.each() } |
| 25 | + |
| 26 | + emitEntry(11) { "12345".fromString.each() } |
| 27 | + emitEntry(55) { "AAPL".fromString.each() } |
| 28 | + emitEntry(54) { "1".fromString.each() } |
| 29 | + emitEntry(38) { "100".fromString.each() } |
| 30 | + emitEntry(40) { "2".fromString.each() } |
| 31 | + emitEntry(44) { "150.00".fromString.each() } |
| 32 | + |
| 33 | + emitEntry(10) { "123".fromString.each() } |
| 34 | +} |
| 35 | + |
| 36 | +def readTag(): Int / read[Byte] = { |
| 37 | + var tag = 0 |
| 38 | + exhaustively { |
| 39 | + val byte = do read[Byte]().toInt |
| 40 | + if (byte >= 48 && byte <= 57) { |
| 41 | + tag = tag * 10 + (byte - 48) |
| 42 | + } else { |
| 43 | + do stop() |
| 44 | + } |
| 45 | + } |
| 46 | + return tag |
| 47 | +} |
| 48 | + |
| 49 | +def readEntry { entry: Int => Unit / read[Byte] }: Unit / read[Byte] = { |
| 50 | + val tag = readTag() |
| 51 | + try { |
| 52 | + entry(tag) |
| 53 | + } with read[Byte] { |
| 54 | + resume { |
| 55 | + val byte = do read[Byte]() |
| 56 | + if (byte.toInt == 1) { |
| 57 | + do stop() |
| 58 | + } else { |
| 59 | + byte |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +def readOrder(): Unit / read[Byte] = { |
| 66 | + def assertTag(tag: Int) = { |
| 67 | + readEntry { got => |
| 68 | + if (got == tag) { |
| 69 | + exhaustively { do read[Byte](); () } |
| 70 | + } else { |
| 71 | + panic("wrong tag") |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + assertTag(8) |
| 76 | + assertTag(9) |
| 77 | + assertTag(35) |
| 78 | + assertTag(49) |
| 79 | + assertTag(56) |
| 80 | + assertTag(34) |
| 81 | + assertTag(52) |
| 82 | + |
| 83 | + assertTag(11) |
| 84 | + assertTag(55) |
| 85 | + assertTag(54) |
| 86 | + assertTag(38) |
| 87 | + assertTag(40) |
| 88 | + assertTag(44) |
| 89 | + |
| 90 | + assertTag(10) |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +def run(n: Int) = { |
| 95 | + with on[IOError].panic; |
| 96 | + |
| 97 | + val filename = "/tmp/financial_information_exchange.txt" |
| 98 | + val size = 4096 |
| 99 | + |
| 100 | + writeFile(filename) { |
| 101 | + repeat(n) { |
| 102 | + emitOrder() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + readFile(filename) { |
| 107 | + repeat(n) { |
| 108 | + readOrder() |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return 0 |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +def main() = benchmark(5){run} |
| 117 | + |
0 commit comments