From 8aa9f962610a1449b00ba0e2be6e3d9d61fc0f34 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Tue, 5 Jun 2012 01:22:05 -0500 Subject: [PATCH] still trying to see positive results from batch rest streaming... so far no luck --- lib/neography/rest.rb | 19 +++--- spec/integration/streaming_spec.rb | 99 ++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 15 deletions(-) diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 60b6708..a1a3591 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -404,19 +404,16 @@ def batch(*args) Array(args).each_with_index do |c,i| batch << {:id => i}.merge(get_batch(c)) end - options = { :body => batch.to_json, :headers => {'Content-Type' => 'application/json', 'Accept' => 'application/json;stream=true'} } + options = { :body => batch.to_json, + :headers => {'Content-Type' => 'application/json' + # Seeing connection reset by peer errors + # and timeouts. + #, 'Accept' => 'application/json;stream=true' + }, + :write_timeout => 600 } post("/batch", options) end - - def batch_not_streaming(*args) - batch = [] - Array(args).each_with_index do |c,i| - batch << {:id => i}.merge(get_batch(c)) - end - options = { :body => batch.to_json, :headers => {'Content-Type' => 'application/json'} } - post("/batch", options) - end - + # For testing (use a separate neo4j instance) # call this before each test or spec def clean_database(sanity_check = "not_really") diff --git a/spec/integration/streaming_spec.rb b/spec/integration/streaming_spec.rb index abb9d60..4bf3dc9 100644 --- a/spec/integration/streaming_spec.rb +++ b/spec/integration/streaming_spec.rb @@ -6,14 +6,105 @@ end it "can batch a bunch of nodes streaming" do - commands = 500.times.collect{|x| [:create_node, {:name => "node-#{x}"}]} + class Enumerator + def next_json + begin + self.next.to_json.to_s + rescue StopIteration + return "" + end + end + end + #Enumerator.class_eval{ alias :call :next} + Enumerator.class_eval{ alias :call :next_json} + #module Enumerable + # class Enumerator + # #alias :call :next + # class_eval{ alias :call :next} + # end + #end + + module Neography + class Rest + + def batch_streaming(*args) + batch = [] + Array(args).each_with_index do |c,i| + batch << {:id => i}.merge(get_batch(c)) + end + options = { :body => batch.to_json, + :headers => {'Content-Type' => 'application/json', + # Seeing connection reset by peer errors + # and timeouts. + 'Accept' => 'application/json;stream=true' + }, + :write_timeout => 600 } + post("/batch", options) + end + + def batch_not_streaming(*args) + batch = [] + Array(args).each_with_index do |c,i| + batch << {:id => i}.merge(get_batch(c)) + end + options = { :body => batch.to_json, :headers => {'Content-Type' => 'application/json'} } + post("/batch", options) + end + + + def batch_chunked(*args) + batch = [] + Array(args).each_with_index do |c,i| + batch << {:id => i}.merge(get_batch(c)) + end + + file = StringIO.new(batch.to_json) + #file = Tempfile.new('a') + #file.write(batch.to_json) + #file.rewind + chunker = lambda do + file.read(50000).to_s + end + + options = { :headers => {'Content-Type' => 'application/json', + 'Accept' => 'application/json;stream=true'}, + :request_block => chunker, + :write_timeout => 600 + } + post("/batch", options) + end + + def batch_chunked2(*args) + batch = [] + Array(args).each_with_index do |c,i| + batch << {:id => i}.merge(get_batch(c)) + end + + options = { :headers => {'Content-Type' => 'application/json' + }, + :request_block => batch.each_slice(1000), + :write_timeout => 600 + } + post("/batch", options) + end + + + end + end + + commands = 2000.times.collect{|x| [:create_node, {:name => "node-#{x}"}]} + file = StringIO.new(commands.to_json) + Benchmark.bm do |x| - x.report("batch ") { @new_nodes = @neo.batch_not_streaming *commands } - x.report("streaming batch ") { @new_nodes_streaming = @neo.batch *commands } + x.report("batch ") { @new_nodes = @neo.batch *commands } + x.report("batch streaming ") { @new_nodes = @neo.batch_streaming *commands } + x.report("batch chunked ") { @new_nodes_streaming = @neo.batch_chunked *commands } + x.report("batch chunked 2 ") { @new_nodes_streaming = @neo.batch_chunked2 *commands } + x.report("batch not streaming ") { @new_nodes_not_streaming = @neo.batch_not_streaming *commands } end + @new_nodes_not_streaming.should_not be_nil @new_nodes.should_not be_nil @new_nodes_streaming.should_not be_nil - pending end end \ No newline at end of file