-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathworkload.js
123 lines (99 loc) · 3.24 KB
/
workload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*!
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const crypto = require('crypto');
const PQueue = require('p-queue');
const random = require('lodash.random');
const timeSpan = require('time-span');
const OPERATIONS = [
'readproportion',
'updateproportion',
'scanproportion',
'insertproportion',
];
class Workload {
constructor(database, options) {
this.database = database;
this.options = options;
this.queue = new PQueue();
this.weights = [];
this.totalWeight = 0;
this.operations = [];
this.latencies = {};
this.opCounts = {};
this.totalOpCount = 0;
for (const operation of OPERATIONS) {
const weight = parseFloat(this.options.get(operation));
if (weight <= 0) {
continue;
}
const shortOpName = operation.replace('proportion', '');
this.operations.push(shortOpName);
this.latencies[shortOpName] = [];
this.totalWeight += weight;
this.weights.push(this.totalWeight);
}
}
getRandomKey() {
return this.keys[random(this.keys.length - 1)];
}
loadKeys() {
return this.database
.run(`SELECT u.id FROM ${this.options.get('table')} u`)
.then(data => data[0].map(row => row[0].value))
.then(keys => (this.keys = keys));
}
run() {
const operationCount = parseInt(this.options.get('operationcount'));
const end = timeSpan();
for (let i = 0; i < operationCount; i++) {
const randomWeight = Math.random() * this.totalWeight;
for (let j = 0; j < this.weights.length; j++) {
const weight = this.weights[j];
const operation = this.operations[j];
if (randomWeight <= weight) {
this.queue.add(() => this.runOperation(operation));
break;
}
}
}
return this.queue.onIdle().then(() => (this.duration = end()));
}
runOperation(operation) {
if (typeof this[operation] !== 'function') {
throw new Error(`unsupported operation: ${operation.type}`);
}
const end = timeSpan();
return this[operation]().then(() => this.latencies[operation].push(end()));
}
read() {
const tableName = this.options.get('table');
const id = this.getRandomKey();
const query = `SELECT u.* FROM ${tableName} u WHERE u.id="${id}"`;
return this.database.run(query, {readOnly: true});
}
update() {
const tableName = this.options.get('table');
const id = this.getRandomKey();
const field = `field${random(9)}`;
const value = crypto.randomBytes(100).toString('hex');
return this.database.runTransactionAsync(transaction => {
transaction.update(tableName, {id, [field]: value});
return transaction.commit();
});
}
}
module.exports = Workload;