搭建Selenium Grid压力测试环境

  1. In one machine, run registry file MaxUserPort.reg
  2. Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters]
    "EnableICMPRedirect"=dword:00000000
    "TcpTimedWaitDelay"=dword:0000001e
    "MaxUserPort"=dword:0000ea60
    "DisableIPSourceRouting"=dword:00000002
    "EnableDeadGWDetect"=dword:00000000
    "KeepAliveTime"=dword:000493e0
    "TcpMaxDataRetransmissions"=dword:00000004
     
         
/**
 * @author junjshi, created on Sep 3, 2013
 * 
 */


import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.testng.annotations.Test;

import com.ebay.maui.controller.TestPlan;
@Test
public class GridTest extends TestPlan{
	private static String browser="firefox";
	
	private static final int threadPoolSize = 200;
	private static int count = 0;
	private static String profilePath = "C:\\grid\\profile\\customProfileDirCUSTFF";
	private static String url = "http://gear2-hub-8915.phx-os1.stratus.dev.ebay.com:8080/wd/hub";
	private static final int interval = 2;//minutes
	
	public static void main(String[] args) {
		ExecutorService exec = Executors.newFixedThreadPool(threadPoolSize);
		CommonTest.setUrl(url);
		CommonTest.setProfilePath(profilePath);
		CommonTest.setInterval(interval);
		Runnable task = new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						
						System.out.println("Clean "+System.getProperty("java.io.tmpdir"));
						File tempDir = new File(System.getProperty("java.io.tmpdir"));
						CommonTest.deleteDirectory(tempDir);
						System.out.println("Executing thread #"+ count++);
						new CommonTest().testGrid(browser);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		};

		for (int i = 0; i < threadPoolSize; i++) {
			exec.execute(task);
		}
	}

	

}
 
  CommonTest.java  
import java.io.File;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.openqa.selenium.WebDriver;

import com.ebay.maui.driver.web.WebUXDriver;

/**
 * @author junjshi, created on Dec 9, 2013
 * 
 */

public class CommonTest {
	private static String profilePath = "C:\\grid\\profile\\customProfileDirCUSTFF";
	private static String url = "http://gear2-hub-8915.phx-os1.stratus.dev.ebay.com:8080/wd/hub";
	private static int interval = 2;//minutes
	
	
	
	public static void setInterval(int interval) {
		CommonTest.interval = interval;
	}

	public static String getProfilePath() {
		return profilePath;
	}

	public static void setProfilePath(String profilePath) {
		CommonTest.profilePath = profilePath;
	}

