Skip to content

Instantly share code, notes, and snippets.

@kmarsh
Last active August 29, 2015 13:57
Show Gist options
  • Save kmarsh/9753608 to your computer and use it in GitHub Desktop.
Save kmarsh/9753608 to your computer and use it in GitHub Desktop.
Benchmarking accessing ElasticSearch via JRuby
#!/usr/bin/env ruby
require 'java'
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.common.settings.ImmutableSettings
import org.elasticsearch.action.search.SearchResponse
import org.elasticsearch.action.search.SearchType
import org.elasticsearch.index.query.FilterBuilders
import org.elasticsearch.index.query.QueryBuilders
def search(client)
response = client.prepareSearch("twitter")
.setTypes("tweet")
.setQuery(QueryBuilders.termQuery("trying", "out")) # Query
.setFrom(0).setSize(10)
.execute()
.actionGet()
p response.getHits().totalHits()
end
def index(client)
json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch #{Time.now.to_f}\"" +
"}"
client.prepareIndex("twitter", "tweet")
.setSource(json)
.execute()
.actionGet()
end
settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch_test").build()
client = TransportClient.new(settings)
client.addTransportAddress(InetSocketTransportAddress.new('localhost', 9300))
(ENV['SEARCH_TIMES'].to_i).times do
search(client)
end
(ENV['INDEX_TIMES'].to_i).times do
index(client)
end
client.close
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

export CLASSPATH=$(ls ~/Downloads/elasticsearch-1.0.0/lib/*.jar | tr '\n' ':')

On a rMBP (8GB RAM, 2.3 GHz Intel Core i7, 256GB SSD)

Jruby
Index Warm up 1,000 docs/sec 10,000 docs/sec 100,000 docs/sec 1,000,000 docs/sec
2.35 3.13 319.49 6.55 1526.72 25.99 3847.63 250.52 3991.70
2.32 3.08 324.68 6.22 1607.72 25.20 3968.25 #DIV/0!
Search (100K) 3.51 25.63 3901.68
3.28 25.28 3955.70
MRI 2.1.0 + rest-client
Index Warm up 1,000 docs/sec 10,000 docs/sec 100,000 docs/sec 1,000,000 docs/sec
0.3 1.72 12.66
0.31 1.49 9.29
1.37 10.14

Initial results show JRuby ~2x faster for larger workloads

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment