Wednesday 18 July 2012

What is difference between | and || in java.


The operators || and && are called conditional operators, while | and & are called bitwise operators. They serve different purposes.
Conditional operators works only with expressions that statically evaluate to boolean on both left- and right-hand sides.
Bitwise operators works with any numeric operands.
If you want to perform a logical comparison, you should use conditional operators, since you will add some kind of type safety to your code.
a | b: evaluate b in any case

a || b: evaluate b only if a evaluates to false


| is the binary or operator
|| is the logic or operator



Below are some conclusion here : 
  • the operand of every expression is evaluated before the operation is performed except for the short-circuit operators (&&, ¦¦) and ternary operator
  • behaviour can produce unexpected results if you're not careful. For example, the following code illustrates what can occur if you try to set the value of a variable in an if condition and you always expect the new value to be available:
int i = 10;
int j = 12;

if( (i<j) ¦  (i=3) > 5 )   // value of i after oper: 3
if( (i<j) ¦¦ (i=3) > 5 )   // value of i after oper: 10

if( (i>j) &  (i=3) > 5 )   // value of i after oper: 3
if( (i>j) && (i=3) > 5 )   // value of i after oper: 10
  • with & and ¦ both operands are always evaluated
  • with && and ¦¦ the second operand is only evaluated when it is necessary
  • with ¦¦ (i<j) evaluates to true; there is no need to check the other operand as ¦¦ returns true if either of the operands are true
  • with && (i>j) evaluates to false; there is no need to check the other operand as && returns true only if both operands are true. In this case one is false so there is no need to check the other operand

What is difference between & and && in java.



Below are following difference between them:


  1. Single & is Ampercent, this is a Bitwise operator where as double && is an AND operation.
  2. & evaluates both sides of the operation and && evaluates the left side of the operation, if it's true, it continues and evaluates the right side.
  3. && is logical AND operator comparing boolean values of operands only. It takes two operands indicating a boolean value and makes a lazy evaluation on them. 
It depends on the type of the arguments...
For integer arguments, the single ampersand ("&")is the "bit-wise AND" operator. The double ampersand ("&&") is not defined for anything but two boolean arguments.
For boolean arguments, the single ampersand constitutes the (unconditional) "logical AND" operator while the double ampersand ("&&") is the "conditional logical AND" operator. That is to say that the single ampersand always evaluates both arguments whereas the double ampersand will only evaluate the second argument if the first argument is true.

Tuesday 17 July 2012

Jetty VS Tomcat Performance Comparison



Jetty and Tomcat are both open servlet containers, which support HTTP server, HTTP client, and javax.servlet container. in this article, we will quick view the difference between Jetty and Tomcat, so which eventually is better one?
You may think it is out-of-box question to compare Jetty and tomcat. Tomcat is obviously and frequently discussed as well as support more features to developers. Yes, I cannot say no, We started using tomcat during development as it is free, Its an foremost application server and provided full web server functions and can be stripped down to be embedded or built up an full JEE server.
Jetty is a uniformly excellent tool about particularly feature. It has been started around since 1998 and claims to be a “100% Java HTTP Server and Servlet Container”. It is a foremost a set of software components that offer HTTP and servlet services. Jetty can be installed as a standalone application server or be easily embedded in an application or framework as a HTTP component. It is a simple servlet engine, as do a feature rich servlet engine or as do part of a full JEE environment.

Jetty Features and Powered:

  • Full-featured and standards-based.
  • Embeddable and Asynchronous.
  • Open source and commercially usable.
  • Dual licensed under Apache and Eclipse.
  • Flexible and extensible, Enterprise scalable.
  • Strong Tools, Application, Devices and Cloud computing supported.
  • Low maintenance cost.
  • Small and Efficient.

Tomcat Features and Powered:

  • Famous open source under Apache.
  • Easier to embed Tomcat in your applications, e.g. in JBoss.
  • Implements the Servlet 3.0, JSP 2.2 and JSP-EL 2.2 support.
  • Strong and widely commercially usable and use.
  • Easy integrated with other application such as Spring.
  • Flexible and extensible, Enterprise scalable.
  • Faster JSP parsing.
  • Stable.

Jetty VS Tomcat Comparison on Performance

Test Environment:
CPU: Intel Core Dou T6400 2.0GHz
RAM: 2G
JDK: Jvm sun 1.6
OS: Ubuntu
I did few cases on Performance side between Tomcat and Jetty, its such simple so that it could not fully report which one presents better.
I developed a servlet whose url is /servlet/TestRuning.
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
  PrintWriter out = response.getWriter();
  String aStr = request.getParameter("a");
  String bStr = request.getParameter("b");
  int a = 100;
  int b = 100;
  try{
   a = Integer.parseInt(aStr);
   b = Integer.parseInt(bStr);
  }catch(Exception excep){
   System.err.println("err:" + excep.getMessage());
  }
  int sum = 0;
  long s = System.currentTimeMillis();
  for(int i = 0; i &lt; a; ++i){
   for(int ii = 0; ii &lt; b; ++ii){
    sum = a / b;
   }
  }
  long e = System.currentTimeMillis();
  long d = e - s;
  out.println( d );
  out.flush();
  out.close();
