Repository: elastic/synthetics
Pull request #394 (2021-10-07 19:09:25)

fix: fix megabytes to mbps
Fixes https://github.com/elastic/synthetics/issues/284

Fixes the default network throttling to Mbps


Patch:
diff --git a/src/helpers.ts b/src/helpers.ts
index 03072f3..0d4e630 100644
--- a/src/helpers.ts
+++ b/src/helpers.ts
@@ -257,24 +257,25 @@ export function getDurationInUs(duration: number) {
   return Math.trunc(duration * 1e6);
 }
 
-export function megabytesToBytes(megabytes: number) {
-  return megabytes * 1024 * 1024;
+export function megabitsToBytes(megabytes: number) {
+  return megabytes * 1024 * 1024 / 8;
 }
 
-export function bytesToMegabytes(bytes: number) {
-  return bytes / 1024 / 1024;
+export function bytesToMegabits(bytes: number) {
+  return bytes / 1024 / 1024 * 8;
 }
 
 export const DEFAULT_NETWORK_CONDITIONS: NetworkConditions = {
-  downloadThroughput: megabytesToBytes(5), // megabytes/second
-  uploadThroughput: megabytesToBytes(3), // megabytes/second
+  downloadThroughput: megabitsToBytes(5), // Devtools CDP expects format to be in bytes/second 
+  uploadThroughput: megabitsToBytes(3), // Devtools CDP expects format to be in bytes/second
   latency: 20, // milliseconds,
   offline: false,
 }
 
+// Tranforms CDP dev tools format back to cli args format
 export function formatNetworkConditionsArgs(networkConditions: NetworkConditions) {
-  const d = bytesToMegabytes(networkConditions.downloadThroughput);
-  const u = bytesToMegabytes(networkConditions.uploadThroughput);
+  const d = bytesToMegabits(networkConditions.downloadThroughput);
+  const u = bytesToMegabits(networkConditions.uploadThroughput);
   const l = networkConditions.latency;
   return `${d}d/${u}u/${l}l`;
 }
@@ -297,10 +298,10 @@ export function parseNetworkConditions(args: string): NetworkConditions {
 
     switch (token) {
       case uploadToken:
-        networkConditions.uploadThroughput = megabytesToBytes(Number(value));
+        networkConditions.uploadThroughput = megabitsToBytes(Number(value));
         break;
       case downloadToken:
-        networkConditions.downloadThroughput = megabytesToBytes(Number(value));
+        networkConditions.downloadThroughput = megabitsToBytes(Number(value));
         break;
       case latencyToken: 
         networkConditions.latency = Number(value);
diff --git a/src/parse_args.ts b/src/parse_args.ts
index 44fb8af..ec41cfc 100644
--- a/src/parse_args.ts
+++ b/src/parse_args.ts
@@ -106,7 +106,7 @@ program
     'always return 0 as an exit code status, regardless of test pass / fail. Only return > 0 exit codes on internal errors where the suite could not be run'
   )
   .addOption(
-    new Option('--throttling <d/u/l>', 'List of options to throttle network conditions for download throughput (d) in megabytes/second, upload throughput (u) in megabytes/second and latency (l) in milliseconds.')
+    new Option('--throttling <d/u/l>', 'List of options to throttle network conditions for download throughput (d) in megabits/second, upload throughput (u) in megabits/second and latency (l) in milliseconds.')
       .default(DEFAULT_NETWORK_CONDITIONS_ARG)
   )
   .option('--no-throttling', 'Turns off default network throttling.')


Test patch:
diff --git a/__tests__/cli.test.ts b/__tests__/cli.test.ts
index 7aea628..bd91c7c 100644
--- a/__tests__/cli.test.ts
+++ b/__tests__/cli.test.ts
@@ -26,7 +26,7 @@
 import { ChildProcess, spawn } from 'child_process';
 import { join } from 'path';
 import { Server } from './utils/server';
-import { megabytesToBytes, DEFAULT_NETWORK_CONDITIONS } from '../src/helpers';
+import { megabitsToBytes, DEFAULT_NETWORK_CONDITIONS } from '../src/helpers';
 
 describe('CLI', () => {
   let server: Server;
@@ -384,8 +384,8 @@ describe('CLI', () => {
     });
 
     it('applies custom throttling', async () => {
-      const downloadThroughput = megabytesToBytes(3);
-      const uploadThroughput = megabytesToBytes(1);
+      const downloadThroughput = megabitsToBytes(3);
+      const uploadThroughput = megabitsToBytes(1);
       const latency = 30;
       const cli = new CLIMock()
         .args(cliArgs.concat([
@@ -405,8 +405,8 @@ describe('CLI', () => {
     });
 
     it('applies custom throttling order agnostic', async () => {
-      const downloadThroughput = megabytesToBytes(3);
-      const uploadThroughput = megabytesToBytes(1);
+      const downloadThroughput = megabitsToBytes(3);
+      const uploadThroughput = megabitsToBytes(1);
       const latency = 30;
       const cli = new CLIMock()
         .args(cliArgs.concat([
@@ -426,7 +426,7 @@ describe('CLI', () => {
     });
 
     it('uses default throttling when specific params are not provided', async () => {
-      const downloadThroughput = megabytesToBytes(2);
+      const downloadThroughput = megabitsToBytes(2);
       const cli = new CLIMock()
         .args(cliArgs.concat([
           '--throttling',
diff --git a/__tests__/core/gatherer.test.ts b/__tests__/core/gatherer.test.ts
index e9669f6..ca0a8e2 100644
--- a/__tests__/core/gatherer.test.ts
+++ b/__tests__/core/gatherer.test.ts
@@ -28,6 +28,7 @@ import { PluginManager } from '../../src/plugins';
 import { wsEndpoint } from '../utils/test-config';
 import { devices } from 'playwright-chromium';
 import { Server } from '../utils/server';
+import { megabitsToBytes } from '../../src/helpers';
 
 jest.mock('../../src/plugins/network');
 
@@ -119,8 +120,8 @@ describe('Gatherer', () => {
 
   describe('Network emulation', () => {
     const networkConditions = { 
-      downloadThroughput: 1024 * 1024 * 0.05, // slow 3g speeds, 0.4 Mbits
-      uploadThroughput: 1024 * 1024 * 0.02,
+      downloadThroughput: megabitsToBytes(3),
+      uploadThroughput: megabitsToBytes(1),
       latency: 20,
       offline: false,
     }
@@ -133,7 +134,7 @@ describe('Gatherer', () => {
       // Experimental browser API https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlink
       const downlink = await driver.page.evaluate(() => navigator.connection.downlink);
 
-      expect(0.5 > downlink && downlink > 0.3).toBe(true);
+      expect(3.5 > downlink && downlink > 2.5).toBe(true);
       await Gatherer.dispose(driver);
       await Gatherer.stop();
     });
@@ -158,7 +159,7 @@ describe('Gatherer', () => {
       // Experimental browser API https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlink
       const downlink = await page1.evaluate(() => navigator.connection.downlink);
 
-      expect(0.5 > downlink && downlink > 0.3).toBe(true);
+      expect(3.5 > downlink && downlink > 2.5).toBe(true);
       await Gatherer.dispose(driver);
       await Gatherer.stop();
     });