-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeapiBench_new.groovy
executable file
·780 lines (655 loc) · 34 KB
/
BeapiBench_new.groovy
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#!/usr/bin/env groovy
/*
* Copyright 2013-2019 Beapi.io
*
* Licensed under the MPL-2.0 License;
* you may not use this file except in compliance with the License.
*
* 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.
*/
/*
First one must authenticate:
./BeapiBench_new.groovy
-c=./cookies.txt
-H="Content-Type: application/json"
--method=POST
-a='{"username":"admin","password":"@6m!nP@s5"}'
--endpoint=http://localhost:8080/authenticate
Then (once we have cookie and token) we make our api call with the token
(note: cookie is gotten from cookie file)
./BeapiBench_new.groovy
-T='JSESSIONID':'A301DC7D052980C009BEFF349DF786D0'
--token=eyJvcmlnaW4iOiIxMjcuMC4wLjEiLCJicm93c2VyIjoiVW5rbm93biIsIm9zIjoiVW5rbm93biIsImFsZyI6IkhTNTEyIn0.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTczMzMzMjExMSwiaWF0IjoxNzMzMjQ1NzExfQ.y8PiB-1zOLxNVIDEcJDqRC3IU-Dv2pDGj6AH1lqk2tdtF6R2yfNB15kM4EzA4ZRzqx80UHhayo0fGxdsZ517HQ
--concurrency=1000
--requests=3000
--testnum=50
--method=GET
--endpoint=http://localhost:8080/v1.0/user/show?id%3Dtest
*/
@Grab(group='commons-cli', module='commons-cli', version='1.4')
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.14')
@Grab(group='commons-io', module='commons-io', version='2.8.0')
import groovy.cli.commons.*;
import groovy.cli.picocli.*
import groovy.json.JsonSlurper
import java.text.DecimalFormat
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets
import java.text.DecimalFormat
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse.PushPromiseHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher
import org.apache.http.protocol.BasicHttpContext
import org.apache.http.protocol.HttpContext
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.impl.client.DefaultHttpClient
//import org.apache.http.client.HttpClient
import org.apache.commons.io.IOUtils
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.HttpResponse
import org.apache.http.HttpEntity
import org.apache.http.message.BasicHeader
import org.apache.http.entity.StringEntity
import org.apache.http.entity.ByteArrayEntity
import org.apache.http.entity.ContentType
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import java.nio.charset.StandardCharsets
import org.apache.http.cookie.Cookie;
import org.apache.http.protocol.BasicHttpContext
import org.apache.http.protocol.HttpContext
import org.apache.http.client.methods.*
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.CookieStore
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.BasicCookieStore
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
class BeapiBench {
private List methods = ['GET', 'PUT', 'POST', 'DELETE']
protected String method
private List graphTypes = ['TIME', 'TOTALTIME', 'IO', 'SUCCESS_FAIL', 'ALL']
private LinkedHashMap graphDesc = [
'TIME' : """The <b>Time Chart</b> is a measure of 3 separate variables: the Requests Per Second or 'RPS' (Y-coord), Number of seconds each test took (X-coord) and Number of milliseconds each request took (Point Label). This is meant to show ramp up from start highest output as well as what occurs with processor when you stay at high output for too long and where the processor/application is most 'comfortable'.""",
'TOTALTIME' : "",
'IO' : "",
'SUCCESS_FAIL': ""
]
protected String graphType = 'ALL'
protected String endpoint
private static final int NUM_THREADS = 50; // Number of concurrent threads
private static final int NUM_REQUESTS = 1000; // Number of total requests
protected Integer concurrency = 50
protected Integer requests = 1000
protected String token = "eyJvcmlnaW4iOiIxMjcuMC4wLjEiLCJicm93c2VyIjoiVW5rbm93biIsIm9zIjoiVW5rbm93biIsImFsZyI6IkhTNTEyIn0.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTczMDM5MzM3NCwiaWF0IjoxNzMwMzA2OTc0fQ.MVBaKNhAVmN6BIJS0cG9prdgt2HjatbEoLDKUHmCLzqggV6pxeyHL55euPC7a94Qqax0DUDFyeHKakqX1KcgZw";
protected List headers
protected String contentType = 'application/json'
protected String path = '/tmp/'
protected String filename = 'beapiBench.txt'
protected String tmpPath = path + filename
boolean noHardcore = true
Float totalTime
Integer testSize = 50
String postData
String bytes
static void main(String[] args) {
CommandLineInterface cli = CommandLineInterface.INSTANCE
cli.parse(args)
}
/*
static void main(String[] args) {
//CommandLineInterface cli = CommandLineInterface.INSTANCE
//cli.parse(args)
String targetUrl = "http://localhost:8080/v1.0/user/show?id%3Dtest"; // Target URL
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_REQUESTS; i++) {
executor.submit(() -> {
try {
sendRequest(targetUrl,"eyJvcmlnaW4iOiIxMjcuMC4wLjEiLCJicm93c2VyIjoiVW5rbm93biIsIm9zIjoiVW5rbm93biIsImFsZyI6IkhTNTEyIn0.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTczMDQwMTA0OSwiaWF0IjoxNzMwMzE0NjQ5fQ.f6HJcSjCWKzFDnF2DmHnnjemCEBxWCBcRxY7tP3mykyCtZ-9y1hQraC4C7_NE8m2tiRIYzbUI3gQPaizs6eYlw")
} catch (Exception e) {
e.printStackTrace();
}
});
}
executor.shutdown();
}
*/
enum CommandLineInterface{
INSTANCE
//HttpClient httpClient = new DefaultHttpClient();
private List methods = ['GET', 'PUT', 'POST', 'DELETE']
protected String method
private List graphTypes = ['TIME', 'TOTALTIME', 'IO', 'SUCCESS_FAIL', 'ALL']
protected String graphType = 'ALL'
protected String endpoint
protected Integer concurrency = 50
protected Integer requests = 1000
protected String resource
protected Integer transferred
protected String token
protected List headers = []
protected String contentType = 'application/json'
protected Map<String, String> cookie = [:]
protected BasicClientCookie cookieStorage
protected String path = '/tmp/'
protected String filename = 'beapiBench.txt'
protected String tmpPath = path + filename
boolean noHardcore = true
Float totalTime
Integer testSize = 50
String postData
String authData
String protocol = 'http2'
CliBuilder cliBuilder
CommandLineInterface() {
cliBuilder = new CliBuilder(
usage: 'BeapiBench [<options>] -m=method --endpoint=url',
header: 'OPTIONS:',
footer: "BeapiBench is a tool for benchmarking and graphing api's. It requires that both ApacheBench (ab) and gnuplot be preinstalled and available to run. Please make sure these are available and installed via your repository. If you have any questions, please visit us a http://beapi.io. Thanks again."
)
cliBuilder.width = 80
cliBuilder.with {
// HELP OPT
h(longOpt: 'help', 'Print this help text and exit (usage: -h, --help)')
// FORCE OPT
f(longOpt: 'force', 'Force run without checking for dependencies')
// REQUIRED TEST OPTS
m(longOpt: 'method', args: 2, valueSeparator: '=', argName: 'property=value', 'request method for endpoint (GET/PUT/POST/DELETE)')
_(longOpt: 'endpoint', args: 2, valueSeparator: '=', argName: 'property=value', 'url for making the api call (usage: --endpoint=http://localhost:8080)')
_(longOpt: 'testnum', args: 2, valueSeparator: '=', argName: 'property=value', 'number of tests to run; defaults to 50 (usage: --testNum=100)')
_(longOpt: 'hardcore', 'No pause between tests')
g(longOpt: 'graphtype', args: 2, valueSeparator: '=', argName: 'property=value', 'type of graph to create: [TIME, TOTALTIME, IO, SUCCESS_FAIL, ALL]; defaults to TESTTIME (usage: -g TOTALTIME)')
// OPTIONAL TEST OPTS
d(longOpt: 'data', args: 2, valueSeparator: ' ', argName: 'property=value', 'value for concurrent users per test run (usage: -c 50, --concurrency=50)')
c(longOpt: 'cookie', args: 2, valueSeparator: '=', argName: 'property=value', 'value for concurrent users per test run (usage: -c 50, --concurrency=50)')
C(longOpt: 'concurrency', args: 2, valueSeparator: '=', argName: 'property=value', 'value for concurrent users per test run (usage: -c 50, --concurrency=50)')
T(longOpt: 'cookie',args: 2, argName: 'property=value', 'key/val pair for passing cookie value (ie -C=\'JSESSIONID\':\'DDADD5351AD3DCAE8906F3C2FDFB8A93\')')
n(longOpt: 'requests', args: 2, valueSeparator: '=', argName: 'property=value', 'requests to make per test run (usage: -n 1000, --requests=1000)')
t(longOpt: 'token', args: 2, valueSeparator: '=', argName: 'property:value', 'JWT bearer token (usage: -t wer4t56g356g356h35h, --token=wer4t56g356g356h35h)')
H(longOpt: 'header', args: 2, valueSeparator: '=', argName: 'property=value', 'optional header to pass (usage: -H <header>, --header=<header>)')
j(longOpt: 'contenttype', args: 2, valueSeparator: '=', argName: 'property=value', "content-type header; defaults to 'application/json' (usage: -c application/xml, --contenttype=application-xml)")
p(longOpt: 'postData', args: 2, valueSeparator: '=', argName: 'property=value', 'txt file supplying POST data (usage: -p post.txt )')
// new_functionality
a(longOpt: 'auth', args: 2, valueSeparator: '=', argName: 'property=value', 'token authentication handshake(usage: -i={"username":"admin","password":"@6m!nP@s5"} )')
// GRAPH OPTS
}
}
void parse(args) {
OptionAccessor options = cliBuilder.parse(args)
try {
if (!options.f) {
/*
// Apache-Utils
String abChk = "dpkg -s ab &> /dev/null | echo \$?"
def proc2 = ['bash', '-c', abChk].execute()
proc2.waitFor()
def outputStream2 = new StringBuffer()
def error2 = new StringWriter()
proc2.waitForProcessOutput(outputStream2, error2)
String output2 = outputStream2.toString()
switch (output2) {
case '0':
break;
case '1':
default:
throw new Exception('Apache-Utils not installed. Please install via your local repositorys or use -f (--force) to force run.')
}
String gnuChk = "dpkg -s gnuplot &> /dev/null | echo \$?"
def proc3 = ['bash', '-c', gnuChk].execute()
proc3.waitFor()
def outputStream3 = new StringBuffer()
def error3 = new StringWriter()
proc3.waitForProcessOutput(outputStream3, error3)
String output3 = outputStream3.toString()
switch (output3) {
case '0':
break;
case '1':
default:
throw new Exception('Gnuplot not installed. Please install via your local repositorys or use --f (--force) to force run.')
}
*/
}
// NO ARGS
if (!args) {
throw new Exception('Could not parse command line options.\n')
}
// HELP
if (options.h) {
cliBuilder.usage()
System.exit 0
}
if (options.m || options.method){
if(options.endpoint) {
if (!methods.contains(options.m.toUpperCase())) {
throw new Exception('Request method not supported. Please try again.\n')
}
this.method = options.m
try {
URL url = new URL(options.endpoint)
} catch (Exception e) {
throw new Exception('Endpoint is not a valid URL. Please try again', e)
}
this.endpoint = options.endpoint
}else{
throw new Exception('Endpoint(--endpoint) is REQUIRED FLAGS for BeapiBench to work. Please try again.\n')
}
}else{
throw new Exception('Method(--method) is REQUIRED FLAGS for BeapiBench to work. Please try again.\n')
}
// --AUTH
if (options.a){
if(options.m && options.endpoint) {
this.authData = new groovy.json.JsonSlurper().parseText(options.a.trim())
try {
URL url = new URL(options.endpoint)
} catch (Exception e) {
throw new Exception('Endpoint is not a valid URL. Please try again', e)
}
this.endpoint = options.endpoint
}else{
throw new Exception('Authentication requires an endpoint(--endpoint) and method(-m).\n')
}
}
// --COOKIE
if (options.c){
def temp = options.c
this.authData = new groovy.json.JsonSlurper().parseText(options.a.trim())
try {
URL url = new URL(options.endpoint)
} catch (Exception e) {
throw new Exception('Endpoint is not a valid URL. Please try again', e)
}
this.endpoint = options.endpoint
}
// --CONCURRENCY
if (options.C) {
try {
this.concurrency = options.C as Integer
if (!(this.concurrency > 0)) {
throw new Exception('Concurrency must be a positive number greater than 0. Please try again.')
}
} catch (Exception e) {
throw new Exception('Concurrency (--concurrency ,-c) expects an Unsigned Integer greater than 0. Please try again.', e)
}
}
if (options.T) {
try {
List temp = options.T.split(':')
this.cookie[temp[0]] = temp[1]
} catch (Exception e) {
println(e)
throw new Exception('Cookie requires a key/value pair', e)
}
}
if (options.n) {
try {
this.requests = options.n as Integer
if (!(this.requests > 0)) {
throw new Exception('Number of requests must be a positive number greater than 0. Please try again.')
}
} catch (Exception e) {
throw new Exception('Requests (--request, -n) expects an Unsigned Integer greater than 0. Please try again.', e)
}
}
if (this.concurrency >= this.requests) {
throw new Exception('Concurrency must be less than number of requests. Please try again')
}
if (options.t) {
this.token = options.t.trim()
}
if (options.H) {
options.H.each() {
this.headers.add(it.trim())
}
}
if (options.p) {
this.postData = options.p.trim()
}
if (options.j) {
this.contentType = options.j.trim()
}
if (options.hardcore) {
this.noHardcore = false
}
if (options.testnum) {
try {
Integer temp = options.testnum as Integer
if (!(temp > 0)) {
throw new Exception('Testnum (--testnum)must be a positive number greater than 0. Please try again.')
}
this.testSize = temp
} catch (Exception e) {
throw new Exception('Testnum (--testnum) expects an Unsigned Integer greater than 0. Please try again.', e)
}
}
if (options.g) {
if (!this.graphTypes.contains(options.g)) {
throw new Exception("GraphType (--graphtype, -g) must match one of existing GraphTypes: [${this.graphTypes}]. Please try again.")
}
this.graphType = options.g
}
} catch (Exception e) {
System.err << e
System.exit 1
}
int i = 0
List data = []
List rps = []
List rng = []
Float avgNum = 0.0
Float maxNum = 0.0
// PROCESS
Properties props = readProperties()
if(options.a){
// require content-type
// require endpoint
//require method
// no concurrency; if has concurrency then 'fail'
// no requestNum; if has requestNum then 'fail'
//
if(props.hasProperty("token")){
println("### resetting current token : "+props.token)
}
if(this.authData){
println("### auth data : "+this.authData)
}
HttpResponse response = sendAuthRequest(this.authData, this.contentType, 'POST' , this.endpoint, this.headers)
int statusCode = response.statusCode()
println("Status Code: " + statusCode);
//String responseBody = IOUtils.toString(response.stream(), StandardCharsets.UTF_8);
String responseBody = response.body()
println("Response: " + responseBody);
println("need to set cookie...")
}else {
while (i < this.testSize) {
// call method; move this to method
print("[TEST ${i + 1} of ${this.testSize}] : ")
List returnData = callApi(this.postData, this.concurrency, this.requests, this.contentType, this.token, this.method, this.endpoint, this.headers, this.cookie)
rps.add(Float.parseFloat(returnData[2]))
rng.add(Float.parseFloat(returnData[1]))
if (!this.resource) {
this.resource = returnData[0] + " bytes"
}
if (!this.transferred) {
this.transferred = ((Integer) Float.parseFloat(returnData[5])) / this.requests
}
if (!returnData.isEmpty()) {
DecimalFormat df = new DecimalFormat("0.00")
if (data.size() > 0) {
int size = data.size() - 1
// TODO: will fail here if tests fail; need to create a way to continue; test returnData
//try {
Float floatTemp1 = Float.parseFloat(returnData[1])
Float floatTemp2 = Float.parseFloat(data[size][1])
Float floatTemp3 = Float.sum(floatTemp1, floatTemp2)
List temp = [returnData[1], df.format(floatTemp3), returnData[2], df.format(Float.parseFloat(returnData[3])), df.format(Float.parseFloat(returnData[4])), df.format(Float.parseFloat(returnData[5])), df.format(Float.parseFloat(returnData[6])), df.format(Float.parseFloat(returnData[7])), df.format(Float.parseFloat(returnData[8])), returnData[9], returnData[10], returnData[11], returnData[12]]
data.add(temp)
this.totalTime = floatTemp3
//}catch(Exception e){
// println("${returnData} :" +e)
//}
} else {
// time/totaltime/rps
List temp = [returnData[1], returnData[1], returnData[2], returnData[3], returnData[4], returnData[5], returnData[6], returnData[7], returnData[8], returnData[9], returnData[10], returnData[11], returnData[12]]
data.add(temp)
}
} else {
println(" TEST FAILED. Set concurrency/requests lower or change server config.")
}
if (this.noHardcore) {
float waitTime = Float.parseFloat(returnData[1])
waitTime = ((waitTime * 1000) * 1.8)
sleep(waitTime as Integer)
}
i++
}
List range = [rng.min(), rng.max()]
avgNum = rps.sum() / rps.size()
maxNum = rps.max()
Float avgMaxMedian = avgNum + maxNum / 2
// CREATE DATA FILE
File apiBenchData = new File("${this.tmpPath}")
if (apiBenchData.exists() && apiBenchData.canRead()) {
apiBenchData.delete()
}
apiBenchData.append('# doc sum time rps success fail data html tpr transferrate connect processing waiting ttime\n')
i = 1
data.each() {
apiBenchData.append "${i} "
apiBenchData.append it.join(' ')
apiBenchData.append '\n'
i++
}
// REMOVE PREVIOUS GRAPHS
1..4.each() {
def file = new File("beapi_chart${it}.png")
if (file.exists() && file.canRead()) {
file.delete()
}
}
}
// CREATE GRAPH
/*
String title = "${this.concurrency} c / ${this.requests} n / ${this.testSize} tests}"
if(this.graphType!='ALL') {
//println("[tmp gnuplot file] >> "+this.tmpPath)
createChart(this.graphType,"${title}", avgNum, avgMaxMedian, range)
}else{
//println("[tmp gnuplot file] >> "+this.tmpPath)
this.graphTypes.each(){
if(it!='ALL'){
createChart(it,title, avgNum, maxNum, range)
}
}
}
createFile()
*/
}
// TODO
protected testConnection() {
}
protected Properties readProperties(){
Properties properties = new Properties();
this.getClass().getResource( './beapi_bench.properties' ).withInputStream {
properties.load(it)
}
// println(properties.token)
return properties
}
protected List callApi(String postData, Integer concurrency, Integer requests, String contentType, String token, String method, String endpoint, List headers, Map cookie) {
String bench = "ab -c ${concurrency} -n ${requests}"
if (cookie) {
bench += " -C ${cookie.key}=${cookie.value}"
}
if (postData) {
bench += " -p ${this.postData}"
}
if (contentType) {
bench += " -H 'Content-Type: ${contentType}'"
}
if (token) {
bench += " -H 'Authorization: Bearer ${token}'"
}
if (headers) {
headers.each() {
bench += " -H '${it}'"
}
}
bench += " -m ${method} ${endpoint}"
def proc = ['bash', '-c', bench].execute()
proc.waitFor()
DecimalFormat df = new DecimalFormat("0.00")
def outputStream = new StringBuffer()
def error = new StringWriter()
proc.waitForProcessOutput(outputStream, error)
String output = outputStream.toString()
List<String> returnData = ['0', '0', '0', '0', '0', '0', '0', '0', '0']
List num = []
String finalOutput = ""
if (output) {
List lines = output.readLines()
lines.each() { it2 ->
if (it2.trim()) {
finalOutput += it2 + " "
switch (it2) {
case ~/Document Length: ([0-9]+) bytes/:
//println "Document Length: ${Matcher.lastMatcher[0][1]}"
returnData[0] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Time taken for tests: ([0-9]*\.[0-9]*) seconds/:
//println "Time taken for tests: ${Matcher.lastMatcher[0][1]}"
returnData[1] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Complete requests: ([0-9]+)/:
//println "Complete requests: ${Matcher.lastMatcher[0][1]}"
returnData[3] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Failed requests: ([0-9]+)/:
//println "Failed requests: ${Matcher.lastMatcher[0][1]}"
returnData[4] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
//avg_num[4] = (returnData.each() as Integer).sum() / 9
break
case ~/Total transferred: ([0-9]+) bytes/:
//println "Total transferred: ${Matcher.lastMatcher[0][1]}"
returnData[5] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/HTML transferred: ([0-9]+) bytes/:
//println "HTML transferred: ${Matcher.lastMatcher[0][1]}"
returnData[6] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Requests per second: ([0-9\.]+) \[#\/sec\] \(mean\)/:
//println "Requests per second: ${Matcher.lastMatcher[0][1]}"
returnData[2] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Time per request: ([0-9\.]+) \[ms\] \(mean, across all concurrent requests\)/:
//println "Time per request: ${Matcher.lastMatcher[0][1]}"
returnData[7] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Transfer rate: ([0-9\.]+) \[Kbytes\/sec\] received/:
//println "Transfer rate: ${Matcher.lastMatcher[0][1]}"
returnData[8] = df.format(Float.parseFloat(Matcher.lastMatcher[0][1]))
break
case ~/Connect: ( *[0-9]+) ( *[0-9]+) ( *[0-9\.]+) ( *[0-9]+) ( *[0-9]+)/:
returnData[9] = df.format(Float.parseFloat(Matcher.lastMatcher[0][2]))
break
case ~/Processing: ( *[0-9]+) ( *[0-9]+) ( *[0-9\.]+) ( *[0-9]+) ( *[0-9]+)/:
returnData[10] = df.format(Float.parseFloat(Matcher.lastMatcher[0][2]))
break
case ~/Waiting: ( *[0-9]+) ( *[0-9]+) ( *[0-9\.]+) ( *[0-9]+) ( *[0-9]+)/:
returnData[11] = df.format(Float.parseFloat(Matcher.lastMatcher[0][2]))
break
case ~/Total: ( *[0-9]+) ( *[0-9]+) ( *[0-9\.]+) ( *[0-9]+) ( *[0-9]+)/:
returnData[12] = df.format(Float.parseFloat(Matcher.lastMatcher[0][2]))
break
}
}
}
println("[${returnData[0]} bytes / ${returnData[8]} kb/s] : ${returnData[2]} rps")
} else {
println("[ERROR: apiBench]: Error message follows : " + error)
}
return returnData
}
public BodyPublisher ofFormData(Map<Object, Object> data) {
var builder = new StringBuilder();
for (Map.Entry<Object, Object> entry : data.entrySet()) {
if (builder.length() > 0) {
builder.append("&");
}
builder.append(URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8));
builder.append("=");
builder.append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8));
}
return BodyPublishers.ofString(builder.toString());
}
protected HttpResponse sendAuthRequest(String authData, String contentType, String method, String endpoint, List headers) {
println("endpoint :"+endpoint)
HttpContext localContext = new BasicHttpContext();
//HttpResponse response
Map<Object, Object> data = new HashMap<>();
data.put("username", "admin");
data.put("password", "@6m!nP@s5");
switch(method){
case 'POST':
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(new URI(endpoint))version(HttpClient.Version.HTTP_2).POST(ofFormData(data)).headers("Content-Type", "application/json").build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
println(response.body())
//assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1));
/*
final List<Cookie> cookies = client.getCookieStore().getCookies();
cookies.each(){ it ->
if(it.getName()=='JSESSIONID'){
this.cookieStorage = it
}
}
*/
return response
break;
default:
//throw error
return null
break;
}
}
}
protected List sendRequest(String postData, Integer concurrency, Integer requests, String contentType, String token, String method, String endpoint, List headers, Map cookie) {
//CookieStore cookieStore = new BasicCookieStore();
//BasicClientCookie clientCookie = new BasicClientCookie("JSESSIONID", "8C1674AEC09A93F10BD959CD6DDCE5C7");
//clientCookie.setDomain("localhost");
//clientCookie.setPath("/");
//cookieStore.addCookie(clientCookie);
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
HttpContext localContext = new BasicHttpContext();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(endpoint)
request.setHeader(new BasicHeader("Content-Type", "application/json"));
request.setHeader(new BasicHeader("Authorization", "Bearer " + token));
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
HttpResponse response = client.execute(request, localContext);
int statusCode = response.getStatusLine().getStatusCode()
println("Status Code: " + statusCode);
String responseBody = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
println("Response: " + responseBody);
}
}