Code is simple, Just involves two loop and few mathematics. As well as We deployed this application under Jetty and Tomcat respectively with default configuration.
Note We was using the same JRE version.
1
2
3
4
wapproxy@ubuntu:~$ ps -ef | grep java
wapproxy  2076     1  1 11:28 ?        00:00:03 /usr/lib/jvm/java-6-openjdk/jre/bin/java -Djetty.home=/home/wapproxy/jetty -Djava.io.tmpdir=/tmp -jar /home/wapproxy/jetty/start.jar /home/wapproxy/jetty/etc/jetty-logging.xml /home/wapproxy/jetty/etc/jetty.xml
wapproxy  2185  1398  8 11:30 pts/0    00:00:02 /usr/lib/jvm/java-6-openjdk/jre/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/home/wapproxy/Tomcat/conf/logging.properties -Djava.endorsed.dirs=/home/wapproxy/Tomcat/endorsed -classpath :/home/wapproxy/Tomcat/bin/bootstrap.jar -Dcatalina.base=/home/wapproxy/Tomcat -Dcatalina.home=/home/wapproxy/Tomcat -Djava.io.tmpdir=/home/wapproxy/Tomcat/temp org.apache.catalina.startup.Bootstrap start
wapproxy  2329  2309  0 11:31 pts/1    00:00:00 grep --color=auto java
The tomcat startup port was 8888 and Jetty port was 8080, Then we did pressure test:
This is Jetty Performance reports:
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
Server Software:        Jetty(6.1.22)
Server Hostname:        172.31.36.158
Server Port:            8080
Document Path:          /jt_jt/servlet/TestRuning?a=100000&amp;b=100000
Document Length:        2 bytes
Concurrency Level:      1
Time taken for tests:   8.715 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      445000 bytes
HTML transferred:       10000 bytes
Requests per second:    573.72 [#/sec] (mean)
Time per request:       1.743 [ms] (mean)
Time per request:       1.743 [ms] (mean, across all concurrent requests)
Transfer rate:          49.86 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.1      0       5
Processing:     0    1   7.1      0      50
Waiting:        0    1   7.1      0      50
Total:          0    2   7.2      0      50
Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      5
  95%      5
  98%     45
  99%     50
 100%     50 (longest request)
This is Tomcat Performance reports:
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
Server Software:        Apache-Coyote/1.1
Server Hostname:        172.31.36.158
Server Port:            8888
Document Path:          /jt_jt/servlet/TestRuning?a=100000&amp;b=100000
Document Length:        3 bytes
Concurrency Level:      1
Time taken for tests:   4.070 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      650000 bytes
HTML transferred:       15000 bytes
Requests per second:    1228.50 [#/sec] (mean)
Time per request:       0.814 [ms] (mean)
Time per request:       0.814 [ms] (mean, across all concurrent requests)
Transfer rate:          155.96 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.2      0       5
Processing:     0    0   1.7      0      45
Waiting:        0    0   1.7      0      45
Total:          0    1   2.1      0      45
Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      5
  95%      5
  98%      5
  99%      5
 100%     45 (longest request)
We fingered out the key data on testing Jetty and Tomcat:
1
2
jetty 8080 Requests per second:    573.72 [#/sec] (mean)
tomcat 8888  Requests per second:    1228.50 [#/sec] (mean)
It shows that Jetty processed 573 requests and tomcat processed 1228 requests each second, from this data statistics, Tomcat did a little bit better.

More testing on Tomcat

Concurrent RequestsRequests Waitting TimeRequests Handling TimeThroughput
10.4220.4222370.37
51.6410.3283047.62
103.1250.3133200
206.5630.3283047.62
4012.50.3133200
6020.6250.3442909.09
80250.3133200
10034.3750.3442909.09
200596.8752.984335.08
300618.752.063484.85
4001006.2502.516397.52

Jetty VS Tomcat

More testing on Jetty

Concurrent RequestsRequests Waitting TimeRequests Handling TimeThroughput
16.3916.391156.48
511.4842.297435.37
1019.0631.906524.59
2025.6251.281780.49
400.79731.8751254.9
606.578394.688152.02
805.563445179.78
1001.781178.125561.4
2006.9841396.875143.18
3003.109932.813321.61
4006.5312612.813153.11
Jetty VS Tomcat
As I mentioned, You don’t take it so serious as it is just referred one aspect of their Performance. If you have any questions please let me know.


http://www.asjava.com/jetty/jetty-vs-tomcat-performance-comparison/