前言:
以前的工作一直做的是web方面的測試工作,效能測試也主要是類比web端向伺服器發送http請求。對app這一塊的體系和架構瞭解比較少。由於下一份工作主要是測試app伺服器的效能。就在網上找了一些資料,簡單的搭建一個app的測試環境。同時希望能夠使用jmeter類比app向伺服器發送請求。所以只適合初學者和我一起學習看,大神勿噴
前面我已經搭建了一個非常簡單的javaweb應用,實現使用javaServlet進行登陸的過程。參照前面的博文
現在基於LoginServlet開發app端的程式 1 使用adt建立android項目。layout我的理解就是與使用者互動的介面,屬於app的前端
在layout的activity_main.xml配置一些代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/nameText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="username" /> <EditText android:id="@+id/pwdText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="password" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="submit" /></LinearLayout>
2 在mainactivity.java輸入下面代碼,初學理解:這個java相當於是處理layout發送過來的資料。一些前端的校正工作可以在這進行,以及具體處理工作也可以在此進行。這裡是接收layout的使用者輸入,然後點擊登陸的時候使用get方法向伺服器發送http請求
。
package com.example.test;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import android.app.Activity; import android.os.Bundle; import android.util.Log;import android.view.View; import android.view.View.OnClickListener;import android.widget.Button; import android.widget.EditText;public class MainActivity extends Activity { private EditText nameText; private EditText pwdText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText = (EditText) findViewById(R.id.nameText); pwdText = (EditText) findViewById(R.id.pwdText); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 使用者輸入使用者名稱密碼, 然後通過Get方法發送給本機伺服器 String name = nameText.getText().toString(); String pwd = pwdText.getText().toString(); // 運行線程,使用GET方法向本機伺服器發送資料 GetThread getThread = new GetThread(name, pwd); getThread.start(); } }); } //子線程:通過GET方法向伺服器發送使用者名稱、密碼的資訊 class GetThread extends Thread { String name; String pwd; public GetThread(String name, String pwd) { this.name = name; this.pwd = pwd; } @Override public void run() { //用HttpClient發送請求,分為五步 //第一步:建立HttpClient對象 HttpClient httpClient = new DefaultHttpClient(); //注意,下面這一行中,我之前把連結中的"test"誤寫成了"text",導致調BUG調了半天沒弄出來,真是浪費時間啊 String url = "http://localhost:8080/LoginServlet/LoginServlet?name=" + name+ "&password=" + pwd; //第二步:建立代表請求的對象,參數是訪問的伺服器位址 HttpGet httpGet = new HttpGet(url); try { //第三步:執行請求,擷取伺服器發還的相應對象 HttpResponse response = httpClient.execute(httpGet); //第四步:檢查相應的狀態是否正常:檢查狀態代碼的值是200表示正常 if (response.getStatusLine().getStatusCode() == 200) { //第五步:從相應對象當中取出資料,放到entity當中 HttpEntity entity = response.getEntity(); BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent())); String result = reader.readLine(); Log.d("HTTP", "GET:" + result); } } catch (Exception e) { e.printStackTrace(); } } }}
我在前面的LoginServlet.java裡面加入了列印代碼。如果有請求過來就列印資訊。用於驗證請求發送成功。
至此:app端與伺服器端的互動類比過程已經完成 分析
從商務邏輯可以看出來,app端與伺服器的互動與web端與伺服器的互動幾乎一樣,可以認為是一套伺服器,兩種不同的用戶端。上面介紹的後台使用的是同一個javaservlet,一個是通過html的靜態web頁面去訪問,另外一種是通過app端訪問,對伺服器端來說效果是一樣的。
所以有過測試web伺服器效能的測試人員,不用擔心說沒有測試過app的伺服器效能。他們是同一套後台,測試伺服器效能時直接類比web端就是OK的。如果沒有web端,只有app端,測試效能的時候,可以找對應開發人員提供http請求報文。然後放到jmeter裡面進行測試就ok了
當然還有一種方式就是使用javasampler直接調用背景介面進行測試。這個要參照前面的javasampler進行
代碼來源:
http://www.cnblogs.com/smyhvae/p/4006009.html