|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const grpc = require('grpc'); |
| 16 | +const protoLoader = require('@grpc/proto-loader'); |
| 17 | +const {Spanner} = require('../build/src'); |
| 18 | + |
| 19 | +const argv = require('yargs') |
| 20 | + .option('port', { |
| 21 | + description: 'The port that the Node.js benchwrapper should run on.', |
| 22 | + type: 'number', |
| 23 | + demand: true, |
| 24 | + }) |
| 25 | + .parse(); |
| 26 | + |
| 27 | +const PROTO_PATH = __dirname + '/spanner.proto'; |
| 28 | +const packageDefinition = protoLoader.loadSync(PROTO_PATH, { |
| 29 | + keepCase: true, |
| 30 | + longs: String, |
| 31 | + enums: String, |
| 32 | + defaults: true, |
| 33 | + oneofs: true, |
| 34 | +}); |
| 35 | +const protoDescriptor = grpc.loadPackageDefinition(packageDefinition); |
| 36 | +const spannerBenchWrapper = protoDescriptor.spanner_bench; |
| 37 | + |
| 38 | +// The benchwrapper should only be executed against an emulator. |
| 39 | +if (!process.env.SPANNER_EMULATOR_HOST) { |
| 40 | + throw new Error( |
| 41 | + 'This benchmarking server only works when connected to an emulator. Please set SPANNER_EMULATOR_HOST.' |
| 42 | + ); |
| 43 | +} |
| 44 | +// This will connect the Spanner client to an emulator, as SPANNER_EMULATOR_HOST has been set. |
| 45 | +const spannerClient = new Spanner(); |
| 46 | + |
| 47 | +// Implementation of SpannerBenchWrapper.Read method. |
| 48 | +function Read(call, callback) { |
| 49 | + const instance = spannerClient.instance('someinstance'); |
| 50 | + const database = instance.database('somedatabase'); |
| 51 | + let tx; |
| 52 | + database |
| 53 | + .getSnapshot() |
| 54 | + .then(data => { |
| 55 | + tx = data[0]; |
| 56 | + return tx.run(call.request.Query); |
| 57 | + }) |
| 58 | + .then(data => { |
| 59 | + const [rows] = data; |
| 60 | + // Just iterate over all rows. |
| 61 | + rows.forEach(() => {}); |
| 62 | + }) |
| 63 | + .finally(() => { |
| 64 | + if (tx) { |
| 65 | + tx.end(); |
| 66 | + } |
| 67 | + callback(null, {}); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +// Implementation of SpannerBenchWrapper.Insert method. |
| 72 | +function Insert(call, callback) { |
| 73 | + const instance = spannerClient.instance('someinstance'); |
| 74 | + const database = instance.database('somedatabase'); |
| 75 | + database.runTransaction(function(err, transaction) { |
| 76 | + if (err) { |
| 77 | + callback(err); |
| 78 | + return; |
| 79 | + } |
| 80 | + call.request.users.forEach(user => { |
| 81 | + transaction.insert('sometable', { |
| 82 | + name: user.name, |
| 83 | + age: user.age, |
| 84 | + }); |
| 85 | + }); |
| 86 | + transaction.commit(function(err) { |
| 87 | + if (err) { |
| 88 | + callback(err); |
| 89 | + } else { |
| 90 | + callback(null, {}); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +// Implementation of SpannerBenchWrapper.Insert method. |
| 97 | +function Update(call, callback) { |
| 98 | + const instance = spannerClient.instance('someinstance'); |
| 99 | + const database = instance.database('somedatabase'); |
| 100 | + database.runTransaction((err, transaction) => { |
| 101 | + if (err) { |
| 102 | + callback(err); |
| 103 | + return; |
| 104 | + } |
| 105 | + transaction.batchUpdate(call.request.Queries, (err, rowCounts) => { |
| 106 | + if (err) { |
| 107 | + callback( |
| 108 | + new grpc.StatusBuilder() |
| 109 | + .withCode(err.code) |
| 110 | + .withDetails(err.details || err.message) |
| 111 | + .withMetadata(err.metadata) |
| 112 | + .build() |
| 113 | + ); |
| 114 | + transaction.rollback().then(() => {}); |
| 115 | + return; |
| 116 | + } |
| 117 | + // Iterate over all rowCounts. |
| 118 | + rowCounts.forEach(() => {}); |
| 119 | + transaction.commit(function(err) { |
| 120 | + if (err) { |
| 121 | + callback(err); |
| 122 | + } else { |
| 123 | + callback(null, {}); |
| 124 | + } |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +// Create and start a benchwrapper server. |
| 131 | +const server = new grpc.Server(); |
| 132 | +server.addService(spannerBenchWrapper['SpannerBenchWrapper']['service'], { |
| 133 | + Read: Read, |
| 134 | + Insert: Insert, |
| 135 | + Update: Update, |
| 136 | +}); |
| 137 | +console.log('starting benchwrapper for Spanner on localhost:' + argv.port); |
| 138 | +server.bind('0.0.0.0:' + argv.port, grpc.ServerCredentials.createInsecure()); |
| 139 | +server.start(); |
0 commit comments