You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/04/06 05:39:44 UTC

[GitHub] [flink-ml] lindong28 commented on a diff in pull request #71: [FLINK-26443] Add benchmark framework

lindong28 commented on code in PR #71:
URL: https://github.com/apache/flink-ml/pull/71#discussion_r843468258


##########
flink-ml-benchmark/README.md:
##########
@@ -0,0 +1,172 @@
+# Flink ML Benchmark Getting Started
+
+This document provides instructions on how to run benchmarks on Flink ML's
+stages in a Linux/MacOS environment.
+
+## Prerequisites
+
+### Install Flink
+
+Please make sure Flink 1.14 or higher version has been installed in your local
+environment. You can refer to the [local
+installation](https://nightlies.apache.org/flink/flink-docs-master/docs/try-flink/local_installation/)
+instruction on Flink's document website for how to achieve this.
+
+### Set Up Flink Environment Variables
+
+After having installed Flink, please register `$FLINK_HOME` as an environment
+variable into your local environment.
+
+```bash
+cd ${path_to_flink}
+export FLINK_HOME=`pwd`
+```
+
+Then please run the following command. If this command returns 1.14.0 or a
+higher version, then it means that the required Flink environment has been
+successfully installed and registered in your local environment.
+
+```bash
+$FLINK_HOME/bin/flink --version
+```
+
+[//]: # (TODO: Add instructions to download binary distribution when release is
+    available)
+### Build Flink ML library
+
+In order to use Flink ML's CLI you need to have the latest binary distribution
+of Flink ML. You can acquire the distribution by building Flink ML's source code
+locally with the following command.
+
+```bash
+cd ${path_to_flink_ml}
+mvn clean package -DskipTests
+cd ./flink-ml-dist/target/flink-ml-*-bin/flink-ml*/
+```
+
+### Start Flink Cluster

Review Comment:
   The prerequisite section typically includes just the installation and env setup. It seems better to move the `Start Flink Cluster` to the `Run Benchmark Example` section.



##########
flink-ml-benchmark/src/main/java/org/apache/flink/ml/benchmark/BenchmarkUtils.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.ml.benchmark;
+
+import org.apache.flink.api.common.JobExecutionResult;
+import org.apache.flink.api.common.accumulators.LongCounter;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.ml.api.AlgoOperator;
+import org.apache.flink.ml.api.Estimator;
+import org.apache.flink.ml.api.Model;
+import org.apache.flink.ml.api.Stage;
+import org.apache.flink.ml.benchmark.data.DataGenerator;
+import org.apache.flink.ml.benchmark.data.InputDataGenerator;
+import org.apache.flink.ml.common.datastream.TableUtils;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/** Utility methods for benchmarks. */
+public class BenchmarkUtils {
+    /** Loads benchmark configuration maps from the provided json file. */
+    @SuppressWarnings("unchecked")
+    public static Map<String, ?> parseJsonFile(String path) throws IOException {
+        InputStream inputStream = new FileInputStream(path);
+        Map<String, ?> jsonMap = ReadWriteUtils.OBJECT_MAPPER.readValue(inputStream, Map.class);
+        Preconditions.checkArgument(
+                jsonMap.containsKey(Benchmark.VERSION_KEY)
+                        && jsonMap.get(Benchmark.VERSION_KEY).equals(1));
+
+        return jsonMap.entrySet().stream()
+                .filter(x -> !x.getKey().equals(Benchmark.VERSION_KEY))
+                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+    }
+
+    /**
+     * Instantiates a benchmark from its parameter map and executes the benchmark in the provided
+     * environment.
+     *
+     * @return Results of the executed benchmark.
+     */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public static BenchmarkResult runBenchmark(
+            StreamTableEnvironment tEnv, String name, Map<String, ?> params) throws Exception {
+        Stage stage = ReadWriteUtils.instantiateWithParams((Map<String, ?>) params.get("stage"));
+        InputDataGenerator inputDataGenerator =
+                ReadWriteUtils.instantiateWithParams((Map<String, ?>) params.get("inputData"));
+        DataGenerator modelDataGenerator = null;
+        if (params.containsKey("modelData")) {
+            modelDataGenerator =
+                    ReadWriteUtils.instantiateWithParams((Map<String, ?>) params.get("modelData"));
+        }
+
+        return runBenchmark(tEnv, name, stage, inputDataGenerator, modelDataGenerator);
+    }
+
+    /**
+     * Executes a benchmark from a stage with its inputDataGenerator in the provided environment.
+     *
+     * @return Results of the executed benchmark.
+     */
+    public static BenchmarkResult runBenchmark(
+            StreamTableEnvironment tEnv,
+            String name,
+            Stage<?> stage,
+            InputDataGenerator<?> inputDataGenerator)
+            throws Exception {
+        return runBenchmark(tEnv, name, stage, inputDataGenerator, null);
+    }
+
+    /**
+     * Executes a benchmark from a stage with its inputDataGenerator and modelDataGenerator in the
+     * provided environment.
+     *
+     * @return Results of the executed benchmark.
+     */
+    public static BenchmarkResult runBenchmark(

Review Comment:
   Could this method be `private`?



##########
flink-ml-benchmark/README.md:
##########
@@ -0,0 +1,172 @@
+# Flink ML Benchmark Getting Started
+
+This document provides instructions on how to run benchmarks on Flink ML's
+stages in a Linux/MacOS environment.
+
+## Prerequisites
+
+### Install Flink
+
+Please make sure Flink 1.14 or higher version has been installed in your local
+environment. You can refer to the [local
+installation](https://nightlies.apache.org/flink/flink-docs-master/docs/try-flink/local_installation/)
+instruction on Flink's document website for how to achieve this.
+
+### Set Up Flink Environment Variables
+
+After having installed Flink, please register `$FLINK_HOME` as an environment
+variable into your local environment.
+
+```bash
+cd ${path_to_flink}
+export FLINK_HOME=`pwd`
+```
+
+Then please run the following command. If this command returns 1.14.0 or a
+higher version, then it means that the required Flink environment has been
+successfully installed and registered in your local environment.
+
+```bash
+$FLINK_HOME/bin/flink --version
+```
+
+[//]: # (TODO: Add instructions to download binary distribution when release is
+    available)
+### Build Flink ML library
+
+In order to use Flink ML's CLI you need to have the latest binary distribution
+of Flink ML. You can acquire the distribution by building Flink ML's source code
+locally with the following command.
+
+```bash
+cd ${path_to_flink_ml}
+mvn clean package -DskipTests
+cd ./flink-ml-dist/target/flink-ml-*-bin/flink-ml*/
+```
+
+### Start Flink Cluster
+
+Please start a Flink standalone session in your local environment with the
+following command.
+
+```bash
+$FLINK_HOME/bin/start-cluster.sh
+```
+
+You should be able to navigate to the web UI at
+[localhost:8081](http://localhost:8081/) to view the Flink dashboard and see
+that the cluster is up and running.
+
+## Run Benchmark Example
+
+In Flink ML's binary distribution's folder, execute the following command to run
+an example benchmark.
+
+```bash
+./bin/flink-ml-benchmark.sh ./examples/benchmark-conf.json --output-file results.json

Review Comment:
   Updates this command to use `./conf/benchmark-conf.json`?



##########
flink-ml-benchmark/src/test/java/org/apache/flink/ml/benchmark/BenchmarkTest.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.ml.benchmark;
+
+import org.apache.flink.ml.benchmark.data.InputDataGenerator;
+import org.apache.flink.ml.benchmark.data.clustering.KMeansModelDataGenerator;
+import org.apache.flink.ml.benchmark.data.common.DenseVectorGenerator;
+import org.apache.flink.ml.clustering.kmeans.KMeans;
+import org.apache.flink.ml.clustering.kmeans.KMeansModel;
+import org.apache.flink.ml.common.distance.EuclideanDistanceMeasure;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Map;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/** Tests benchmarks. */
+@SuppressWarnings("unchecked")
+public class BenchmarkTest extends AbstractTestBase {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    @Test
+    @SuppressWarnings({"unchecked"})
+    public void testParseJsonFile() throws Exception {
+        File configFile = new File(tempFolder.newFolder().getAbsolutePath() + "/test-conf.json");
+        InputStream inputStream =
+                this.getClass().getClassLoader().getResourceAsStream("benchmark-conf.json");
+        FileUtils.copyInputStreamToFile(inputStream, configFile);
+
+        Map<String, ?> benchmarks = BenchmarkUtils.parseJsonFile(configFile.getAbsolutePath());
+        assertEquals(benchmarks.size(), 1);
+        assertTrue(benchmarks.containsKey("KMeansModel-1"));
+
+        Map<String, ?> benchmark = (Map<String, ?>) benchmarks.get("KMeansModel-1");
+
+        KMeansModel stage =
+                ReadWriteUtils.instantiateWithParams((Map<String, ?>) benchmark.get("stage"));

Review Comment:
   This logic could be covered by `testRunBenchmark()` if `testRunBenchmark()` uses `runBenchmark(StreamTableEnvironment tEnv, String name, Map<String, ?> params) `.
   
   The test name `testParseJsonFile()` actually suggests this test just tests the `parseJsonFile()` method. 
   
   And `ReadWriteUtils.instantiateWithParams()` is just an implementation detail of `runBenchmark()`.
   
   Would it be simpler to just check that the output value of `parseJsonFile()` meets expectation?



##########
flink-ml-benchmark/src/test/java/org/apache/flink/ml/benchmark/BenchmarkTest.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.ml.benchmark;
+
+import org.apache.flink.ml.benchmark.data.InputDataGenerator;
+import org.apache.flink.ml.benchmark.data.clustering.KMeansModelDataGenerator;
+import org.apache.flink.ml.benchmark.data.common.DenseVectorGenerator;
+import org.apache.flink.ml.clustering.kmeans.KMeans;
+import org.apache.flink.ml.clustering.kmeans.KMeansModel;
+import org.apache.flink.ml.common.distance.EuclideanDistanceMeasure;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Map;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/** Tests benchmarks. */
+@SuppressWarnings("unchecked")
+public class BenchmarkTest extends AbstractTestBase {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    @Test
+    @SuppressWarnings({"unchecked"})
+    public void testParseJsonFile() throws Exception {
+        File configFile = new File(tempFolder.newFolder().getAbsolutePath() + "/test-conf.json");
+        InputStream inputStream =
+                this.getClass().getClassLoader().getResourceAsStream("benchmark-conf.json");
+        FileUtils.copyInputStreamToFile(inputStream, configFile);
+
+        Map<String, ?> benchmarks = BenchmarkUtils.parseJsonFile(configFile.getAbsolutePath());
+        assertEquals(benchmarks.size(), 1);
+        assertTrue(benchmarks.containsKey("KMeansModel-1"));
+
+        Map<String, ?> benchmark = (Map<String, ?>) benchmarks.get("KMeansModel-1");
+
+        KMeansModel stage =
+                ReadWriteUtils.instantiateWithParams((Map<String, ?>) benchmark.get("stage"));
+        assertEquals("prediction", stage.getPredictionCol());
+        assertEquals("features", stage.getFeaturesCol());
+        assertEquals(2, stage.getK());
+        assertEquals(EuclideanDistanceMeasure.NAME, stage.getDistanceMeasure());
+
+        DenseVectorGenerator inputDataGenerator =
+                ReadWriteUtils.instantiateWithParams((Map<String, ?>) benchmark.get("inputData"));
+        assertArrayEquals(new String[] {"features"}, inputDataGenerator.getColNames());
+        assertEquals(2, inputDataGenerator.getSeed());
+        assertEquals(10000, inputDataGenerator.getNumValues());
+        assertEquals(10, inputDataGenerator.getVectorDim());
+
+        KMeansModelDataGenerator modelDataGenerator =
+                ReadWriteUtils.instantiateWithParams((Map<String, ?>) benchmark.get("modelData"));
+        assertEquals(1, modelDataGenerator.getSeed());
+        assertEquals(2, modelDataGenerator.getArraySize());
+        assertEquals(10, modelDataGenerator.getVectorDim());
+    }
+
+    @Test
+    public void testRunBenchmark() throws Exception {
+        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
+
+        KMeans kMeans = new KMeans().setK(5).setFeaturesCol("test_feature");
+        InputDataGenerator<?> inputsGenerator =
+                new DenseVectorGenerator()
+                        .setColNames("test_feature")
+                        .setNumValues(1000L)
+                        .setVectorDim(10);
+
+        long estimatedTime = System.currentTimeMillis();
+        BenchmarkResult result =
+                BenchmarkUtils.runBenchmark(tEnv, "testBenchmarkName", kMeans, inputsGenerator);

Review Comment:
   This method is only used in the test.
   
   Could it be simpler to test the method that is actually used in the production code, e.g. `runBenchmark(StreamTableEnvironment tEnv, String name, Map<String, ?> params)`?



##########
flink-ml-benchmark/README.md:
##########
@@ -0,0 +1,172 @@
+# Flink ML Benchmark Getting Started
+
+This document provides instructions on how to run benchmarks on Flink ML's
+stages in a Linux/MacOS environment.
+
+## Prerequisites
+
+### Install Flink
+
+Please make sure Flink 1.14 or higher version has been installed in your local
+environment. You can refer to the [local
+installation](https://nightlies.apache.org/flink/flink-docs-master/docs/try-flink/local_installation/)
+instruction on Flink's document website for how to achieve this.
+
+### Set Up Flink Environment Variables
+
+After having installed Flink, please register `$FLINK_HOME` as an environment
+variable into your local environment.
+
+```bash
+cd ${path_to_flink}
+export FLINK_HOME=`pwd`
+```
+
+Then please run the following command. If this command returns 1.14.0 or a

Review Comment:
   Given that Flink version requirement is already described above, and the `flink-ml-benchmark.sh` checks the Flink version and prints proper error when needed, would it be simpler to remove this paragraph from the README?



##########
flink-ml-benchmark/src/main/resources/benchmark-conf.json:
##########
@@ -0,0 +1,31 @@
+{
+  "version": 1,
+  "KMeansModel-1": {

Review Comment:
   Would it be useful to add an estimator benchmark in this example?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org