-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speeding up Shoes web requests #307
Comments
One of the first things I did many years ago was drop curl from Shoes 3. - It was incredibly hard to cross compile the library, they changed the api into the library dealing with notifications/callbacks which didn't look compatible with Shoes. Back then, open_uri was known to be slow, still is but it's one less dependency. We also know since then it has some other 'bugs'. |
The good news is that Shoes does not have to include libcurl in the same way as before because Typhoeus deals with it. Now your feedback is that including libcurl at C level is cumbersome. Using Typhoeus would just mean including Typhoeus gem and libcurl precompiled libraries. Wouldn't it be something closer to our reach? |
There is currently no callback for a progress bar in Typhoeus though libcurl does support it. Reference typhoeus/typhoeus#554. |
I was about to ask about that callback. That was part of the problem with C level curl too. I've already noted elsewhere the days of of cross compiling Shoes for Window may be over soon. This is just another reason. It would be easier for us if Typhoeus included a precompiledt/gemified libcurl for mingw but not a requirement. |
The callback actually always been available in curl. It was called CURLOPT_PROGRESSFUNCTION but documentation encourage to use CURLOPT_XFERINFOFUNCTION now.
What does it mean concretely? Why is that?
Agreed. We should rely more on Ruby. There are several things that could be written in Ruby rather than C. Typhoeus/libcurl is a prime example that speed is not affected by relying on Ruby. The idea would be to wrap Typhoeus in the infamous Download class and provide Why not a requirement? The whole thing would be roughly an additional 800KB including DLL files because some of the DLLs are already in Shoes. |
Ruby 2.2.6 won't cross compile. There's some other things don't cross compile anymore unless big hurdles are crossed. Every body wants to run tests or finalize something in their build by running what was built by running some exe on Windows before the build is complete. It happens because nobody thinks about cross compiling or folks like nokogiri create their own build framework which is at odds with how Shoes does cross platform builds. We know we need to build libgif and other dependencies. Might as well do that on Windows and skip the cross compile problems. Actually, the curl source code is still in the shoes/http/ dir. Out of date of course. download follows a tortuous path in C as it builds the ruby structs needed to call up into Ruby lib/shoes/dowload.rb That mess attempts to emulate the callbacks/event that curl exposed to the download api. We need to fix it for #304 independently of this issue. We could create a new Shoes method that doesn't have the progress callbacks and uses what ever Typhoeus allows. Or perhaps use ffi to hook into the callbacks. I haven't studied their code. |
How about setting up a build box on Windows (or a Windows VM)? You could control the build from your favourite OS, sending commands, automatically download the resulting logs and packages.
This is really up to you. Either way, using open_uri and/or Net::HTTP is just too slow.
Attempted to do exactly that! There is something missing to actually callback but no idea what. First day on the job as we would say. :) https://github.com/BackOrder/ethon |
Aha, they use ffi ! This is good news.
A lot for me to learn. I know I can start a VM with some Virtual box script but once its started how do I login. How do I run a rake script.... |
You should perhaps consider something simple to start with. Running a scheduled rakefile would be something to make you comfortable. The said rakefile could download source from github, libraries, etc., check if it needs to be packaged again, compile when there are changes and then send resulting logs and packaged apps to your ftp. You'd only have to turn on the VM and the scheduler would take care of the rest. It would otherwise be possible to run a small Ruby client/server if you want live output. The server would also be on a scheduler to make sure it's always running (or restarting when needed). It would look like this:
|
My attempt to add progress feature to Typhoeus worked with some help from the maintainer. It means we will be able to completely replace the current slow |
I have cross compiled curl 7.50-dev and curl.exe works on win7 - except for https (no certificates). This is good. The build process isn't as tedious as it used to be. The certficate problem is probably a curl config problem and I suspect there may not be good certs in openssl . RubyInstaller is (now) pretty explicit about not including them in 2.x.x - a minor shoes build addition to the rakefiles once I figure out where they are, and where they should be. Odd's are very high they aren't where my curl.exe expects them. There are many options to explore. But, we have a test program - curl.exe. [Update] |
https (using openssl) seems to work for me in Shoes 3.3.4 (change the url in samples/simple/downloader.rb and image.rb). It may be slow but it works. So, openssl and certs are probably OK. Thats for the net/http ruby stuff. I'm not keen on rejuvenating the old Shoes/C code to call curl at the C level for download and image. but it could be done. Since Shoes is currently doing a c->Ruby (lib/shoes/download.rb) that's what needs to be replaced with Typhoeus which means I need to build that gem on the 5 platforms so it and libcurl.so/dll/dylib can be.called from Shoes. A lot of cross platform dragons since we can't depend on linux or osx to have libcurl.so/dyllib |
It is noteworthy to mention that while my tests with Typheous were excellent both on features and speed, the fact remains that GIL (and possibly GTK threads) is preventing it to be an effective solution. It basically turned out to be Shoes UI is not updated while downloading. It's not a big deal for small files but it's a huge problem for larger files. This may also apply to older curl code at C level but you would know better. |
Oops! I forgot I told download.rb to not verify ssl. So, openssl certificates is likely to still be wrong on Shoes/Windows. I installed the typhoeus gem with draged in ethon in Ruby 2.3.3/Windows, It didn't build anything native so it must be using FFI. I built Shoes (win7), including the libcurl-4.dll I built yesterday and If those were all the libcurls it searched for, it wouldn't find libcurl-4.dll. It appears it is the ethon gem that is doing the ffi since it's the gem requiring the ffi binary gem (which Shoes includes). That will need fixing. I haven't looked at the ethon code yet but it could be a simple fix. Or not. Shoes progress (the widget) or the Shoes.Setup gem handling or |
ethon/curls/settings.rb: ffi_lib ['libcurl', 'libcurl.so.4'] - seems like place to explore. |
Please, read the first post. It contains all the instructions to make it work without having to change code. |
@backorder - always good to be reminded. You installed curl (the app) and changed the PATH in order to get a working test harness. However, Shoes users shouldn't have to do that for basic functionality We only need libcurl*dll inside the shoes app and the gems in the ruby. Code changes will be required to ethon/typheous - it could be an override/monkey patch we can do in Shoes or there is some upper level setting to add to that array passed into ffi_lib. |
This is just something to get you started. I suspect it should work if the proper library is in Shoes directory. |
Setting this gets ethon to work. (xwin7)
No sure how that can be monkey patched - i believe it's being call by the 'require'
which would work with the original code - perhaps the xwin7 curl build can be told to name it libcurl.dll |
* libcurl rename in setup phase because ethon gem doesn't see the cross compiled libcurl-4.dll name.
Time for some benchmarks. On my cable modem, my website, 423KB file, (ymmv)
Typheous won't be faster than bare curl and and has a noticeable FFI pause which would skew this test even more - it's a small file. Clearly the problem is the progress bar and not so much the native library. What about a larger file? 10.9MB
Shoes Wins ! Beware of benchmarks! Yes, I did check twice. People complain about net/http or read complaints about net/http. This was Ruby 2.3.4. Perhaps they fixed net/http? Has anyone checked? Why is download with progress bar so damn slow? I don't know, I can only guess. Switching the back end library may not be a useful path to pursue. |
My understanding is that it creates a new thread and then Shoes juggles between Ruby green threads (for Shoes UI and code execution), GTK and the new download/progress thread. Unfortunately, GIL makes it so that only one thing at the time is executed. Testing on a larger file resulted in freezing Shoes UI. |
One mystery solved. The :pause arg is number of seconds to [update] |
Here's the script I'm using - in case I'm calculating transfer rates incorrectly or you want to compare. Shoes.app do
background "#eee"
@list = stack do
para "Enter a URL to download:", :margin => [10, 8, 10, 0]
flow :margin => 10 do
@url = edit_line :width => -120
@url.text ='http://walkabout.mvmanila.com/public/share/Ytm-2.exe'
st_time = 0
end_time = 0
totalsz = 0
button "Download", :width => 120 do
st_time = Time.now
@list.append do
stack do
background "#eee".."#ccd"
stack :margin => 10 do
dld = nil
para @url.text, " [", link("cancel") { dld.abort }, "]", :margin => 0
d = inscription "Beginning transfer.", :margin => 0
p = progress :width => 1.0, :height => 14
dld = download @url.text, :save => File.basename(@url.text),
:pause => 0.02,
:progress => proc { |dl|
d.text = "Transferred #{dl.transferred} of #{dl.length} bytes (#{dl.percent}%)"
p.fraction = dl.percent },
:finish => proc { |dl|
end_time = Time.now
secs = (end_time.to_i - st_time.to_i)
kb = dl.length < 1024 ? 1: dl.length / 1024
d.text = "Download completed KB/s: #{kb/secs} #{secs}"
}
end
end
end
end
end
end
end |
I've got the Shoes Windows download to 90 secs (not as pathetic) by dropping the (Windows) gtk_idle timeout from 100ms to 5ms - see shoes/native/gtk.c(shoes_native_loop(). Much of Shoes/Windows seems snappier but I know enough to know I could be biased. The file open dialog (cobbler->Install Gempack->load) where it creates a file open dialog really does seem faster. Setting the idle to 1ms does not hurt anything but not real improvements. This is a gtk timer that fires every given X ms and tells ruby to run it's threads - Linux gtk polls fd's which shoes/Windows doesn't do - unreliable if memory serves but that was back in Ruby 1.9.3 days. |
I'm attempting to modify /lib/shoes/download.rb with typhoeus and I can see how that happens in the old code. The old code keep everything download in memory and on saved it to a file at the end of the transfer so anything larger than your physical memory is going to start paging into virtual memory and only gets worse. |
You might want to test with something like an ISO file (Linux, FreeBSD, etc). Something at around 1 to 4 Gb would be a fairly good stress test. |
I've got a version of lib/shoes/download.rb using Typhoeus and it's performance is less than stellar on Windows. 123KB/s and 91 secs to to download the 10.9Mb file. 4 or 5 times slower than curl. It's better that we started with so that's worth something. No need for ethon mods or typhoeus mods. I do like the code simplicity of this version of download.rb (and it fixes some bugs - the large .iso) so I tried it on Linux minlin - 700KB/s and 16 secs - Acceptable on Linux.
Ruby hasn't used green threads since 1.9.x. Now days It Uses pthreads.- Native threads on Linux and OSX. Gtk uses pthreads (if built to do so and I try to do so). Ruby does have a thread pool (The GIL) that it manages which should not effect other pthreads (like GTK or Cocoa). Windows uses a pthreads emulator which is a dll (winpthreads.dll) included with shoes. Clearly there are still performance issues with Windows and Ruby - not my wheel house to fix. As we might remember, Windows has always had an event loop performance problem - back to the days of Shoes Raisins/Policeman when one thread sucked up 100%. The change to Gtk for Windows helped with that problem but I suspect it's not optimal when it deals with socket IO - it uses a different method of scheduling for Windows vs Linux. |
* Performance is slightly better on Windows. No big deal on Linux. * fixes an oversight - don't store the whole download in memory if save: <file> is given. * much useless code in the download.rb
* not even close to the api in the manual - since shoes 3.2.0 (m3.rb) * beware - m4.rb segfaults. gdb works.
The can of worms is fully open now! The old ruby, download.rb replacement was not compliant with the 3.0 download api as documented in the manual. There is a segfault in rbload.c for one on of the manual samples (m4) - that will be easier to fix and test than the speed problem with Windows I May have to revisit the select/poll of fd. Even with the new code/rake refactorings, that's a big pain to test since it affects everything. |
* minlin is good - image and download * not so good for other targets.
* hack to fix image and download (open_uri) to use a specified ca certs file. * does not fix OSX or Windows openssl and ssl certs - just hides it * different solutions required to get openssl really working on osx and windows. Which would not be need if Tyhpoeus was adopted and working - that's being a realy PITA.
* windows and osx need this - inspired by https://www.theguild.nl/ruby-and-ssl-certificate-validation/ * doesn't really fix the ssl issues in the ruby in Shoes windows and osx - that's a deps issue and much deeper * also includes the libcurl shared library and the ethon and Typhoeus gems - it doesn't use them (but you can try them)
There's a Shoes 3.3.4 beta |
* include some fixes from branch curl make typhoeus available when require 'typhoeus' * improves Windows performance on this task, and probably many others.
* still fails to load typhoeus during ruby init. Very confusing. because x86_64_linux and xwin7 work.
Shoes web requests are very slow. Installing Typhoeus and libcurl considerably speeds up web requests. Expert-funnies runs in a blink of an eye. It would be a fantastic to include in Shoes.
How to install Typhoeus and libcurl on Windows?
PATH
(either the user or system, your choice);C:\Program Files\Curl
to your PATHUpdated expert-funnies.rb:
The text was updated successfully, but these errors were encountered: