Today I found some magic.
An ActiveSupport module that provides an AMAZINGLY elegant alternative to the classic Ruby Benchmark class; which I had always considered to be a bit ‘clunky’.
ActiveSupport::Benchmarkable provides you a simple method ‘benchmark’ which you can pass a few options and block through and output the benchmark result to the log. Wrap this block around expensive operations or possible bottlenecks to get a time reading for the operation.
For example, let’s say you thought your data transformattion method was taking too long; you could wrap it in a benchmark block.
<% benchmark 'v' do %> <%= expensive_record_transforming %> <% end %>
That would write something like “
Processing some data (143.6ms)” to the log, which you can now use to monitor or compare speed over time (make sure its not getting worse as data size increases) or compared to alternate methods for optimisation.
The ActiveSupport::Benchmarkable docs also indicate that, you may give an optional logger level (
:error) as the
:level option. The default logger level value is
<% benchmark 'Low-level files', level: :debug do %> <%= lowlevel_files_operation %> <% end %>
Finally, you can pass true as the third argument to silence all log activity (other than the timing information) from inside the block. This is great for boiling down a noisy block to just a single statement that produces one log line:
<% benchmark 'Process data files', level: :info, silence: true do %> <%= expensive_and_chatty_files_operation %> <% end %>
while these samples are indicative of what they would look like in a .html.erb file (view) – it also works in any other place you use the mix in.
I have personally used these to great success in controllers and models to help diagnose specific controller/model operations suspected to be slow.
This ActiveSupport module is an immensely powerful tool to have in a developers arsenal. I highly recommend it’s use.
Recently I had a text field being used to store JSON data from my web application. I hadn’t yet discovered the amazingness of the PostgreSQL JSONB datatype.
JSONB is an amazing datatype because Postgres understands how to query the structure and the keys and values can be indexed.
So if you have a text fields you want to convert to JSONB (provided the existing data is in properly formed JSON already) You can change the datatype and convert the existing data using this simple SQL command:
alter table tbl_name alter column col_name type jsonb using col_name::JSON;
You may be inclined to covert the text using the to_json function like: ‘USING to_json(col_name);’ – BUT DON’T DO THAT! What happens with that is you get a single string encapsulating the valid JSON, containing your JSON. Effectively a double-encoding bug. The former (casting the column to JSON is the correct way).
Recently, I helped a client who owned an iPhone which they needed to restore to factory settings, however they had forgotten the passcode they had originally set the phone up with. Normally, to update an iPhone you are required to ‘authenticate yourself not he device so that the computer being used to do the update knows you own the device.
Under normal conditions this is completely fine; but in this situation it caused a big problem.
Luckily, if you don’t care about the contents of the phone, it is possible to reinstall iOS with out the passcode if you know the trick (be warned, the Phone will be completely erased, but if you cannot get into it and have no backup – you’ve basically lost the datas already, haven’t you?)
- Turn off the device by holding down the power button until the slide to turn of message appears and swipe.
- Connect your lightning cable to your PC but DO NOT PLUG IN THE PHONE.
- Hold the Phone’s “Home” button as you connect the usb cable into the device. This should cause the iPhone to start up automatically.
- Continue holding down the “Home” button until the “Connect to iTunes” symbol appears on the screen. When you see this, release the “Home” button.
- Click the “Restore” button and the device will be completely erased and iOS will be reinstalled onto the device.
And walla! One freshly Factory Restored iPhone without knowing the passcode. This can be done on any computer with iTunes, it does not have to be the computer that normally manages the backups and syncs of the phone.
Recently there has been a rise and fall of several penny auction sites boasting about selling genuine high-demand items at massive discounts off recommended retail prices. But all is not what it seems.
The secret to this scam is that you must pay money every time you make a bid!
And every time a bid is made, the timer for the auction increases a little bit, usually by about 10 seconds. So you have a $1000 camera with a bid for $150 and its got 6 seconds left, but nope, someone else makes a bid and extends the timer by 12 seconds or so, and then another, and another and this can go on for hours.
How much each bid costs varies, but usually they will front-bill your credit card (when you signup) for 200 bids for, say for the sake of argument $100. Many people often complain that the charge to their credit card is under-played and not obvious until its to late. Usually, getting refunds from Penny Auction operators is like trying to get blood out of a stone. (Although this practise itself is not a scam; it is definitely misleading).
Now is where the trouble starts.
The thing that most people don’t understand (and where the scam kicks in) is that penny auction operators are allowed to shill their own auction’s bids. They typically do this because;
- The price is too low
- If not enough people have big on the item
In other words, if the money paid by either the actual bid, or the charges made to the sum of people bidding is LESS than the cost of the item, the site will simply start bidding on its own behalf to push up the price (and extended the duration).
The other dangerous part of the penny auction is that the site operators are allowed to sell your browsing habits and personal information with third-parties, often with those that helps the operator perform “services”.
So with every bid effectively being charged to a credit card; each individual bid (with the obvious exception of the shill bids) subsidises the price of the equipment for the person who eventually wins the item and whom musty pay not only the price they bid, but also for each bid they made.
Sure, some people do actually win very cheap items. But for every individual auction there are several losers making up the difference in price (and who receive nothing). And since the auctioneer can bid against you, no item need actually be sold until the auction site has made a huge profit on that “cheap” item.
Fun Facts from a “Swipe Auctions” example:
By the time the auction ended (5 hours later than the first image at the top), the camera in the Swipe Auctions auction pictured had had another 13,105 BIDS!!
At using an EXTREMELY conservative estimate of 5 cents a bid, people still spent $1,283 on bids alone (probably closer to $2,500), and the guy who won had to buy the camera for $256 on top of whatever he spent on bidding.
Only 1 person won that camera auction, and only after spending several hundreds in bids.
Several people lost this auction and threw their money down the drain and wasted several hours of their lives.
There is no way to know if Swipe Auctions extended the auction superficially on their side in order to make more money (is it a coincidence that it ended so abruptly so early (11:00pm), but right after they cleared over $100 in bids (AT LEAST) over the MSRP of the camera?)
For the record, Swipe Auctions no longer exists (and they had actually launched dozens of ‘sister sites’- I have no idea if any of those are still in operation). But this article is written in the hopes that I can protect people from falling into these dodgy sites who aim to take your money and give you very little if nothing in return.