Forum Discussion

seanlon11's avatar
seanlon11
Occasional Contributor
17 years ago

Strategy to validate Load Tests are complete

I am running some Load Tests that I created manually within the SoapUI GUI from a LoadTestRunner class that I have created (see below).  I am using the TestNG framework. 

Since these are Load Tests, and could potentially run for an extended period of time, I am running them asynchronously.  Since I kick them off asynchronously, I paint myself into a corner by not being able to easily determine whether the test has completed or not. 

My solution is to currently check the status of each test every 10 seconds.  If its status is RUNNING, the thread sleeps for 10 seconds and checks again.  If the thread is not RUNNING, I then evaluate its result.

Has anyone else run across this problem, and if so, have you put together a better solution?

Thanks,
    Sean 



public class LoadTestTestRunner {
    private static final Log LOGGER = LogFactory.getLog(LoadTestTestRunner.class);

    @Test(dataProvider = "loadTests", dataProviderClass = SoapUIDataProvider.class)
    public void testRunner(LoadTest loadTest) throws Exception {
        LoadTestRunner runner = loadTest.run();
        do {
            System.out.println(loadTest.getName() + " has been running for " + runner.getTimeTaken() + " millis");
            Thread.sleep(10000L);
        } while (Status.RUNNING.equals(runner.getStatus()));
        if (Status.FAILED.equals(runner.getStatus())) {
            LOGGER.error(loadTest.getName() + " failed!");
            assert false;
        } else {
            assert true;
        }
    }
}



public class SoapUIDataProvider {

    private static final Log LOGGER = LogFactory.getLog(SoapUIDataProvider.class);

    @DataProvider(name = "testCases")
    public static Object[][] provideTestCases() throws IOException, SoapUIException, XmlException {
        List<TestCase> testCases = findTestCases();
        Object[][] retval = new Object[testCases.size()][1];
        for (int i = 0; i < retval.length; i++) {
            retval[i] = new Object[] { TestCaseInvocationHandler.instance(testCases.get(i)) };
        }
        return retval;
    }
   
    @DataProvider(name = "loadTests")
    public static Object[][] provideLoadTests() throws IOException, SoapUIException, XmlException {
        List<TestCase> testCases = findTestCases();
        List<LoadTest> loadTests = null;
        for (TestCase testCase : testCases) {
            if(loadTests != null) {
                loadTests.addAll(testCase.getLoadTestList());
            }
            else {
                loadTests = testCase.getLoadTestList();
            }
        }
        Object[][] retval = new Object[loadTests.size()][1];
        for (int i = 0; i < retval.length; i++) {
            retval[i] = new Object[] { LoadTestInvocationHandler.instance(loadTests.get(i)) };
        }
        return retval;
    }
   
    private static List<TestCase> findTestCases() throws IOException, SoapUIException, XmlException {
        // read project files
        File[] projectFiles = findProjectFiles();
        if (projectFiles == null || projectFiles.length == 0) {
            // no xml files found
            return Collections.emptyList();
        }
        List<TestCase> testCases = null;
        for (File file : projectFiles) {
            validateCanRead(file);
            WsdlProject project = new WsdlProject(file.getPath());
            // read property overrides from properties file
            Properties properties = findProperties(file);
            if (properties != null) {
                for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                    project.setPropertyValue(entry.getKey().toString(), entry.getValue().toString());
                }
            }
            List<TestSuite> suites = project.getTestSuiteList();
            for (TestSuite testSuite : suites) {
                if (testCases != null) {
                    testCases.addAll(testSuite.getTestCaseList());
                } else {
                    testCases = testSuite.getTestCaseList();
                }
            }
        }
        return testCases;
    }

    private static void validateCanRead(File file) {
        if (!file.canRead()) {
            String message = "permission error: unable to read from file system \"" + file.getPath() + "\"";
            LOGGER.error(message);
            throw new SecurityException(message);
        }
    }

    private static File[] findProjectFiles() {
        File projectDir = new File(TestUtils.getLocalSoapUIProjectDir());
        if (!projectDir.exists() || !projectDir.isDirectory()) {
            String message = "soapui project directory does not exist! \"" + projectDir + "\"";
            LOGGER.error(message);
            throw new RuntimeException(message);
        }
        validateCanRead(projectDir);
        return projectDir.listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                if (pathname.isFile()) {
                    StringTokenizer tokenizer = new StringTokenizer(pathname.getName(), ".");
                    String ext = null;
                    // last token will be the file extension
                    while (tokenizer.hasMoreTokens()) {
                        ext = tokenizer.nextToken();
                    }
                    return "xml".equalsIgnoreCase(ext);
                }
                return false;
            }
        });
    }

    private static Properties findProperties(File projectFile) throws IOException {
        String propsFileStr = StringUtils.chomp(projectFile.getPath(), ".xml") + ".properties";
        File propsFile = new File(propsFileStr);
        if (!projectFile.exists()) {
            return null;
        }
        Properties properties = new Properties();
        InputStream stream = null;
        try {
            stream = new FileInputStream(propsFile);
            properties.load(stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
        return properties;
    }
}

No RepliesBe the first to reply