This commit is contained in:
jjavierdguezas 2020-10-14 19:11:56 +02:00
parent 4745fac73e
commit 53ce1d54f0
2 changed files with 76 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import CloseIcon from '@material-ui/icons/Close';
import AddIcon from '@material-ui/icons/Add';
import Dialog from '@material-ui/core/Dialog';
import AppBar from '@material-ui/core/AppBar';
import Alert from '@material-ui/lab/Alert';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import {DragDropContext, Droppable, Draggable} from 'react-beautiful-dnd';
@ -16,6 +17,7 @@ import LightBulbIcon from '../icons/LightBulb';
import DeviceList from './DeviceList';
import AddDeviceContainer from '../../containers/AddDeviceContainer';
import ErrorBoundary from '../ErrorBoundary';
import {recommendedMaxNumberOfDevices} from '../../utils/deviceManagerUtils';
import styles from './styles.css';
@ -29,6 +31,8 @@ function DeviceManager(props) {
inactiveFiltered: [],
});
const [maxDevicesWarning, setMaxDevicesWarning] = useState(false);
useEffect(() => {
const activeDevices = props.browser.devices;
const activeDevicesById = activeDevices.reduce((acc, val) => {
@ -36,6 +40,8 @@ function DeviceManager(props) {
return acc;
}, {});
setMaxDevicesWarning(activeDevices.length >= recommendedMaxNumberOfDevices);
const currentInactiveDevicesById = devices.inactive.reduce((acc, val) => {
acc[val.id] = val;
return acc;
@ -104,6 +110,7 @@ function DeviceManager(props) {
const updateDevices = devices => {
const active = [...devices.active];
const inactive = [...devices.inactive];
setMaxDevicesWarning(active.length >= recommendedMaxNumberOfDevices);
setDevices({active, inactive});
props.setActiveDevices(active);
};
@ -133,6 +140,12 @@ function DeviceManager(props) {
</Toolbar>
</AppBar>
<div className={styles.container}>
{maxDevicesWarning && (
<Alert severity="warning" className={classes.maxDevicesWarning}>
Adding more than {recommendedMaxNumberOfDevices} devices may
slow down the system.
</Alert>
)}
<Typography variant="body1" className={classes.toolTip}>
<span></span>Drag and drop the devices across to re-order them.
</Typography>
@ -190,6 +203,10 @@ const useStyles = makeStyles(theme => ({
color: theme.palette.text.primary,
width: 'fit-content',
},
maxDevicesWarning: {
position: 'absolute',
top: '80px',
},
}));
export default DeviceManager;

View file

@ -0,0 +1,59 @@
import os from 'os';
import {SSL_ERROR_CODES} from '../constants/values';
import childProcess from 'child_process';
export const MIN_NUMBER_OF_DEVICES = 2;
function exec(command) {
const output = childProcess.execSync(command, {encoding: 'utf8'});
return output;
}
export function getPhysicalCpuCount() {
const platform = os.platform();
try {
if (platform === 'linux') {
const output = exec(
'lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l'
);
return parseInt(output.trim(), 10);
}
if (platform === 'darwin') {
const output = exec('sysctl -n hw.physicalcpu_max');
return parseInt(output.trim(), 10);
}
if (platform === 'win32') {
const output = exec('WMIC CPU Get NumberOfCores');
return output
.split(os.EOL)
.map(parseInt)
.filter(value => !Number.isNaN(value))
.reduce((sum, number) => sum + number, 0);
}
throw new Error('Unexpected OS');
} catch {
const cores = os.cpus().filter((cpu, index) => {
const hasHyperthreading = cpu.model.includes('Intel');
const isOdd = index % 2 === 1;
return !hasHyperthreading || isOdd;
});
return cores.length;
}
}
// TODO: observe and improve this
export function getRecommendedMaxNumberOfDevices() {
const logicalCpuInfos = os.cpus();
const cpuSpeed = logicalCpuInfos[0].speed;
const cpuCount = logicalCpuInfos.length;
const value = Math.max(
MIN_NUMBER_OF_DEVICES,
Math.trunc(cpuCount * (cpuSpeed / 2000))
);
return value;
}
export const recommendedMaxNumberOfDevices = getRecommendedMaxNumberOfDevices();