From b7c6d29a27bd2788ebaf396642bad94299e72243 Mon Sep 17 00:00:00 2001 From: Alvin Dimas Praditya Date: Sat, 10 Aug 2024 06:09:28 +0700 Subject: [PATCH] fix benchmark result on linux --- .idea/modules.xml | 1 + .idea/modules/lagassist.iml | 8 ++ .../lagassist/minebench/SpecsGetter.java | 73 ++++++++----------- 3 files changed, 39 insertions(+), 43 deletions(-) create mode 100644 .idea/modules/lagassist.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index 593c58c..dd026d1 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/.idea/modules/lagassist.iml b/.idea/modules/lagassist.iml new file mode 100644 index 0000000..d67c19d --- /dev/null +++ b/.idea/modules/lagassist.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/alvindimas05/lagassist/minebench/SpecsGetter.java b/src/main/java/org/alvindimas05/lagassist/minebench/SpecsGetter.java index 26dd04a..073bf0a 100644 --- a/src/main/java/org/alvindimas05/lagassist/minebench/SpecsGetter.java +++ b/src/main/java/org/alvindimas05/lagassist/minebench/SpecsGetter.java @@ -35,51 +35,38 @@ public class SpecsGetter { private static OperatingSystemMXBean osmx = ManagementFactory.getOperatingSystemMXBean(); -// private static String getLinuxCPU() { -// -// final String regex = "model name : (.*\\n)"; -// -// File fl = new File("/proc/cpuinfo"); -// if (!fl.exists()) { -// return "unknown"; -// } -// if (!fl.canRead()) { -// return "unknown"; -// } -// try { -// String stg = Others.readInputStreamAsString(new FileInputStream(fl)); -// -// final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); -// final Matcher matcher = pattern.matcher(stg); -// matcher.find(); -// -// return matcher.group(1).replaceAll("\n", ""); -// -// } catch (IOException e) { -// e.printStackTrace(); -// return "unknown"; -// } -// -// } + private static String getLinuxCPU() { - private static String formatCPU(String cpuname){ - return String.join(" ", - Arrays.stream(cpuname - .replaceAll("\\(R\\)", "") - .replaceAll(" CPU", "") - .split(" ")).toList().subList(0, 4)); - } + final String regex = "model name : (.*\\n)"; - private static String getLinuxCPU(){ - Runtime rt = Runtime.getRuntime(); - try { - Process proc = rt.exec("lscpu | grep 'Model name' | cut -f 2 -d ':' | awk '{$1=$1}1'"); - BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); + File fl = new File("/proc/cpuinfo"); + if (!fl.exists()) { + return "unknown"; + } + if (!fl.canRead()) { + return "unknown"; + } + try { + String stg = Others.readInputStreamAsString(new FileInputStream(fl)); - return formatCPU(reader.readLine()); - } catch(IOException e){ - return "unknown"; - } + final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); + final Matcher matcher = pattern.matcher(stg); + matcher.find(); + + return formatCPU(matcher.group(1).replaceAll("\n", "")); + } catch (IOException e) { + e.printStackTrace(); + } + return "unknown"; + } + + private static String formatCPU(String cpuname){ + return String.join(" ", + Arrays.copyOfRange( + cpuname.replaceAll("\\(R\\)| CPU", "").split(" "), + 0, 4 + ) + ); } private static String getWindowsCPU() { @@ -184,7 +171,7 @@ public static BenchResponse getBenchmark() { } catch (Exception e) { e.printStackTrace(); } - return null; + return new BenchResponse(-1, -1, -1, false); }