HanG321 Blog
Be Shine, Be Smile, Be Wild
  • Home
  • Blog
    • 好文閱讀 readings
    • 生活記事 diary
    • 時事評論 commentary
    • 科技資訊 technology
    • 電腦編程 programming
    • 金融財經 finance
    • 音樂電影 music/movie
  • About

AsyncWeb & load test

June 2, 2009|asyncweb, Java, mina|電腦編程
Home » Blog » 電腦編程 » AsyncWeb & load test

There is insufficient information for AsyncWeb (subproject of Mina) on internet,
what I need is simulating a web services that reply in various seconds for load testing.

the following code may not be correct, some wiered debug message…
and I just depressed it by log4j.properties

Mostly follow the lightweight example of AsyncWeb,
the eclipse project zip can be obtained here


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
public class Main {
 
  private static final int PORT = 80;
 
  /**
   * Main
   *
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    SocketAcceptor acceptor = new NioSocketAcceptor();
 
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new HttpCodecFactory()));
    acceptor.getFilterChain().addLast("executor", new ExecutorFilter(new OrderedThreadPoolExecutor(32)));
 
    //Allow the port to be reused even if the socket is in TIME_WAIT state
    acceptor.setReuseAddress(true);
 
    acceptor.getSessionConfig().setReuseAddress(true);
    acceptor.getSessionConfig().setReceiveBufferSize(1024);
    acceptor.getSessionConfig().setSendBufferSize(1024);
    // No Nagle's algorithm
    acceptor.getSessionConfig().setTcpNoDelay(true);
    acceptor.getSessionConfig().setSoLinger(-1);
    acceptor.setBacklog(10240);
 
    acceptor.setHandler(new CustomHttpIoHandler());  // customer handler
    acceptor.bind(new InetSocketAddress(PORT));
 
  }
 
}

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
public class CustomHttpIoHandler implements IoHandler {
 
  private final Logger logger = LoggerFactory.getLogger(getClass());
 
  private final Timer timer;
 
  private Random random = new Random(System.currentTimeMillis());
 
  private final String SOAPENV_HEAD = "";
 
  public CustomHttpIoHandler(){
    timer = new Timer(true);
  }
 
  public void exceptionCaught(IoSession session, Throwable cause)
  throws Exception {
    if (!(cause instanceof IOException)) {
      cause.printStackTrace();
    }
    session.close(true);
  }
 
  public void messageSent(IoSession session, Object message) throws Exception {
  }
 
  public void sessionClosed(IoSession session) throws Exception {
  }
 
  public void sessionCreated(IoSession session) throws Exception {
  }
 
  public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
    session.close(true);
  }
 
  public void sessionOpened(IoSession session) throws Exception {
    session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
  }
 
  public void messageReceived(IoSession session, Object message) throws Exception {
 
    HttpRequest req = (HttpRequest) message;
    doAsynchronousDelayedResponse(session, req);
 
  }
 
  /**
   * business logic for various percentage and behaviour
   *
   * @param session mina session
   * @param req Http request
   * @throws CharacterCodingException
   */
  private void doAsynchronousDelayedResponse(final IoSession session, final HttpRequest req) throws CharacterCodingException {
 
    int delay = 0;
    Behaviour behaviour;
 
    int value = nextRandom(1, 100);
    if(value < = 50){
      // 50% of responses return valid result within 0-3 secs (good response time & good result)
      delay = nextRandom(10, 3000);
      behaviour = Behaviour.GOOD_TIME_GOOD_RESULT;
 
    }else if(value > 50 && value < =60){
      //10% of responses return expected error within 0-3 secs (good response time & expected error result)
      delay = nextRandom(10, 3000);
      behaviour = Behaviour.GOOD_TIME_ERROR_RESULT;
 
    }else if(value > 60 && value < = 90){
      //30% of responses return valid result within 3-15 secs (fair response time & good result)
      delay = nextRandom(3000, 15000);
      behaviour = Behaviour.FAIR_TIME_GOOD_RESULT;
 
    }else if(value > 90 && value < =95){
      //5% of responses return valid result within 15-190 secs (poor response time & good result)
      delay = nextRandom(15000, 190000);
      behaviour = Behaviour.POOR_TIME_GOOD_RESULT;
 
    }else if(value > 95 && value < =97){
      //2.5% of responses return valid result after 190 seconds (bad response time & good result)
      delay = nextRandom(15000, 190000);
      behaviour = Behaviour.BAD_TIME_GOOD_RESULT;
 
    }else{
      //2.5% of responses return 'no content' after 190 seconds (bad response time & bad result)
      delay = nextRandom(15000, 190000);
      behaviour = Behaviour.BAD_TIME_BAD_RESULT;
    }
 
    final MutableHttpResponse res = new DefaultHttpResponse();
    res.setStatus(HttpResponseStatus.OK);
    res.setContentType("application/soap+xml; charset=utf-8");
 
    // get corresponding response from behaviour
    StringBuffer sb = getResponseXML(behaviour);
 
    IoBuffer bb = IoBuffer.allocate(1024);
    bb.setAutoExpand(true);
    bb.putString(sb.toString(), Charset.forName("UTF-8").newEncoder());
    bb.flip();
    res.setContent(bb);
 
    logger.info("delay:" + delay + " stub:" + behaviour);
 
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        writeResponse(session, req, res);
      }
    }, delay);
  }
 
  /**
   * Write the http response
   *
   * @param session mina session
   * @param req HTTP Request
   * @param res HTTP Response
   */
  private void writeResponse(IoSession session, HttpRequest req, MutableHttpResponse res) {
 
    res.normalize(req);
    WriteFuture future = session.write(res);
    if (!HttpHeaderConstants.VALUE_KEEP_ALIVE.equalsIgnoreCase(
        res.getHeader( HttpHeaderConstants.KEY_CONNECTION))) {
      future.addListener(IoFutureListener.CLOSE);
    }
  }
 
  /**
   * Generate next random number from min(inclusive) to max(inclusive)
   * @param min minimum number
   * @param max maximum number
   * @return random integer
   */
  private int nextRandom(int min, int max){
 
    return random.nextInt(max-min+1)+min;
  }
 
  /**
   * Indicate what kind of behaviour
   *
   * @author SteveChan
   */
  public enum Behaviour{
    GOOD_TIME_GOOD_RESULT,  //return valid result within 0-3 secs
    GOOD_TIME_ERROR_RESULT, //return expected error within 0-3 secs
    FAIR_TIME_GOOD_RESULT,  //return valid result within 3-15 secs
    POOR_TIME_GOOD_RESULT,  //return valid result within 15-190 secs (
    BAD_TIME_GOOD_RESULT,   //return valid result after 190 seconds
    BAD_TIME_BAD_RESULT     //return 'no content' after 190 seconds
  }
 
  /**
   *
   * @param stub indicate what Behaviour
   * @return StringBuffer with return xml content
   */
  private StringBuffer getResponseXML(Behaviour stub){
 
    StringBuffer sb = new StringBuffer();
 
    if(stub.equals(Behaviour.GOOD_TIME_GOOD_RESULT) ||
        stub.equals(Behaviour.FAIR_TIME_GOOD_RESULT) ||
        stub.equals(Behaviour.POOR_TIME_GOOD_RESULT) ||
        stub.equals(Behaviour.BAD_TIME_GOOD_RESULT)){
 
      sb.append(SOAPENV_HEAD)
      .append("")
      // some content here
      .append("")
      .append("");
 
    }else if(stub.equals(Behaviour.GOOD_TIME_ERROR_RESULT)){
 
      sb.append(SOAPENV_HEAD)
      .append("")
      // some content here
      .append("")
      .append("");
 
    }else{
      //BAD_TIME_BAD_RESULT
      sb.append(SOAPENV_HEAD)
      .append("")
      // some content here
      .append("")
      .append("");
    }
 
    if(logger.isDebugEnabled()){
      logger.debug("return stardard soap response xml by " + stub );
      logger.debug(sb.toString());
    }
    return sb;
 
  }
 
}
June 2, 2009 user

