Is it better to buy a Sri Lanka eSIM before traveling or after arrival?

Before you travel. You can install it at home on Wi-Fi and check it is set up before boarding. Validity starts only when the eSIM first connects in Sri Lanka, so installing early does not use up your plan.