Created
May 27, 2015 08:32
-
-
Save bearice/e3149a8d2fc7a2de961d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
request = require 'request' | |
rp = require 'request-promise' | |
errors = require 'request-promise/errors' | |
JSONStream = require 'JSONStream' | |
http = require 'http' | |
cluster = require 'cluster' | |
http.globalAgent = new http.Agent | |
keepAlive: false | |
maxSockets: Infinity | |
counter = | |
"Login":0 | |
"Confirm":0 | |
"Initial":0 | |
"DoInvest":0 | |
"ApplyInvest":0 | |
"Callback":0 | |
"Done":0 | |
"Error":0 | |
class Timer | |
constructor: (@name) -> | |
@last = @start = (new Date).valueOf() | |
@ticks = {start:@start} | |
tick: (name) => | |
time = (new Date).valueOf() | |
counter[name]++ | |
if @last_name | |
counter[@last_name]-- | |
@ticks[@last_name] = time-@last_time | |
@last_name=name | |
@last_time=time | |
return this | |
toJSON: -> | |
@ticks.total = @last_time - @start | |
@ticks | |
toString: () => | |
s = ("#{k}: #{v}" for k,v of @ticks) | |
"[Timer #{@name} #{s.join ", "}]" | |
parseForm = (resp) -> | |
a = {} | |
re = /input[^>]+name=["']([^'"]+)['"][^>]+value=['"]([^'"]+)['"]/g | |
while (m = re.exec resp) isnt null | |
a[m[1]]=m[2] | |
return a | |
srv_jmb = "https://wj-05.jimubox.com" | |
#srv_cmbc = "http://111.205.207.16:18080" | |
srv_cmbc = "http://111.205.207.111:60040" | |
taskCount = 0 | |
users = fs.readFileSync('test_users.csv').toString().split('\r\n') | |
getUser = -> | |
users[Math.floor(Math.random()*users.length)] | |
jar_cache = {} | |
logger = JSONStream.stringify("","\n","") | |
logger.pipe fs.createWriteStream("post.log", flags:"a+") | |
doTest = (taskId) -> | |
u = getUser() | |
t = new Timer(taskId) | |
j = jar_cache[u] || request.jar() | |
post = (url, form) -> | |
rp( | |
method: "POST" | |
url: url | |
form: form | |
jar: j | |
time: true | |
).promise() | |
form = | |
username: u | |
password: '111111' | |
t.tick "Login" | |
p = post("#{srv_jmb}/User/Login",form) | |
p.catch( -> | |
t.tick "Confirm" | |
jar_cache[u] = j | |
form = | |
ProjectID:40330 | |
Amount:1 | |
if jar_cache[u] | |
Promise.resolve "OK" | |
else | |
post("#{srv_jmb}/Invest/Confirm?ProjectID=40330", form) | |
).then((resp)-> | |
t.tick "Initial" | |
form = | |
ProjectID:40330 | |
InvestAmount:1 | |
CouponID:'' | |
Contract:'on' | |
post("#{srv_jmb}/Invest/Initial", form) | |
).then((resp)-> | |
t.tick "ApplyInvest" | |
form = parseForm resp | |
post("#{srv_cmbc}/tradeBank/trans/investApplyByCust.html", form) | |
).then((resp)-> | |
t.tick "DoInvest" | |
form = parseForm resp | |
form['tradePwd']='1' | |
delete form['radioGroup1'] | |
delete form['button'] | |
t.orderData = form | |
post("#{srv_cmbc}/tradeBank/trans/doInvestApplyByCust.html", form) | |
).then((resp)-> | |
t.tick "Callback" | |
form = parseForm resp | |
#post("#{srv_jmb}/CmbcPay/InitiativeTender", form) | |
Promise.resolve "OK" | |
).then( -> | |
t.tick "Done" | |
taskCount-- | |
logger.write | |
user: u | |
order: t.orderData | |
time: t.toJSON() | |
).catch( (e)-> | |
t.tick "Error" | |
taskCount-- | |
delete jar_cache[u] | |
logger.write | |
user: u | |
form: form | |
order: t.orderData | |
time: t.toJSON() | |
error: e.toString() | |
error_url: e.options.url | |
) | |
if cluster.isMaster | |
cluster.fork() | |
cluster.fork() | |
cluster.on 'exit', (worker,code, signal) -> | |
cluster.fork() if code is 0 | |
else | |
i=0 | |
setInterval( -> | |
if counter["Done"] > 5000 | |
process.exit(0) | |
else if taskCount < Math.min(parseInt(process.env.TASK_COUNT || 1000),counter["Done"]+10) | |
taskCount++ | |
doTest("Task#{i++}") | |
,1) | |
setInterval( -> | |
s = ("#{k}=#{v}" for k,v of counter).join "\t" | |
console.info "Task=#{taskCount}\t#{s}" | |
,1000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment