forked from klmallory/artillery-engine-rediscluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (145 loc) · 4.74 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const net = require('net');
const debug = require('debug')('engine:RedisCluster');
const A = require('async');
const _ = require('lodash');
const redis = require('redis');
const redisCluster = require('redis-clustr');
const helpers = require('artillery-core/lib/engine_util');
let cluster;
function RedisClusterEngine(script, ee) {
this.script = script;
this.ee = ee;
this.helpers = helpers;
this.config = script.config;
return this;
}
RedisClusterEngine.prototype.createScenario = function createScenario(scenarioSpec, ee) {
const tasks = scenarioSpec.flow.map(rs => this.step(rs, ee));
return this.compile(tasks, scenarioSpec.flow, ee);
};
RedisClusterEngine.prototype.step = function step(rs, ee, opts) {
opts = opts || {};
let self = this;
if (rs.loop) {
let steps = _.map(rs.loop, function (rs) {
return self.step(rs, ee, opts);
});
return this.helpers.createLoopWithCount(
rs.count || -1,
steps,
{
loopValue: rs.loopValue || '$loopCount',
overValues: rs.over,
whileTrue: self.config.processor
? self.config.processor[rs.whileTrue] : undefined
});
}
if (rs.log) {
return function log(context, callback) {
return process.nextTick(function () { callback(null, context); console.log(self.helpers._renderVariables(JSON.stringify(rs.log), context.vars)); });
};
}
if (rs.think) {
return this.helpers.createThink(rs, _.get(self.config, 'defaults.think', {}));
}
if (rs.function) {
return function (context, callback) {
let func = self.config.processor[rs.function];
if (!func) {
return process.nextTick(function () { callback(null, context); });
}
return func(context, ee, function () {
return callback(null, context);
});
};
}
if (rs.send) {
return function send(context, callback) {
const command = rs.send.command.toLowerCase();
const key = rs.send.key == null ? "" : self.helpers._renderVariables(rs.send.key, context.vars);
const value = rs.send.value == null ? "" : self.helpers._renderVariables(rs.send.value, context.vars);
ee.emit('request');
const startedAt = process.hrtime();
var onError = function(err)
{
debug('Send error');
ee.emit('error', err);
console.log(err);
return callback(err, context);
};
var handleResponse = function(err, reply)
{
const endedAt = process.hrtime(startedAt);
let delta = (endedAt[0] * 1e9) + endedAt[1];
reply = reply == null ? "" : reply
if (err) {
return onError(err)
}
else {
if (command == "hget"){
console.log(key, reply);}
ee.emit('response', delta, 'response', context._uid);
debug('response');
debug(reply.toString());
return callback(null, context);
}
};
if (command == "get") {
var data = cluster.get(key, function (err, reply) {
return handleResponse(err, reply);
});
}
else if (command == "hget") {
var data = cluster.hmget(key.split(','), function (err, reply) {
return handleResponse(err, reply);
});
}
else if (command == "set") {
var data = cluster.set(key, value, function (err, reply) {
return handleResponse(err, reply);
});
}
else if (command == "hmset") {
var data = cluster.hset(key, values.split(','), function (err, reply) {
return handleResponse(err, reply);
});
}
};
}
return function (context, callback) {
return callback(null, context);
};
};
RedisClusterEngine.prototype.compile = function compile(tasks, scenarioSpec, ee) {
const self = this;
return function scenario(initialContext, callback) {
const init = function init(next) {
if (cluster == undefined)
{
var servers = [];
var hosts = self.script.config.redis.targets.split(',');
hosts.forEach(function (ele) { servers.push({ host: ele.split(':')[0], port: parseInt(ele.split(':')[1]) }); });
cluster = new redisCluster(servers);
cluster.on("error", function (err) {
return onError(err);
});
}
ee.emit('started');
return next(null, initialContext);
};
let steps = [init].concat(tasks);
A.waterfall(
steps,
function done(err, context) {
if (err) {
debug(err);
}
return callback(err, context);
});
};
};
module.exports = RedisClusterEngine;