diff --git a/pxtlib/emitter/cloud.ts b/pxtlib/emitter/cloud.ts index 2203041d9969..ee144e189f35 100644 --- a/pxtlib/emitter/cloud.ts +++ b/pxtlib/emitter/cloud.ts @@ -116,6 +116,25 @@ namespace pxt.Cloud { }) } + export function downloadScriptMetaAsync(id: string): Promise { + return privateRequestAsync({ + url: id + (id.startsWith("S") ? `?time=${Date.now()}` : ""), + forceLiveEndpoint: true, + }).then(resp => { + return JSON.parse(resp.text).meta; + }) + } + + export async function downloadBuiltSimJsInfoAsync(id: string): Promise { + const targetVersion = pxt.appTarget.versions && pxt.appTarget.versions.target || ""; + const url = pxt.U.stringifyQueryString(id + "/js", { v: "v" + targetVersion }) + (id.startsWith("S") ? `&time=${Date.now()}` : ""); + const resp = await privateRequestAsync({ + url, + forceLiveEndpoint: true, + }); + return resp.json; + } + export async function markdownAsync(docid: string, locale?: string): Promise { // 1h check on markdown content if not on development server const MARKDOWN_EXPIRATION = pxt.BrowserUtils.isLocalHostDev() ? 0 : 1 * 60 * 60 * 1000; diff --git a/pxtrunner/runner.ts b/pxtrunner/runner.ts index 80885eb8d9d6..4a6413cdcd14 100644 --- a/pxtrunner/runner.ts +++ b/pxtrunner/runner.ts @@ -213,6 +213,7 @@ namespace pxt.runner { function initInnerAsync() { pxt.setAppTarget((window as any).pxtTargetBundle) + pxt.analytics.enable(pxt.Util.userLanguage()); Util.assert(!!pxt.appTarget); const href = window.location.href; @@ -375,7 +376,7 @@ namespace pxt.runner { } export async function simulateAsync(container: HTMLElement, simOptions: SimulateOptions): Promise { - const builtSimJS = simOptions.builtJsInfo || await buildSimJsInfo(simOptions); + const builtSimJS = simOptions.builtJsInfo || await fetchSimJsInfo(simOptions) || await buildSimJsInfo(simOptions); const { js } = builtSimJS; if (!js) { @@ -466,7 +467,24 @@ namespace pxt.runner { simDriver?.postMessage(msg); } + export async function fetchSimJsInfo(simOptions: SimulateOptions): Promise { + try { + const start = Date.now(); + const result = await pxt.Cloud.downloadBuiltSimJsInfoAsync(simOptions.id); + pxt.tickEvent("perfMeasurement", { + durationMs: Date.now() - start, + operation: "fetchSimJsInfo", + }); + return result; + } catch (e) { + // This exception will happen in the majority of cases, so we don't want to log it unless for debugging. + pxt.debug(e.toString()); + return undefined; + } + } + export async function buildSimJsInfo(simOptions: SimulateOptions): Promise { + const start = Date.now(); await loadPackageAsync(simOptions.id, simOptions.code, simOptions.dependencies); let didUpgrade = false; @@ -529,6 +547,10 @@ namespace pxt.runner { const res = pxtc.buildSimJsInfo(compileResult); res.parts = compileResult.usedParts; + pxt.tickEvent("perfMeasurement", { + durationMs: Date.now() - start, + operation: "buildSimJsInfo", + }); return res; }