|
| 1 | +const fs = require('fs'); |
| 2 | +const hdr = require('hdr-histogram-js'); |
| 3 | + |
| 4 | +function formatRow(row) { |
| 5 | + const widths = [6, 15, 14, 14, 22, 14]; |
| 6 | + return row.map((val, i) => String(val).padEnd(widths[i] || 10)).join(''); |
| 7 | +} |
| 8 | + |
| 9 | +function updateCLI( |
| 10 | + updateInterval, |
| 11 | + messageLimit, |
| 12 | + testTime, |
| 13 | + measureRTT, |
| 14 | + mode, |
| 15 | + isRunningRef, |
| 16 | + totalMessagesRef, |
| 17 | + totalConnectsRef, |
| 18 | + totalSubscribedRef, |
| 19 | + totalPublishersRef, |
| 20 | + messageRateTs, |
| 21 | + rttValues, |
| 22 | + rttArchive |
| 23 | +) { |
| 24 | + return new Promise((resolve) => { |
| 25 | + let prevTime = Date.now(); |
| 26 | + let prevMessageCount = 0; |
| 27 | + let prevConnectCount = 0; |
| 28 | + let startTime = Date.now(); |
| 29 | + let resolved = false; |
| 30 | + |
| 31 | + console.log('Starting benchmark...'); |
| 32 | + |
| 33 | + const header = ['Time', 'Total Messages', 'Message Rate', 'Connect Rate']; |
| 34 | + header.push(mode.includes('subscribe') ? 'Active Subscriptions' : 'Active Publishers'); |
| 35 | + if (measureRTT) header.push('Avg RTT (ms)'); |
| 36 | + console.log(formatRow(header)); |
| 37 | + const perSecondStats = []; |
| 38 | + |
| 39 | + const interval = setInterval(() => { |
| 40 | + const now = Date.now(); |
| 41 | + const elapsed = (now - prevTime) / 1000; |
| 42 | + |
| 43 | + const messageRate = (totalMessagesRef.value - prevMessageCount) / elapsed; |
| 44 | + const connectRate = (totalConnectsRef.value - prevConnectCount) / elapsed; |
| 45 | + |
| 46 | + if (prevMessageCount === 0 && totalMessagesRef.value !== 0) { |
| 47 | + startTime = Date.now(); |
| 48 | + } |
| 49 | + |
| 50 | + if (totalMessagesRef.value !== 0) { |
| 51 | + messageRateTs.push(messageRate); |
| 52 | + } |
| 53 | + |
| 54 | + prevMessageCount = totalMessagesRef.value; |
| 55 | + prevConnectCount = totalConnectsRef.value; |
| 56 | + prevTime = now; |
| 57 | + |
| 58 | + const metrics = [ |
| 59 | + Math.floor((now - startTime) / 1000), |
| 60 | + totalMessagesRef.value, |
| 61 | + messageRate.toFixed(2), |
| 62 | + connectRate.toFixed(2), |
| 63 | + mode.includes('subscribe') ? totalSubscribedRef.value : totalPublishersRef.value |
| 64 | + ]; |
| 65 | + |
| 66 | + let avgRttMs = null; |
| 67 | + |
| 68 | + if (measureRTT) { |
| 69 | + const tickRttValues = rttValues.splice(0); |
| 70 | + if (tickRttValues.length > 0) { |
| 71 | + const sum = tickRttValues.reduce((a, b) => a + b, 0n); |
| 72 | + const avgRtt = Number(sum) / tickRttValues.length; |
| 73 | + avgRttMs = avgRtt / 1000; |
| 74 | + metrics.push(avgRttMs.toFixed(3)); |
| 75 | + } else { |
| 76 | + metrics.push('--'); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + perSecondStats.push({ |
| 81 | + second: Math.floor((now - startTime) / 1000), |
| 82 | + messages: totalMessagesRef.value, |
| 83 | + messageRate: Number(messageRate.toFixed(2)), |
| 84 | + avgRttMs: avgRttMs !== null ? Number(avgRttMs.toFixed(3)) : null |
| 85 | + }); |
| 86 | + |
| 87 | + console.log(formatRow(metrics)); |
| 88 | + |
| 89 | + const shouldStop = |
| 90 | + (messageLimit > 0 && totalMessagesRef.value >= messageLimit) || |
| 91 | + (testTime > 0 && now - startTime >= testTime * 1000 && totalMessagesRef.value !== 0); |
| 92 | + |
| 93 | + if (shouldStop && !resolved) { |
| 94 | + resolved = true; |
| 95 | + clearInterval(interval); |
| 96 | + isRunningRef.value = false; |
| 97 | + resolve({ startTime, now, perSecondStats }); |
| 98 | + } |
| 99 | + }, updateInterval * 1000); |
| 100 | + |
| 101 | + process.on('SIGINT', () => { |
| 102 | + if (!resolved) { |
| 103 | + console.log('\nReceived Ctrl-C - shutting down'); |
| 104 | + clearInterval(interval); |
| 105 | + isRunningRef.value = false; |
| 106 | + resolved = true; |
| 107 | + resolve({ startTime, now: Date.now(), perSecondStats, sigint: true }); |
| 108 | + } |
| 109 | + }); |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +function writeFinalResults( |
| 114 | + start, |
| 115 | + end, |
| 116 | + argv, |
| 117 | + mode, |
| 118 | + totalMessages, |
| 119 | + totalSubscribed, |
| 120 | + messageRateTs, |
| 121 | + rttValues, |
| 122 | + rttArchive, |
| 123 | + perSecondStats |
| 124 | +) { |
| 125 | + const duration = (end - start) / 1000; |
| 126 | + const messageRate = totalMessages / duration; |
| 127 | + |
| 128 | + console.log('#################################################'); |
| 129 | + console.log(`Mode: ${mode}`); |
| 130 | + console.log(`Total Duration: ${duration.toFixed(6)} Seconds`); |
| 131 | + console.log(`Message Rate: ${messageRate.toFixed(6)} msg/sec`); |
| 132 | + |
| 133 | + const result = { |
| 134 | + StartTime: Math.floor(start / 1000), |
| 135 | + Duration: duration, |
| 136 | + Mode: mode, |
| 137 | + MessageRate: messageRate, |
| 138 | + TotalMessages: totalMessages, |
| 139 | + TotalSubscriptions: totalSubscribed, |
| 140 | + ChannelMin: argv['channel-minimum'], |
| 141 | + ChannelMax: argv['channel-maximum'], |
| 142 | + SubscribersPerChannel: argv['subscribers-per-channel'], |
| 143 | + MessagesPerChannel: argv['messages'], |
| 144 | + MessageRateTs: messageRateTs, |
| 145 | + OSSDistributedSlots: argv['oss-cluster-api-distribute-subscribers'], |
| 146 | + Addresses: [`${argv.host}:${argv.port}`], |
| 147 | + PerSecondStats: perSecondStats |
| 148 | + }; |
| 149 | + |
| 150 | + if (argv['measure-rtt-latency'] && !mode.includes('publish')) { |
| 151 | + const histogram = hdr.build({ |
| 152 | + lowestDiscernibleValue: 1, |
| 153 | + highestTrackableValue: 10_000_000, |
| 154 | + numberOfSignificantValueDigits: 3 |
| 155 | + }); |
| 156 | + |
| 157 | + rttArchive.forEach((rtt) => { |
| 158 | + const val = Number(rtt); |
| 159 | + if (val >= 0) histogram.recordValue(val); |
| 160 | + }); |
| 161 | + |
| 162 | + const avgRtt = histogram.mean / 1000; |
| 163 | + const p50 = histogram.getValueAtPercentile(50) / 1000; |
| 164 | + const p95 = histogram.getValueAtPercentile(95) / 1000; |
| 165 | + const p99 = histogram.getValueAtPercentile(99) / 1000; |
| 166 | + const p999 = histogram.getValueAtPercentile(99.9) / 1000; |
| 167 | + |
| 168 | + result.RTTSummary = { |
| 169 | + AvgMs: Number(avgRtt.toFixed(3)), |
| 170 | + P50Ms: Number(p50.toFixed(3)), |
| 171 | + P95Ms: Number(p95.toFixed(3)), |
| 172 | + P99Ms: Number(p99.toFixed(3)), |
| 173 | + P999Ms: Number(p999.toFixed(3)), |
| 174 | + totalCount: histogram.totalCount |
| 175 | + }; |
| 176 | + |
| 177 | + console.log(`Avg RTT ${avgRtt.toFixed(3)} ms`); |
| 178 | + console.log(`P50 RTT ${p50.toFixed(3)} ms`); |
| 179 | + console.log(`P95 RTT ${p95.toFixed(3)} ms`); |
| 180 | + console.log(`P99 RTT ${p99.toFixed(3)} ms`); |
| 181 | + console.log(`P999 RTT ${p999.toFixed(3)} ms`); |
| 182 | + console.log(`Total Messages tracked latency ${histogram.totalCount} messages`); |
| 183 | + } |
| 184 | + |
| 185 | + console.log('#################################################'); |
| 186 | + |
| 187 | + if (argv['json-out-file']) { |
| 188 | + fs.writeFileSync(argv['json-out-file'], JSON.stringify(result, null, 2)); |
| 189 | + console.log(`Results written to ${argv['json-out-file']}`); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +module.exports = { |
| 194 | + updateCLI, |
| 195 | + writeFinalResults |
| 196 | +}; |
0 commit comments