From dd2240f75cf413ce91d53ec3123e8e0ee1fecbe3 Mon Sep 17 00:00:00 2001 From: ozerodb <50593663+ozerodb@users.noreply.github.com> Date: Fri, 1 Apr 2022 00:47:51 +0200 Subject: [PATCH] uploaded usl-modeler 1.0 --- .gitignore | 136 ++++++++++++++++++ .idea/.gitignore | 8 ++ .idea/artifacts/usl_modeler_jar.xml | 18 +++ .idea/compiler.xml | 13 ++ .idea/encodings.xml | 7 + .idea/jarRepositories.xml | 20 +++ .idea/misc.xml | 14 ++ .idea/vcs.xml | 6 + README.md | 16 ++- pom.xml | 26 ++++ src/main/java/META-INF/MANIFEST.MF | 3 + .../io/github/kotlandpolito/UslModeler.java | 47 ++++++ .../java/io/github/kotlandpolito/Utils.java | 60 ++++++++ 13 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/artifacts/usl_modeler_jar.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/io/github/kotlandpolito/UslModeler.java create mode 100644 src/main/java/io/github/kotlandpolito/Utils.java diff --git a/.gitignore b/.gitignore index a1c2a23..f229ae7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,117 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/intellij,java,maven +# Edit at https://www.toptal.com/developers/gitignore?templates=intellij,java,maven + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +### Java ### # Compiled class file *.class @@ -21,3 +135,25 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +replay_pid* + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# End of https://www.toptal.com/developers/gitignore/api/intellij,java,maven diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/artifacts/usl_modeler_jar.xml b/.idea/artifacts/usl_modeler_jar.xml new file mode 100644 index 0000000..8b48384 --- /dev/null +++ b/.idea/artifacts/usl_modeler_jar.xml @@ -0,0 +1,18 @@ + + + $PROJECT_DIR$/out/artifacts/usl_modeler_jar + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..e98f4d7 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 53d4e1c..b14a6d0 100644 --- a/README.md +++ b/README.md @@ -1 +1,15 @@ -# usl-modeler \ No newline at end of file +# usl-modeler + +This little repository contains the Java code we put together in order to use [usl4j](github.com/codahale/usl4j) during the first Web Applications II laboratory. + +## Usage + +The program accepts the input filename and the output filename as arguments. + +Using the {concurrency, throughput} pairs in the input file, it models a model that obeys to the the Universal Scalability Law, and writes in the output file the predicted throughput at increasing concurrency levels. + +A command line example is: + +```bash +java -jar usl-modeler.jar +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bf72be0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + io.github.kotlandpolito + usl-modeler + 1.0 + + + 8 + 8 + UTF-8 + UTF-8 + + + + + com.codahale + usl4j + 0.7.0 + + + + \ No newline at end of file diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000..36336fc --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: io.github.kotlandpolito.UslModeler + diff --git a/src/main/java/io/github/kotlandpolito/UslModeler.java b/src/main/java/io/github/kotlandpolito/UslModeler.java new file mode 100644 index 0000000..e9e5594 --- /dev/null +++ b/src/main/java/io/github/kotlandpolito/UslModeler.java @@ -0,0 +1,47 @@ +package io.github.kotlandpolito; + +import com.codahale.usl4j.Measurement; +import com.codahale.usl4j.Model; + +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class UslModeler { + public static void main(String[] args) { + if (args.length < 2){ + System.out.println("USAGE:\njava -jar usl-modeler-.jar "); + return; + } + + List lines; + try { + lines = Utils.parseCsv(args[0]); + } catch (FileNotFoundException e) { + System.out.println("Couldn't find the specified input file"); + return; + } + + double[][] points = lines.stream() + .map(Utils::stringArrayToDoubleArray) + .toArray(double[][]::new); + + // Map the points to measurements of concurrency and throughput, then build a model from them. + Model model = Arrays.stream(points) + .map(Measurement.ofConcurrency()::andThroughput) + .collect(Model.toModel()); + + // Predict the throughput for various levels of possible concurrency. + List outLines = new ArrayList<>(); + for (int i = 1; i < 300; ++i) { + outLines.add(new String[]{String.valueOf(i), String.valueOf(model.throughputAtConcurrency(i))}); + } + + try { + Utils.writeCsv(args[1], outLines); + } catch (FileNotFoundException e) { + System.out.println("Couldn't write to the specified output file"); + } + } +} diff --git a/src/main/java/io/github/kotlandpolito/Utils.java b/src/main/java/io/github/kotlandpolito/Utils.java new file mode 100644 index 0000000..a58b322 --- /dev/null +++ b/src/main/java/io/github/kotlandpolito/Utils.java @@ -0,0 +1,60 @@ +package io.github.kotlandpolito; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Utils { + + public static List parseCsv(String filename) throws FileNotFoundException { + List lines = new ArrayList<>(); + try (Scanner scanner = new Scanner(new File(filename))) { + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if(!line.isEmpty()){ + lines.add(line.split(",")); + } + } + } + return lines; + } + + public static void writeCsv(String filename, List lines) throws FileNotFoundException { + File csvOutputFile = new File(filename); + try (PrintWriter pw = new PrintWriter(csvOutputFile)) { + lines.stream() + .map(Utils::lineToCSV) + .forEach(pw::println); + } + } + + public static String escapeSpecialCharacters(String data) { + String escapedData = data.replaceAll("\\R", " "); + if (data.contains(",") || data.contains("\"") || data.contains("'")) { + data = data.replace("\"", "\"\""); + escapedData = "\"" + data + "\""; + } + return escapedData; + } + + public static String lineToCSV(String[] line) { + return Stream.of(line) + .map(Utils::escapeSpecialCharacters) + .collect(Collectors.joining(",")); + } + + public static double[] stringArrayToDoubleArray(String[] strings){ + return Arrays.stream(strings).mapToDouble(Double::parseDouble).toArray(); + } + + public static String[] doubleArrayToStringArray(double[] doubles){ + return Arrays.stream(doubles).mapToObj(String::valueOf).toArray(String[]::new); + } + +}