	public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		CommonTest.url = url;
	}

	public static int getInterval() {
		return interval;
	}

	public void runTest(String url) throws MalformedURLException{
		WebUXDriver.getWebUXDriver().setMode("ExistingGrid");
		WebUXDriver.getWebUXDriver().setHubUrl(url);
		WebUXDriver.getWebUXDriver().setFfProfilePath(profilePath);
		WebDriver driver = WebUXDriver.getWebDriver(true);
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		System.out.println(driver.getTitle());
		try{
			driver.quit();
		}catch(Exception ex){}
	}
	
	public void runChromeTest(String url) throws MalformedURLException{

		WebUXDriver.getWebUXDriver().setBrowser("chrome");
		WebUXDriver.getWebUXDriver().setMode("ExistingGrid");
		WebUXDriver.getWebUXDriver().setHubUrl(url);
		WebDriver driver = WebUXDriver.getWebDriver(true);
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		System.out.println(driver.getTitle());
		try{
			driver.quit();
		}catch(Exception ex){}
	}
	
	public void runIETest(String url) throws MalformedURLException{

		WebUXDriver.getWebUXDriver().setBrowser("iexplore");
		WebUXDriver.getWebUXDriver().setMode("ExistingGrid");
		WebUXDriver.getWebUXDriver().setHubUrl(url);
		WebDriver driver = WebUXDriver.getWebDriver(true);
		driver.get("http://www.qa.ebay.com");
		System.out.println(driver.getTitle());
		try{
			driver.quit();
		}catch(Exception ex){}
	}
	
	public void testGrid(String browser) throws MalformedURLException, InterruptedException{
		ExecutorService exec = Executors.newCachedThreadPool();    
        int timeout =  60 * 30;
        final String browserName = browser;
        Callable<Boolean> task = new Callable<Boolean>(){

			@Override
			public Boolean call() throws Exception {
				if(browserName.equalsIgnoreCase("chrome")){
					runChromeTest(url);
					return Boolean.TRUE;  
				}else if(browserName.equalsIgnoreCase("firefox")){
					runTest(url);
					return Boolean.TRUE;
				}else
				{
					runIETest(url);
					return Boolean.TRUE;	
				}
			}
	    };
	    Calendar start = Calendar.getInstance();
	    start.setTime(new Date());
        Future<Boolean> future = exec.submit(task);    
        Boolean taskResult = null;    
        String failReason = null;    
        try {    
            taskResult = future.get(timeout, TimeUnit.SECONDS);    
        } catch (InterruptedException e) {    
            failReason = "Main thread was interrupted when waiting for computing result";    
        } catch (ExecutionException e) {    
            failReason = "Main thread is wating for result, but excpetion throw.";  
            e.printStackTrace();
        } catch (TimeoutException e) {    
            failReason = "Main thread is wating for result, but time out.";    
            exec.shutdownNow();    
        }   
        System.out.println(taskResult+";"+failReason);
        Calendar end = Calendar.getInstance();
	    end.setTime(new Date());
	    long duration = (end.getTimeInMillis()-start.getTimeInMillis())/1000/60;
	    System.out.println("duration is "+duration);
        if(duration<interval){
        	System.out.println("Wating ..."+ String.valueOf(interval-duration)+" minutes");
        	Thread.sleep(1000*(interval-duration)*60);
        }
	}	
	
	public static boolean deleteDirectory(File path) {
		if (path.exists()) {
			File[] files = path.listFiles();
			if(files==null)return true;
			
			for (int i = 0; i < files.length; i++) {
				if(files[i].lastModified()> Calendar.getInstance().getTimeInMillis()-1000*60*60)
					continue;
				if (files[i].isDirectory()) {
					
					deleteDirectory(files[i]);
				} else {
					files[i].delete();
				}
			}
		}
		return path.delete();
	}
}
  with MAUI dependency,(eBay开发的自动测试框架,读者可以用WebDriver开发) Run as JAVA application with VM parameter: -Xmx1024M
  1. Set the thread count and hub url and FirefoxProfile path as you need.
  2. Need to pay attention to folder C:\Users\stack\AppData\Local\Temp, it might increase very fast, clean it before testing on node machine.
    Add task to clean up the temp folder.
    cleanup.ps1

    Set-StrictMode -Version Latest
    # Function to remove all empty directories under the given path. If -DeletePathIfEmpty is provided the given Path directory will also be deleted if it is empty.
    function Remove-EmptyDirectories([parameter(Mandatory=$ true )][ValidateScript({Test-Path $_})][string] $Path, [ switch ] $DeletePathIfEmpty)
    {
         Get-ChildItem -Path $Path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $ null } | Remove-Item -Force -Recurse
         # If we should delete the given path when it is empty, and it is a directory, and it is empty, then delete it.
         if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $ null ) { Remove-Item -Path $Path -Force }
    }
    # Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
    function Remove-FilesCreatedBeforeDate([parameter(Mandatory=$ true )][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$ true )][DateTime] $DateTime, [ switch ] $DeletePathIfEmpty)
    {
         Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DateTime } | Remove-Item -Force
         Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty
    }
    # Function to remove all files in the given Path that have not been modified after the given date, as well as any empty directories that may be left behind.
    function Remove-FilesNotModifiedAfterDate([parameter(Mandatory=$ true )][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$ true )][DateTime] $DateTime, [ switch ] $DeletePathIfEmpty)
    {
         Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $DateTime } | Remove-Item -Force
         Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty
    }
    # Delete all files that have not been updated in 1 hour.
    Remove-FilesNotModifiedAfterDate -Path "C:\Users\stack\AppData\Local\Temp" -DateTime ((Get-Date).AddHours(- 1 ))

    cleanup.bat

    cd c:\grid
    powershell .\cleanup.ps1

    Add windows task CleanUpTempFolder:



  3. Because stress testing brought a big burden to node machine, so it might occupied too much CPU & Memory in node machine, make node machine can't create driver successfully.
    So change the c:\grid\service\watchdog\watchdog.config file, change run_time to 5, default is 15. And delete watchdog.log file.

    process_list=chrome.exe,iexplore.exe,firefox.exe,chromedriver.exe,IEDriverServer.exe
    run_time= 3
    poll_time= 1
  4. Monitoring tool:
    1. jvisualvm

    2. netstat -na (check connection situation)
  5. Problems we might found:
    org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Could not get document from window handle

    chrome crash
    Solution: kill all related processes, clean the temp folder, restart selenium node service

 

猜你喜欢

转载自shijunjuan.iteye.com/blog/1997768