|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const readline = require('readline'); |
| 4 | +const seedrandom = require('seedrandom'); |
| 5 | + |
| 6 | + |
| 7 | +let RNG; |
| 8 | + |
| 9 | +class GPSData { |
| 10 | + constructor(gpsTracesFilePath) { |
| 11 | + this.tracks = {}; |
| 12 | + this.coordinates = []; |
| 13 | + this.trackIds = []; |
| 14 | + this._loadGPSTraces(gpsTracesFilePath); |
| 15 | + } |
| 16 | + |
| 17 | + _loadGPSTraces(gpsTracesFilePath) { |
| 18 | + const expandedPath = path.resolve(gpsTracesFilePath); |
| 19 | + const data = fs.readFileSync(expandedPath, 'utf-8'); |
| 20 | + const lines = data.split('\n'); |
| 21 | + const headers = lines[0].split(','); |
| 22 | + |
| 23 | + const latitudeIndex = headers.indexOf('Latitude'); |
| 24 | + const longitudeIndex = headers.indexOf('Longitude'); |
| 25 | + const trackIdIndex = headers.indexOf('TrackID'); |
| 26 | + |
| 27 | + for (let i = 1; i < lines.length; i++) { |
| 28 | + if (lines[i].trim() === '') continue; |
| 29 | + const row = lines[i].split(','); |
| 30 | + |
| 31 | + const latitude = parseFloat(row[latitudeIndex]); |
| 32 | + const longitude = parseFloat(row[longitudeIndex]); |
| 33 | + const trackId = row[trackIdIndex]; |
| 34 | + |
| 35 | + const coord = [longitude, latitude]; |
| 36 | + this.coordinates.push(coord); |
| 37 | + |
| 38 | + if (!this.tracks[trackId]) { |
| 39 | + this.tracks[trackId] = []; |
| 40 | + } |
| 41 | + this.tracks[trackId].push(coord); |
| 42 | + } |
| 43 | + |
| 44 | + this.trackIds = Object.keys(this.tracks); |
| 45 | + } |
| 46 | + |
| 47 | + getRandomCoordinate() { |
| 48 | + const randomIndex = Math.floor(RNG() * this.coordinates.length); |
| 49 | + return this.coordinates[randomIndex]; |
| 50 | + } |
| 51 | + |
| 52 | + getRandomTrack() { |
| 53 | + const randomIndex = Math.floor(RNG() * this.trackIds.length); |
| 54 | + const trackId = this.trackIds[randomIndex]; |
| 55 | + return this.tracks[trackId]; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +async function runOSRMMethod(osrm, method, coordinates) { |
| 60 | + const time = await new Promise((resolve, reject) => { |
| 61 | + const startTime = process.hrtime(); |
| 62 | + osrm[method]({coordinates}, (err, result) => { |
| 63 | + if (err) { |
| 64 | + if (['NoSegment', 'NoMatch', 'NoRoute', 'NoTrips'].includes(err.message)) { |
| 65 | + resolve(null); |
| 66 | + } else { |
| 67 | + |
| 68 | + reject(err); |
| 69 | + } |
| 70 | + } else { |
| 71 | + const endTime = process.hrtime(startTime); |
| 72 | + resolve(endTime[0] + endTime[1] / 1e9); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + return time; |
| 77 | +} |
| 78 | + |
| 79 | +async function nearest(osrm, gpsData) { |
| 80 | + const times = []; |
| 81 | + for (let i = 0; i < 1000; i++) { |
| 82 | + const coord = gpsData.getRandomCoordinate(); |
| 83 | + times.push(await runOSRMMethod(osrm, 'nearest', [coord])); |
| 84 | + } |
| 85 | + return times; |
| 86 | +} |
| 87 | + |
| 88 | +async function route(osrm, gpsData) { |
| 89 | + const times = []; |
| 90 | + for (let i = 0; i < 1000; i++) { |
| 91 | + const from = gpsData.getRandomCoordinate(); |
| 92 | + const to = gpsData.getRandomCoordinate(); |
| 93 | + |
| 94 | + |
| 95 | + times.push(await runOSRMMethod(osrm, 'route', [from, to])); |
| 96 | + } |
| 97 | + return times; |
| 98 | +} |
| 99 | + |
| 100 | +async function table(osrm, gpsData) { |
| 101 | + const times = []; |
| 102 | + for (let i = 0; i < 250; i++) { |
| 103 | + const numPoints = Math.floor(RNG() * 3) + 15; |
| 104 | + const coordinates = []; |
| 105 | + for (let i = 0; i < numPoints; i++) { |
| 106 | + coordinates.push(gpsData.getRandomCoordinate()); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + times.push(await runOSRMMethod(osrm, 'table', coordinates)); |
| 111 | + } |
| 112 | + return times; |
| 113 | +} |
| 114 | + |
| 115 | +async function match(osrm, gpsData) { |
| 116 | + const times = []; |
| 117 | + for (let i = 0; i < 1000; i++) { |
| 118 | + const numPoints = Math.floor(RNG() * 50) + 50; |
| 119 | + const coordinates = gpsData.getRandomTrack().slice(0, numPoints); |
| 120 | + |
| 121 | + |
| 122 | + times.push(await runOSRMMethod(osrm, 'match', coordinates)); |
| 123 | + } |
| 124 | + return times; |
| 125 | +} |
| 126 | + |
| 127 | +async function trip(osrm, gpsData) { |
| 128 | + const times = []; |
| 129 | + for (let i = 0; i < 250; i++) { |
| 130 | + const numPoints = Math.floor(RNG() * 2) + 5; |
| 131 | + const coordinates = []; |
| 132 | + for (let i = 0; i < numPoints; i++) { |
| 133 | + coordinates.push(gpsData.getRandomCoordinate()); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + times.push(await runOSRMMethod(osrm, 'trip', coordinates)); |
| 138 | + } |
| 139 | + return times; |
| 140 | +} |
| 141 | + |
| 142 | +function bootstrapConfidenceInterval(data, numSamples = 1000, confidenceLevel = 0.95) { |
| 143 | + let means = []; |
| 144 | + let dataLength = data.length; |
| 145 | + |
| 146 | + for (let i = 0; i < numSamples; i++) { |
| 147 | + let sample = []; |
| 148 | + for (let j = 0; j < dataLength; j++) { |
| 149 | + let randomIndex = Math.floor(RNG() * dataLength); |
| 150 | + sample.push(data[randomIndex]); |
| 151 | + } |
| 152 | + let sampleMean = sample.reduce((a, b) => a + b, 0) / sample.length; |
| 153 | + means.push(sampleMean); |
| 154 | + } |
| 155 | + |
| 156 | + means.sort((a, b) => a - b); |
| 157 | + let lowerBoundIndex = Math.floor((1 - confidenceLevel) / 2 * numSamples); |
| 158 | + let upperBoundIndex = Math.floor((1 + confidenceLevel) / 2 * numSamples); |
| 159 | + let mean = means.reduce((a, b) => a + b, 0) / means.length; |
| 160 | + let lowerBound = means[lowerBoundIndex]; |
| 161 | + let upperBound = means[upperBoundIndex]; |
| 162 | + |
| 163 | + return { mean: mean, lowerBound: lowerBound, upperBound: upperBound }; |
| 164 | +} |
| 165 | + |
| 166 | +function calculateConfidenceInterval(data) { |
| 167 | + let { mean, lowerBound, upperBound } = bootstrapConfidenceInterval(data); |
| 168 | + let bestValue = Math.max(...data); |
| 169 | + let errorMargin = (upperBound - lowerBound) / 2; |
| 170 | + |
| 171 | + return { mean, errorMargin, bestValue }; |
| 172 | +} |
| 173 | + |
| 174 | +async function main() { |
| 175 | + const args = process.argv.slice(2); |
| 176 | + |
| 177 | + const {OSRM} = require(args[0]); |
| 178 | + const path = args[1]; |
| 179 | + const algorithm = args[2].toUpperCase(); |
| 180 | + const method = args[3]; |
| 181 | + const gpsTracesFilePath = args[4]; |
| 182 | + const iterations = parseInt(args[5]); |
| 183 | + |
| 184 | + const gpsData = new GPSData(gpsTracesFilePath); |
| 185 | + const osrm = new OSRM({path, algorithm}); |
| 186 | + |
| 187 | + |
| 188 | + const functions = { |
| 189 | + route: route, |
| 190 | + table: table, |
| 191 | + nearest: nearest, |
| 192 | + match: match, |
| 193 | + trip: trip |
| 194 | + }; |
| 195 | + const func = functions[method]; |
| 196 | + if (!func) { |
| 197 | + throw new Error('Unknown method'); |
| 198 | + } |
| 199 | + const allTimes = []; |
| 200 | + for (let i = 0; i < iterations; i++) { |
| 201 | + RNG = seedrandom(42); |
| 202 | + allTimes.push((await func(osrm, gpsData)).filter(t => t !== null)); |
| 203 | + } |
| 204 | + |
| 205 | + const opsPerSec = allTimes.map(times => times.length / times.reduce((a, b) => a + b, 0)); |
| 206 | + const { mean, errorMargin, bestValue } = calculateConfidenceInterval(opsPerSec); |
| 207 | + console.log(`Ops: ${mean.toFixed(1)} ± ${errorMargin.toFixed(1)} ops/s. Best: ${bestValue.toFixed(1)} ops/s`); |
| 208 | + |
| 209 | +} |
| 210 | + |
| 211 | +main(); |
0 commit comments