<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Re: Strange behavior and Performance
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/andrens">Andre Luiz Silva</a> in <i>jBPM</i> - <a href="https://community.jboss.org/message/730344#730344">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">We started</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">several</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">threads</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">calling the</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">webservice</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">to start</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">the process</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">.</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">Here's the</span><span style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;"> </span><span class="hps" style="font-family: arial, sans-serif; font-size: 16px; text-align: -webkit-auto; background-color: #f5f5f5;">code:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>package timeconsuming;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>import java.io.File;</p><p>import java.io.FileWriter;</p><p>import java.io.IOException;</p><p>import java.util.ArrayList;</p><p>import java.util.Date;</p><p>import java.util.List;</p><p>import java.util.concurrent.ExecutorService;</p><p>import java.util.concurrent.Executors;</p><p>import java.util.concurrent.TimeUnit;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>import javax.ws.rs.core.MultivaluedMap;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>import com.sun.jersey.api.client.ClientResponse;</p><p>import com.sun.jersey.api.client.UniformInterfaceException;</p><p>import com.sun.jersey.api.client.WebResource;</p><p>import com.sun.jersey.api.client.config.ClientConfig;</p><p>import com.sun.jersey.client.apache.ApacheHttpClient;</p><p>import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;</p><p>import com.sun.jersey.core.util.MultivaluedMapImpl;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>public class JbpmProcessTest {</p><p>  </p><p>          private static final int MAX_TIME = 3; //test maximum time in hours</p><p>          private static final int THREADS = 50;  //test threads count</p><p>          private static final String USER = "krisv";</p><p>          private static final String PASSWORD = "krisv";</p><p><span>          private static final String PROCESS_URL = "</span><a class="jive-link-external-small" href="" target="_blank">http://CPQD037542:8080/gwt-console-server/rs/process/definition/%s/new_instance</a><span>";</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>          public static void main(String[] args) {</p><p>                    try {</p><p>                              new JbpmProcessTest().instantiateProcess(1000, "Looping", true, "TesteScript");</p><p>                    } catch (Exception ex) {</p><p>                              ex.printStackTrace();</p><p>                    }</p><p>          }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>          public void instantiateProcess(int amount, String title, boolean isThread, final String processId) throws IOException {</p><p>                    ExecutorService executor = null;</p><p>  </p><p>                    if (isThread) {</p><p>                              executor = Executors.newFixedThreadPool(THREADS);</p><p>                    } else {</p><p>                              executor = Executors.newSingleThreadExecutor();</p><p>                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    File file = new File("C:\\JBPM" + title + "Test-" + THREADS + ".txt");</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    if (!file.exists() && !file.createNewFile()) {</p><p>                              System.out.println("Error to create file!");</p><p>                              return;</p><p>                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    final FileWriter writer = new FileWriter(file);</p><p>                    writer.write("JBPM" + title + "Test\n");</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    final List<Long> requestDelays = new ArrayList<Long>();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    Date initialDate = new Date();</p><p>                    System.out.println("Initiated: " + initialDate);</p><p>                    writer.write("Initiated: " + initialDate + "\n");</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    for (int i = 0; i < amount; i++) {</p><p>                              final int idx = i;</p><p>                              executor.execute(</p><p>                                        new Runnable () {</p><p>                                                  public void run() {</p><p>                                                            DefaultApacheHttpClientConfig apcc = new DefaultApacheHttpClientConfig();</p><p>                                                            apcc.getProperties().put(DefaultApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);</p><p>                                                            apcc.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);</p><p>                                                      ApacheHttpClient client = ApacheHttpClient.create(apcc);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                                                            WebResource webResource = client.resource(String.format(PROCESS_URL, processId));</p><p>  </p><p>                                                            // Just to authenticate</p><p>                                                            try {</p><p>                                                                      webResource.post(ClientResponse.class);</p><p>                                                                      MultivaluedMap<String, String> formData = new MultivaluedMapImpl();</p><p>                                                                      formData.add("j_username", USER);</p><p>                                                                      formData.add("j_password", PASSWORD);</p><p>                                                                      webResource.path("/j_security_check").type("application/x-www-form-urlencoded").post(String.class, formData);</p><p>                                                            } catch (UniformInterfaceException ex) {</p><p>                                                                      // DO NOTHING</p><p>                                                            }</p><p>  </p><p>                                                            long initialDelay = new Date().getTime();</p><p>                                                            ClientResponse response = webResource.post(ClientResponse.class);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                                                            long endDelay = new Date().getTime() - initialDelay;</p><p>                                                            System.out.println("response " + idx + " : " + response.getStatus() + " Delayed: " + endDelay / 1000);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                                                            try {</p><p>                                                                      writer.write("response " + idx + " : " + response.getStatus() + " Delayed: " + endDelay / 1000 + "\n");</p><p>                                                                      writer.flush();</p><p>                                                                      requestDelays.add(endDelay);</p><p>                                                            } catch (IOException ex) {</p><p>                                                                      // ex.printStackTrace();</p><p>                                                            }</p><p>                                                            client.destroy();</p><p>                                                  }</p><p>                                        }</p><p>                              );</p><p>                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    try {</p><p>                              executor.shutdown();</p><p>                              executor.awaitTermination(MAX_TIME, TimeUnit.HOURS);</p><p>                    } catch (InterruptedException ex) {</p><p>                              ex.printStackTrace();</p><p>                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    Date endDate = new Date();</p><p>                    Long delay = endDate.getTime() - initialDate.getTime();</p><p>                    System.out.println("Finalized: " + endDate);</p><p>                    writer.write("Finalized: " + endDate + "\n");</p><p>                    System.out.println("Total Execution Time: " + delay / 1000);</p><p>                    writer.write("Total Execution Time: " + delay / 1000 + "\n");</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    Long totalDelay = 0L;</p><p>                    for (Long requestDelay : requestDelays) {</p><p>                              totalDelay += requestDelay;</p><p>                    }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>                    System.out.println("Request Delay Average: " + (totalDelay / 1000) / requestDelays.size());</p><p>                    writer.write("Request Delay Average: " + (totalDelay / 1000)</p><p>                                        / requestDelays.size() + "\n");</p><p>                    writer.write("Total Request Amount: " + amount + "\n");</p><p>                    writer.close();</p><p>          }</p><p>}</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/730344#730344">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>