Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.sshd;
20
 
21
import java.io.ByteArrayOutputStream;
22
import java.io.OutputStream;
23
import java.util.ArrayList;
24
import java.util.Arrays;
25
import java.util.List;
26
import java.util.concurrent.CountDownLatch;
27
 
28
import org.apache.sshd.client.kex.DHG1;
29
import org.apache.sshd.common.Cipher;
30
import org.apache.sshd.common.KeyExchange;
31
import org.apache.sshd.common.NamedFactory;
32
import org.apache.sshd.common.cipher.BlowfishCBC;
33
import org.apache.sshd.util.BaseTest;
34
import org.apache.sshd.util.BogusPasswordAuthenticator;
35
import org.apache.sshd.util.EchoShellFactory;
36
import org.apache.sshd.util.Utils;
37
import org.junit.After;
38
import org.junit.Before;
39
import org.junit.Test;
40
 
41
import static org.junit.Assert.assertArrayEquals;
42
 
43
public class LoadTest extends BaseTest {
44
 
45
    private SshServer sshd;
46
    private int port;
47
 
48
    @Before
49
    public void setUp() throws Exception {
50
        port = Utils.getFreePort();
51
 
52
        sshd = SshServer.setUpDefaultServer();
53
        sshd.setPort(port);
54
        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
55
        sshd.setShellFactory(new EchoShellFactory());
56
        sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
57
        sshd.start();
58
    }
59
 
60
    @After
61
    public void tearDown() throws Exception {
62
        sshd.stop(true);
63
    }
64
 
65
    @Test
66
    public void testLoad() throws Exception {
67
        test("this is my command", 4, 4);
68
    }
69
 
70
    @Test
71
    public void testHighLoad() throws Exception {
72
        final StringBuilder response = new StringBuilder(1000000);
73
        for (int i = 0; i < 100000; i++) {
74
            response.append("0123456789");
75
        }
76
        test(response.toString(), 1, 100);
77
    }
78
 
79
    @Test
80
    public void testBigResponse() throws Exception {
81
        final StringBuilder response = new StringBuilder(1000000);
82
        for (int i = 0; i < 100000; i++) {
83
            response.append("0123456789");
84
        }
85
        test(response.toString(), 1, 1);
86
    }
87
 
88
    protected void test(final String msg, final int nbThreads, final int nbSessionsPerThread) throws Exception {
89
        final List<Throwable> errors = new ArrayList<Throwable>();
90
        final CountDownLatch latch = new CountDownLatch(nbThreads);
91
        for (int i = 0; i < nbThreads; i++) {
92
            Runnable r = new Runnable() {
93
                public void run() {
94
                    try {
95
                        for (int i = 0; i < nbSessionsPerThread; i++) {
96
                            runClient(msg);
97
                        }
98
                    } catch (Throwable t) {
99
                        errors.add(t);
100
                    } finally {
101
                        latch.countDown();
102
                    }
103
                }
104
            };
105
            new Thread(r).start();
106
        }
107
        latch.await();
108
        if (errors.size() > 0) {
109
            throw new Exception("Errors", errors.get(0));
110
        }
111
    }
112
 
113
    protected void runClient(String msg) throws Exception {
114
        SshClient client = SshClient.setUpDefaultClient();
115
        try {
116
            client.getProperties().put(SshClient.MAX_PACKET_SIZE, Integer.toString(1024 * 16));
117
            client.getProperties().put(SshClient.WINDOW_SIZE, Integer.toString(1024 * 8));
118
            client.setKeyExchangeFactories(Arrays.<NamedFactory<KeyExchange>>asList(
119
                    new DHG1.Factory()));
120
            client.setCipherFactories(Arrays.<NamedFactory<Cipher>>asList(
121
                    new BlowfishCBC.Factory()));
122
            client.start();
123
            ClientSession session = client.connect("sshd", "localhost", port).await().getSession();
124
            session.addPasswordIdentity("sshd");
125
            session.auth().verify();
126
 
127
            ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
128
            ByteArrayOutputStream out = new ByteArrayOutputStream();
129
            ByteArrayOutputStream err = new ByteArrayOutputStream();
130
            channel.setOut(out);
131
            channel.setErr(err);
132
            channel.open().await();
133
            OutputStream pipedIn = channel.getInvertedIn();
134
 
135
            msg += "\nexit\n";
136
            pipedIn.write(msg.getBytes());
137
            pipedIn.flush();
138
 
139
            channel.waitFor(ClientChannel.CLOSED, 0);
140
 
141
            channel.close(false);
142
            assertArrayEquals(msg.getBytes(), out.toByteArray());
143
        } finally {
144
            client.stop();
145
        }
146
    }
147
}