Jak zadzwonić do swojego spokojnego serwisu internetowego za pomocą JMeter? Wykonaj proste testy obciążenia i automatyzację
Opublikowany: 2018-02-12Reprezentacyjny transfer stanu (REST) zyskał powszechną akceptację w sieci Web jako prostsza alternatywa dla usług sieci Web opartych na protokole SOAP i Web Services Description Language (WSDL).
Kluczowym dowodem tej zmiany w projektowaniu interfejsów jest przyjęcie REST przez głównych dostawców usług Web 2.0 — w tym Yahoo, Google i Facebook — którzy wycofali lub przekazali interfejsy oparte na SOAP i WSDL na rzecz łatwiejszego w użyciu, model zorientowany na zasoby, aby wyeksponować swoje usługi. Czasami musisz przetestować swoją usługę internetową RESTFul. Możesz mieć kilka opcji:
- Uderz w swoją usługę internetową za pomocą jednego adresu URL
- Użyj
JMeter
i wciśnij WebService w pętli. Ten test wykonuje również prostyLoad Test
.
W tym przykładzie wyjaśnię, jak utworzyć prostą Hello World WebService
i uruchomić tę samą usługę sieciową za pomocą JMeter.
Jeśli masz jakiekolwiek z poniższych pytań, jesteś we właściwym miejscu:
- Testowanie usług internetowych SOAP/REST za pomocą JMeter
- Testowanie wydajności RESTful API przy użyciu JMeter
- Rest API Automation w JMeter – Testowanie wydajności
- Jak przetestować REST API za pomocą JMeter
- Używanie JMeter do testowania usługi internetowej RESTful (Jersey)
Podsumowanie akcji:
- Warunek
Pre-Requisite:
Całkowicie zaimplementuj tę usługę RESTFul. - Stwórz prostą klasę Javy: CrunchifyJMeterTest.java (Serwis na który trafimy za pomocą JMetera).
- Ponownie wdróż tę samą aplikację na serwerze internetowym Apache Tomcat.
- Uruchom JMeter i otwórz poniżej dostarczony plik
.jmx
- Uruchom i przeanalizuj swój test
Inny musi przeczytać:
- SOAP vs REST: prosty protokół dostępu do obiektów vs. Reprezentacyjny transfer państwowy
Krok 1
Warunek Pre-Requisite:
Całkowicie zaimplementuj tę usługę RESTFul. Wdróż i uruchom aplikację.
Daj mi znać, jeśli zauważysz z tym jakiś problem.
Krok 2
Utwórz plik CrunchifyJMeterTest.java
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 |
package com . crunchify . restjersey ; import java . io . FileNotFoundException ; import java . io . IOException ; import javax . ws . rs . GET ; import javax . ws . rs . Path ; import javax . ws . rs . Produces ; /** * @author Crunchify * */ @ Path ( "/index" ) public class CrunchifyJMeterTest { @ GET @ Produces ( "text/html" ) public String checkECV ( ) throws InterruptedException , FileNotFoundException , IOException { String result = "<br><div align='center'><h2>Hey This is Crunchify's JMeter Test...</h2></div>" ; System . out . println ( result ) ; Thread . sleep ( 1000 ) ; return result ; } } |
Powinieneś zobaczyć poniżej Struktura katalogów Eclipse:
Krok 3
Wdróż ponownie ten sam projekt CrunchifyRESTJerseyExample na Tomcat. Przetestuj swoją usługę REST.
URL:
http://localhost:8080/CrunchifyRESTJerseyExample/crunchify/index/
Krok 4
Skopiuj poniższy kod do pliku i zapisz go jako Crunchify-JMeter-Test.jmx
.
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 |
<? xml version = "1.0" encoding = "UTF-8" ?> < jmeterTestPlan version = "1.2" properties = "2.4" jmeter = "2.9 r1437961" > < hashTree > < TestPlan guiclass = "TestPlanGui" testclass = "TestPlan" testname = "App Shah Desktop Test" enabled = "true" > < stringProp name = "TestPlan.comments" > < / stringProp > < boolProp name = "TestPlan.functional_mode" > false < / boolProp > < boolProp name = "TestPlan.serialize_threadgroups" > false < / boolProp > < elementProp name = "TestPlan.user_defined_variables" elementType = "Arguments" guiclass = "ArgumentsPanel" testclass = "Arguments" testname = "User Defined Variables" enabled = "true" > < collectionProp name = "Arguments.arguments" / > < / elementProp > < stringProp name = "TestPlan.user_define_classpath" > < / stringProp > < / TestPlan > < hashTree > < ThreadGroup guiclass = "ThreadGroupGui" testclass = "ThreadGroup" testname = "Crunchify's REST Service JMeter Test" enabled = "true" > < stringProp name = "ThreadGroup.on_sample_error" > continue < / stringProp > < elementProp name = "ThreadGroup.main_controller" elementType = "LoopController" guiclass = "LoopControlPanel" testclass = "LoopController" testname = "Loop Controller" enabled = "true" > < boolProp name = "LoopController.continue_forever" > false < / boolProp > < intProp name = "LoopController.loops" > - 1 < / intProp > < / elementProp > < stringProp name = "ThreadGroup.num_threads" > 5 < / stringProp > < stringProp name = "ThreadGroup.ramp_time" > 2 < / stringProp > < longProp name = "ThreadGroup.start_time" > 1367432020000 < / longProp > < longProp name = "ThreadGroup.end_time" > 1367432020000 < / longProp > < boolProp name = "ThreadGroup.scheduler" > false < / boolProp > < stringProp name = "ThreadGroup.duration" > < / stringProp > < stringProp name = "ThreadGroup.delay" > < / stringProp > < / ThreadGroup > < hashTree > < HTTPSamplerProxy guiclass = "HttpTestSampleGui" testclass = "HTTPSamplerProxy" testname = "HTTP Request" enabled = "true" > < boolProp name = "HTTPSampler.postBodyRaw" > true < / boolProp > < elementProp name = "HTTPsampler.Arguments" elementType = "Arguments" > < collectionProp name = "Arguments.arguments" > < elementProp name = "" elementType = "HTTPArgument" > < boolProp name = "HTTPArgument.always_encode" > false < / boolProp > < stringProp name = "Argument.value" > < / stringProp > < stringProp name = "Argument.metadata" > = < / stringProp > < / elementProp > < / collectionProp > < / elementProp > < stringProp name = "HTTPSampler.domain" > localhost < / stringProp > < stringProp name = "HTTPSampler.port" > 8080 < / stringProp > < stringProp name = "HTTPSampler.connect_timeout" > 10000 < / stringProp > < stringProp name = "HTTPSampler.response_timeout" > 10000 < / stringProp > < stringProp name = "HTTPSampler.protocol" > < / stringProp > < stringProp name = "HTTPSampler.contentEncoding" > < / stringProp > < stringProp name = "HTTPSampler.path" > / CrunchifyRESTJerseyExample / crunchify / index < / stringProp > < stringProp name = "HTTPSampler.method" > GET < / stringProp > < boolProp name = "HTTPSampler.follow_redirects" > true < / boolProp > < boolProp name = "HTTPSampler.auto_redirects" > false < / boolProp > < boolProp name = "HTTPSampler.use_keepalive" > true < / boolProp > < boolProp name = "HTTPSampler.DO_MULTIPART_POST" > false < / boolProp > < boolProp name = "HTTPSampler.monitor" > false < / boolProp > < stringProp name = "HTTPSampler.embedded_url_re" > < / stringProp > < / HTTPSamplerProxy > < hashTree / > < ResultCollector guiclass = "StatGraphVisualizer" testclass = "ResultCollector" testname = "Aggregate Graph" enabled = "true" > < boolProp name = "ResultCollector.error_logging" > false < / boolProp > < objProp > < name > saveConfig < / name > < value class = "SampleSaveConfiguration" > < time > true < / time > < latency > true < / latency > < timestamp > true < / timestamp > < success > true < / success > < label > true < / label > < code > true < / code > < message > true < / message > < threadName > true < / threadName > < dataType > true < / dataType > < encoding > false < / encoding > < assertions > true < / assertions > < subresults > true < / subresults > < responseData > false < / responseData > < samplerData > false < / samplerData > < xml > true < / xml > < fieldNames > false < / fieldNames > < responseHeaders > false < / responseHeaders > < requestHeaders > false < / requestHeaders > < responseDataOnError > false < / responseDataOnError > < saveAssertionResultsFailureMessage > false < / saveAssertionResultsFailureMessage > < assertionsResultsToSave > 0 < / assertionsResultsToSave > < bytes > true < / bytes > < / value > < / objProp > < stringProp name = "filename" > < / stringProp > < / ResultCollector > < hashTree / > < ResultCollector guiclass = "StatVisualizer" testclass = "ResultCollector" testname = "Aggregate Report" enabled = "true" > < boolProp name = "ResultCollector.error_logging" > false < / boolProp > < objProp > < name > saveConfig < / name > < value class = "SampleSaveConfiguration" > < time > true < / time > < latency > true < / latency > < timestamp > true < / timestamp > < success > true < / success > < label > true < / label > < code > true < / code > < message > true < / message > < threadName > true < / threadName > < dataType > true < / dataType > < encoding > false < / encoding > < assertions > true < / assertions > < subresults > true < / subresults > < responseData > false < / responseData > < samplerData > false < / samplerData > < xml > false < / xml > < fieldNames > false < / fieldNames > < responseHeaders > false < / responseHeaders > < requestHeaders > false < / requestHeaders > < responseDataOnError > false < / responseDataOnError > < saveAssertionResultsFailureMessage > false < / saveAssertionResultsFailureMessage > < assertionsResultsToSave > 0 < / assertionsResultsToSave > < bytes > true < / bytes > < / value > < / objProp > < stringProp name = "filename" > < / stringProp > < / ResultCollector > < hashTree / > < / hashTree > < / hashTree > < / hashTree > < / jmeterTestPlan > |

Krok-5
- Pobierz Apache JMeter.
- Uruchom JMeter za pomocą pliku jmeter.bat lub jmeter.sh
- Kliknij
File -> Open
- Przejdź do folderu, w którym zapisałeś plik Crunchify-JMeter-Test.jmx i wybierz plik
Krok-6
- Teraz rozwiń test JMeter Rest Service Crunchify, jak widać na poniższym obrazku
- Kliknij Żądanie HTTP
- Upewnij się, że poniższe parametry są poprawne
Krok-7
Przeanalizuj wynik testu.
Rest API Automation w JMeter?
Cóż, po każdym cyklu rozwojowym można w określonych odstępach czasu uruchamiać test, który już zdefiniowałeś w pliku JMeter.
Istnieje wiele innych sposobów zmiany parametrów testu. Pobaw się nim i daj mi znać, jak to działa lub jeśli zauważysz z tym jakiś problem.