-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.js
68 lines (56 loc) · 1.52 KB
/
usage.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
const express = require('express');
const mongoose = require('mongoose');
const autocannon = require('autocannon');
const versionPlugin = require('./index');
const CONCURRENCY = 5;
const REQUEST_AMOUNT = 100;
const MONGO_URL = 'mongodb://127.0.0.1:27017/tests';
// plugin must load before .model(), referrence: https://mongoosejs.com/docs/models.html#compiling
mongoose.plugin(versionPlugin);
const TestSchema = new mongoose.Schema({ name: String });
const Model = mongoose.model('Test', TestSchema);
async function runServer() {
await mongoose.connect(MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
const { _id } = await Model.create({ name: 'aaa' });
const app = express();
app.get('/', async function handleReq(req, res) {
const doc = await Model.findById(_id);
await Model.updateOne({ _id, __v: doc.__v }, { name: 'bbb' });
res.send('ok');
});
app.listen(3000, () => {
console.log('Using optimistic lock...');
});
}
async function runLoadTest() {
await autocannon({
url: 'http://localhost:3000',
connections: 5,
pipelining: 1,
amount: 100,
});
}
async function runResult() {
const { __v } = await Model.findOne(
{},
{ __v: 1, _id: 0 },
{ sort: { _id: -1 } },
);
console.log(`
Request amount: ${REQUEST_AMOUNT}
Concurrency: ${CONCURRENCY}
Success amount: ${__v}
`);
}
async function main() {
await runServer();
await runLoadTest();
await runResult();
process.exit(0);
}
main();