junyi.is中文

Thirty-eight seconds

From a connection in Shanghai, one visa page on my own product took 38 seconds to show the right content. I got that to about one. Making the page usable was another matter: a retest that said two seconds had timed the wrong element, on a line that wouldn't hold still.

Piaopiao Travel is built for people planning trips from mainland China. I built and tested almost all of it from the United States. For months, every speed number I had came from the wrong side of the ocean.

In August I fixed that the plain way: a real browser, on a wired connection in Shanghai, opening the product the way someone arriving from a search engine would — straight onto one country's visa page.

What the browser saw

The correct content appeared after 38.39 seconds. For about thirty of those seconds, the page showed the homepage instead.

Not a blank screen and not a spinner. The wrong page, looking finished. Someone who searched for one country's entry rules would land on a banner about planning a trip, and would reasonably leave.

visa page, Shanghai, real browser
  correct content visible   38.39 s
  homepage shown instead    ~30 s
  failed requests           0
  blocked third parties     0

Zero failed requests. Every file arrived. They arrived slowly, and the page had been built to wait for all of them before deciding what to show.

A narrow pipe, not a queue

The last file to land was the product's own 286 KB JavaScript bundle, at 38.04 seconds. I fetched that same file from both sides:

same 286 KB file
  United States    2.20 s   (~130 KB/s)
  Shanghai        11.47 s   (~25 KB/s)

small files (28 KB, 71 KB): no difference

My first write-up said the files were downloading one after another. That was wrong. They were downloading in parallel and sharing a narrow pipe. The fix for a queue and the fix for a narrow pipe are different things, and I had written down the wrong diagnosis.

Two other things were not slow. They were unreachable. Six destination photos came from a public image archive that timed out after 25 seconds with zero bytes. The default open map tiles timed out after 21. Neither showed up as an error. The photos simply had no size, and the map had no ground.

Three fixes, and one I left alone

The server already rendered a text summary of each country, hidden from people and kept for search engines. I made it visible: the country, its visa type, length of stay, official fee, official link, and one line saying the full guide was still loading. When the scripts arrive, it steps aside.

The photos moved onto the product's own domain — reachable by definition, since that is how the page was opened — and were resized to the width the cards actually use. If one fails anyway, the card shows the brand gradient with the city name. Never a hole.

For the map I switched tile providers and tested the new one's hostnames one at a time. Its first rotating subdomain failed 6 connections out of 6 from Shanghai; the others and the bare domain passed 12 of 12. Rotating across them would have made some tiles disappear with no error at all, which is harder to notice than a map that fails outright. I used only the bare domain.

The page now showed the right country in about a second. What I did not touch was the 286 KB bundle. Making it smaller was real performance work, and I scoped it out on purpose so the visible fix could go out the same day.

It got its turn the next day. Taking a pinyin dictionary out of it and loading each panel only where it is used took the JavaScript a visa page loads first, the 286 KB bundle included, from 485 KB compressed to 218. That went to production the same night. The country index followed: 236,895 compressed bytes down to 50,030, by moving the details only the detail view needs into a separate request.

Three days later

I went back to confirm. Same machine, same page.

Seeing the right country was one number. Being able to use the guide underneath it was another. A retest the night the size cuts went live had put that at two seconds. That number was wrong. I had stopped the clock on a line of text that belongs to the page shell, which arrives before the guide does. Timed against the guide itself, with the cuts live, three runs in production took between 22 and 33 seconds.

The line itself was not steady either. The same 52 KB file took 1.9 seconds to start arriving once and 0.53 seconds the next time. Nothing in the file had changed. The line had.

That was the actual finding. One measurement over a connection like that is not a measurement of the product. It is a measurement of the product at one hour, on one route, and only of the element I chose to time. I had been about to treat both 38 seconds and 2 seconds as facts about my code.

A baseline instead of a number

So I put a small sampler on the same connection, three times a day, with no browser in it — a browser is too fragile to leave unattended for months. It measures five things: time to first byte on a visa page and on the homepage, the transfer of the country index, the transfer of a single country's details, and one map tile. Every probe bypasses the cache, so every sample is a cold first visit.

One number gets read first: effective bandwidth during the index transfer. If it is low, everything is slow and the code is not the suspect. If it is normal and one probe is slow, the problem is mine.

first baseline, 11:47 Beijing time
  effective bandwidth    124 KB/s
  visa page, first byte  1260 ms
  country index          2835 ms
  map tile                482 ms

The rest of the size work came the same week, and only deleting bytes moved anything. Dropping one web font and no longer preloading another cut the fonts from 182 KB to 82. Moving the entrance animations to plain CSS took the first-load JavaScript to 194 KB.

One change I tried moved a file into an earlier wave of downloads. Tested against production it came out even, which meant it had eaten the gain from dropping the font. On a line that narrow, moving bytes around buys nothing. I reverted it the same day.

What it can't tell me

One city, one carrier, one wired line. Users on other carriers or on mobile data are not in this data at all, and mobile will be worse. The sampler cannot see the moment a page becomes usable, only the pieces that decide it; when I need that number, I still run the browser by hand.

It did change how I argue with myself. A claim that the site is slow for mainland users now has to come with two other numbers: that day's bandwidth, and the same request from the United States. One of my early mainland problems, a half-transparent first screen, looked exactly the same from both sides. It was just a problem.