The routine inside handleConnection() accesses the socket input both by a scanner and a raw input stream, which is incorrect since a scanner does buffered reading and makes it impossible to read the value bytes later by a raw input stream.
The Java version uses a PrintWriter with auto flush enabled to write data into the socket, while the Python version manually flushes out at the end of each loop. This difference has huge impact on the benchmark result. You can try to flush out on every write in the Python version to see how slow it could be:
defoutput(sockfile, string):
"""Actually write to socket"""sockfile.write(string)
sockfile.flush() #experiment: auto flush on every write
I'll submit a pull request to fix the Java version. It's a version which replicates the logic of the Python version, without any further performance tuning.
I got the benchmark result as below, and the Java version is faster:
#Test against the Python server#usr and sys times are omitted#python 2.7.3, Ubuntu 12.04.1 LTS
Key-Value-Polyglot$ foriin {1..5};dotime python test.py;done
real 0m1.684s
real 0m1.660s
real 0m1.670s
real 0m1.666s
real 0m1.676s
#Test against the Java server#usr and sys times are omitted#java 1.7.0_07, Ubuntu 12.04.1 LTS
Key-Value-Polyglot$ foriin {1..5};dotime python test.py;done
real 0m2.984s #the first test could be slower as JIT is not warmed up
real 0m1.494s #faster than Python
real 0m1.460s
real 0m1.395s
real 0m1.368s