Related Posts

  • JAVA_HOME of OpenJDK 1.7 on CentOS 7
    JAVA_HOME of OpenJDK 1.7 on CentOS 7June 7, 2015
  • Install Jenkins on Tomcat 7 (DSM)
    Install Jenkins on Tomcat 7 (DSM)August 18, 2014
  • Java Quick Starter
    Java Quick StarterApril 7, 2009

Popular Posts

  • Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)
    Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)June 8, 2015
  • assembly
    assemblyFebruary 11, 2006
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題….October 28, 2004
← xbox 360
六月 →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.




最新文章 Recent Posts

  • Serverless Cloud Run Development: Challenge Lab
  • ionic 4: capacitor & pwa
  • Open file limit of tomcat and mysql in Linux server
  • avoid subquery in mysql update statement
  • hacktoberfest

最新留言 Recent Comments

  • admin on LikeCoin
  • Edmond on LikeCoin
  • John Smith on Install macOS High Sierra on VMware
  • admin on Install macOS High Sierra on VMware
  • nana on Install macOS High Sierra on VMware




熱門文章 Popular Posts

  • Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)
    Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8) June 8, 2015
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題…. October 28, 2004
  • assembly
    assembly February 11, 2006
  • 新工作
    新工作 January 6, 2009
  • 嫁人要嫁工程師
    嫁人要嫁工程師 April 27, 2006

標籤雲 Tag Cloud

CentOS Character chroot Cluster crash cryptography DD-WRT debug Domino DSM Dual Core DWA email ESXi GCP git google HylaFax IE Java Javascript JRE LikeCoin Linux log LotusScript mint MX MySQL nginx PKI PowerShell Qwiklabs srt telent VMware vpn vSphere WinXP wordpress XPEnology 專欄 網絡資訊 選股 風帆

日曆 Calendar

June 2009
M T W T F S S
  « May   Jul »  
1234567
891011121314
15161718192021
22232425262728
2930  

Follow Me

Follow Us on RSSFollow Us on TwitterFollow Us on YouTube

文章存檔 Archives

Copyright © 2004-2021 hang321.net. All Rights Reserved