Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de725e541d032977eac9854892108621a1975a4f (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server.session;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.junit.Test;


/**
 * AbstractSameNodeLoadTest
 * 
 * This test performs multiple concurrent requests for the same session on the same node.
 * 
 */
public abstract class AbstractSameNodeLoadTest
{
    protected boolean _stress = Boolean.getBoolean( "STRESS" );

    public abstract AbstractTestServer createServer(int port);

    @Test
    public void testLoad() throws Exception
    {
        if ( _stress )
        {
            String contextPath = "";
            String servletMapping = "/server";
            AbstractTestServer server1 = createServer( 0 );
            server1.addContext( contextPath ).addServlet( TestServlet.class, servletMapping );

            try
            {
                server1.start();
                int port1 = server1.getPort();

                HttpClient client = new HttpClient();
                client.start();
                try
                {
                    String url = "http://localhost:" + port1 + contextPath + servletMapping;


                    //create session via first server
                    ContentResponse response1 = client.GET(url + "?action=init");
                    assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
                    String sessionCookie = response1.getHeaders().getStringField( "Set-Cookie" );
                    assertTrue(sessionCookie != null);
                    // Mangle the cookie, replacing Path with $Path, etc.
                    sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");

                    //simulate 10 clients making 100 requests each
                    ExecutorService executor = Executors.newCachedThreadPool();
                    int clientsCount = 10;
                    CyclicBarrier barrier = new CyclicBarrier( clientsCount + 1 );
                    int requestsCount = 100;
                    Worker[] workers = new Worker[clientsCount];
                    for ( int i = 0; i < clientsCount; ++i )
                    {
                        workers[i] = new Worker(barrier, client, requestsCount, sessionCookie, url);
                        executor.execute( workers[i] );
                    }
                    // Wait for all workers to be ready
                    barrier.await();
                    long start = System.nanoTime();

                    // Wait for all workers to be done
                    barrier.await();
                    long end = System.nanoTime();
                    long elapsed = TimeUnit.NANOSECONDS.toMillis( end - start );
                    System.out.println( "elapsed ms: " + elapsed );

                    executor.shutdownNow();

                    // Perform one request to get the result
                    Request request = client.newRequest( url + "?action=result" );
                    request.header("Cookie", sessionCookie);
                    ContentResponse response2 = request.send();
                    assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
                    String response = response2.getContentAsString();
                    System.out.println( "get = " + response );
                    assertEquals(response.trim(), String.valueOf( clientsCount * requestsCount ) );
                }
                finally
                {
                    client.stop();
                }
            }
            finally
            {
                server1.stop();
            }
        }
    }

    public static class Worker implements Runnable
    {
        public static int COUNT = 0;
        
        private final HttpClient client;

        private final CyclicBarrier barrier;

        private final int requestsCount;

        private final String sessionCookie;

        private final String url;
        
        private final String name;


        public Worker(CyclicBarrier barrier, HttpClient client, int requestsCount, String sessionCookie, String url)
        {
            this.client = client;
            this.barrier = barrier;
            this.requestsCount = requestsCount;
            this.sessionCookie = sessionCookie;
            this.url = url;
            this.name = ""+(COUNT++);
        }


        public void run()
        {
            try
            {
                // Wait for all workers to be ready
                barrier.await();

                Random random = new Random( System.nanoTime() );

                for ( int i = 0; i < requestsCount; ++i )
                {
                    int pauseMsec = random.nextInt(1000);

                    //wait a random number of milliseconds between requests up to 1 second
                    if (pauseMsec > 0)
                    {
                        Thread.currentThread().sleep(pauseMsec);
                    }
                    Request request = client.newRequest(url + "?action=increment");
                    request.header("Cookie", sessionCookie);
                    ContentResponse response = request.send();
                    assertEquals(HttpServletResponse.SC_OK,response.getStatus());
                }

                // Wait for all workers to be done
                barrier.await();
            }
            catch ( Exception x )
            {
                throw new RuntimeException( x );
            }
        }
    }

    public static class TestServlet
        extends HttpServlet
    {
        @Override
        protected void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException
        {
            String action = request.getParameter( "action" );
            if ( "init".equals( action ) )
            {
                HttpSession session = request.getSession( true );
                session.setAttribute( "value", 0 );
            }
            else if ( "increment".equals( action ) )
            {
                HttpSession session = request.getSession( false );
                assertNotNull(session);
                synchronized(session)
                {
                    int value = (Integer) session.getAttribute( "value" );
                    session.setAttribute( "value", value + 1 );
                }
            }
            else if ( "result".equals( action ) )
            {
                HttpSession session = request.getSession( false );
                assertNotNull(session);
                Integer value = null;
                synchronized (session)
                {
                    value = (Integer) session.getAttribute( "value" );
                }
                PrintWriter writer = response.getWriter();
                writer.println( value );
                writer.flush();
            }
        }
    }
}

Back to the top