Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 11, 2015 19:29
Show Gist options
  • Select an option

  • Save henrik/4649195 to your computer and use it in GitHub Desktop.

Select an option

Save henrik/4649195 to your computer and use it in GitHub Desktop.
Got a script using Savon 2 with the e-conomic SOAP API (a 3 MB WSDL) down from around 30 seconds to around 2 seconds by marshalling the Wasabi WSDL parser. Consider this a proof-of-concept.
# For Wasabi 3.0.0.
# Marshals WSDL for a huge speedup with large files.
# A 3 MB WSDL went from around 30 to 2 seconds.
# Marshalling: 3.1 MB -> 629 KB.
# Zipping: 629 KB -> 59 KB.
module Wasabi
class Parser
alias_method :old_parse, :parse
def parse
old_parse
# The Nokogiri document prevents marshalling.
# We've parsed so we don't need the document anymore.
self.document = nil
end
end
class Document
alias_method :old_parser, :parser
def parser
return @parser if @parser
@parser = load_parser_from_marshal
unless @parser
old_parser
dump_parser_to_marshal
end
@parser
end
private
def load_parser_from_marshal
if File.exist?(marshal_file)
Marshal.load(unzip(File.read(marshal_file)))
end
end
def dump_parser_to_marshal
data = zip(Marshal.dump(@parser))
File.open(marshal_file, "w") { |f| f.write(data) }
end
def zip(string)
Zlib::Deflate.deflate(string)
end
def unzip(string)
Zlib::Inflate.inflate(string)
end
def marshal_file
basename = File.basename(@document)
"./#{basename}.marshalled.zip"
end
end
end
@koppen
Copy link

koppen commented Jan 27, 2013

Clever! I'd love to see something like this in R-conomic. The performance is pretty much what's holding back the upgrade to Savon 1+

Do you think it could be added in a sensible manner?

@henrik
Copy link
Author

henrik commented Jan 27, 2013

@koppen Yeah, I think it could be added sensibly. I've been working on a tiny wrapper (around 50 lines) around Savon, usage e.g.:

number = client.call :Entry_GetLastUsedSerialNumber
data = client.call :Entry_GetData, entityHandle: { SerialNumber: number }

What I would like is for that wrapper to bundle a marshalled WSDL, and then R-conomic could use that wrapper and not Savon directly.

One thing I've wanted to improve with R-conomic is that it currently mixes low-level building of SOAP requests with its own entity abstractions. So if it were to use a wrapper like that, I think the code would be easier to maintain.

And for simple things, or to explore the API, you could just use the wrapper directly.

@henrik
Copy link
Author

henrik commented Jan 27, 2013

The marshalled WSDL is 629 KB where the original WSDL is 3.1 MB, by the way, so the gem would be a lot smaller, too.

The gem would probably need to tie down a specific version of Wasabi (Savon's parser) to ensure it can unmarshall the file. But I think that'd be acceptable to get this speedup.

@henrik
Copy link
Author

henrik commented Jan 27, 2013

And around 60 KB zipped. If that doesn't slow things down measurably, it could keep the file size down even more.

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