Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 252ecb7aac8bef353578b53cf5ed2c02cb3ff24f (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
//
//  ========================================================================
//  Copyright (c) 1995-2013 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.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.util.thread.QueuedThreadPool;


/* ------------------------------------------------------------ */
/** 
 */
public class Siege
{
    private static final class ConcurrentExchange extends HttpExchange
    {
        private final long _start=System.currentTimeMillis();
        private final HttpClient _client;
        private final CountDownLatch _latch;
        volatile int _status;
        volatile int _count;
        volatile long _bytes;
        final List<String> _uris;
        final int _repeats;
        int _u;
        int _r;
        
        AtomicBoolean counted=new AtomicBoolean(false);

        public ConcurrentExchange(HttpClient client,CountDownLatch latch, List<String> uris, int repeats)
        {
            _client = client;
            _latch = latch;
            _uris = uris;
            _repeats = repeats;
        }

        @Override
        protected void onConnectionFailed(Throwable ex)
        {
            if (!counted.getAndSet(true))
                _latch.countDown();
            super.onConnectionFailed(ex);
        }

        @Override
        protected void onException(Throwable ex)
        {
            if (!counted.getAndSet(true))
                _latch.countDown();
            super.onException(ex);
        }

        @Override
        protected void onExpire()
        {
            if (!counted.getAndSet(true))
                _latch.countDown();
            super.onExpire();
        }

        @Override
        protected void onResponseComplete() throws IOException
        {
            if (_status==200)
                _count++;
            if (!next() && !counted.getAndSet(true))
            {
                _latch.countDown();
                long duration=System.currentTimeMillis()-_start;
                System.err.printf("Got %d/%d with %dB in %dms %d%n",_count,_uris.size()*_repeats,_bytes,duration,_latch.getCount());
            }
        }
        

        /* ------------------------------------------------------------ */
        @Override
        protected void onResponseContent(Buffer content) throws IOException
        {
            _bytes+=content.length();
            super.onResponseContent(content);
        }

        /* ------------------------------------------------------------ */
        /**
         * @see org.eclipse.jetty.client.HttpExchange#onResponseHeader(org.eclipse.jetty.io.Buffer, org.eclipse.jetty.io.Buffer)
         */
        @Override
        protected void onResponseHeader(Buffer name, Buffer value) throws IOException
        {
            super.onResponseHeader(name,value);                
            if ("Set-Cookie".equalsIgnoreCase(name.toString()))
            {
                String v=value.toString();
                int c = v.indexOf(';');
                if (c>=0)
                    v=v.substring(0,c);
                addRequestHeader("Cookie",v);
            }
        }

        /* ------------------------------------------------------------ */
        /**
         * @see org.eclipse.jetty.client.HttpExchange#onResponseHeaderComplete()
         */
        @Override
        protected void onResponseHeaderComplete() throws IOException
        {
            super.onResponseHeaderComplete();
        }

        /* ------------------------------------------------------------ */
        /**
         * @see org.eclipse.jetty.client.HttpExchange#onResponseStatus(org.eclipse.jetty.io.Buffer, int, org.eclipse.jetty.io.Buffer)
         */
        @Override
        protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
        {
            _status=status;
            super.onResponseStatus(version,status,reason);
        }

        public boolean next()
        {
            if (_u>=_uris.size())
            {
                _u=0;
                _r++;
                if (_r>=_repeats)
                    return false;
            }
            
            String uri=_uris.get(_u++);
            
            reset();
            setMethod(HttpMethods.GET);
            setURL(uri);

            try
            {
                _client.send(this);
            }
            catch(IOException e)
            {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }

    public static void main(String[] args)
        throws Exception
    {
        if (args.length==0)
            args=new String[] 
                 { "-c", "2", "-r", "2", "http://localhost:8080/dump", "http://localhost:8080/d.txt"};
        
        int concurrent=1;
        int repeats=1;
        final List<String> uris = new ArrayList<String>();

        for (int i=0; i<args.length; i++)
        {
            String arg=args[i];
            if ("-c".equals(arg))
            {
                concurrent=Integer.parseInt(args[++i]);
                continue;
            }
            
            if ("-r".equals(arg))
            {
                repeats=Integer.parseInt(args[++i]);
                continue;
            }
            
            uris.add(arg);
        }
        
        QueuedThreadPool pool = new QueuedThreadPool();
        pool.setMaxThreads(500);
        pool.setDaemon(true);
        
        HttpClient client = new HttpClient();
        client.setThreadPool(pool);
        client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        client.setIdleTimeout(30000);
        client.setConnectTimeout(30000);
        client.setMaxConnectionsPerAddress(concurrent*2);
        client.start();
        
        final CountDownLatch latch = new CountDownLatch(concurrent);   
        
        
        for (int i=0;i<concurrent;i++)
        {
            ConcurrentExchange ex = new ConcurrentExchange(client,latch,uris,repeats);
            if (!ex.next())
                latch.countDown();
        }
        
        latch.await();
        client.stop();
        pool.stop();
    }
}

Back to the top