From 67820504148985e9451f1f418b7a7d30636f3e89 Mon Sep 17 00:00:00 2001 From: Rui Hu Date: Thu, 14 Mar 2024 11:30:15 +0800 Subject: [PATCH] Update unit tests and add performance analyze --- build.gradle.kts | 8 ++++ .../interview/PerformanceAnalyze.java | 47 +++++++++++++++++++ .../cleverthis/interview/SolutionTest.java | 23 ++------- .../interview/SolutionTestBase.java | 30 ++++++++++++ 4 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 src/test/java/com/cleverthis/interview/PerformanceAnalyze.java create mode 100644 src/test/java/com/cleverthis/interview/SolutionTestBase.java diff --git a/build.gradle.kts b/build.gradle.kts index 818ae5b..841b95c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,4 +28,12 @@ java { tasks.test { useJUnitPlatform() jvmArgs = listOf("-Dfast=true") +} + +tasks.register("runPerformanceAnalyze", JavaExec::class.java) +tasks.named("runPerformanceAnalyze") { + dependsOn("testClasses") + group = "verification" + classpath = sourceSets.test.get().runtimeClasspath + mainClass.set("com.cleverthis.interview.PerformanceAnalyze") } \ No newline at end of file diff --git a/src/test/java/com/cleverthis/interview/PerformanceAnalyze.java b/src/test/java/com/cleverthis/interview/PerformanceAnalyze.java new file mode 100644 index 0000000..6c3b089 --- /dev/null +++ b/src/test/java/com/cleverthis/interview/PerformanceAnalyze.java @@ -0,0 +1,47 @@ +package com.cleverthis.interview; + +import com.cleverthis.interview.padlock.PadlockImpl; + +/** + * Performance test but not mean to run in unit test. + */ +public class PerformanceAnalyze { + private static void solve(PadlockImpl padlock) { + // TODO + } + + private static final String RUN_NAME = "Boilerplate"; + private static final int TOTAL_RUN = 50; + private static final int NUMPAD_SIZE = 7; + + static { + System.out.println("Run name: " + RUN_NAME); + System.out.println("Total run: " + TOTAL_RUN); + System.out.println("Numpad size: " + NUMPAD_SIZE); + } + + public static void main(String[] args) { + long timeSum = 0; + long writeSum = 0; + for (int i = 0; i < TOTAL_RUN; i++) { + PadlockImpl padlock = new PadlockImpl(NUMPAD_SIZE); + padlock.resetCounter(); + long start = System.currentTimeMillis(); + solve(padlock); + long end = System.currentTimeMillis(); + if (!padlock.isPasscodeCorrect()) throw new IllegalStateException( + "Invalid solution: passcode not correct after return"); + long dT = end - start; + timeSum += dT; + writeSum += padlock.getWriteCounter(); + System.out.println("Run #" + (i + 1) + ": time: " + dT + "ms; write: " + padlock.getWriteCounter()); + } + System.out.println("Run time sum: " + timeSum + "ms"); + System.out.println("Write sum: " + writeSum); + double avgTime = timeSum / (double) TOTAL_RUN; + double avgWrite = writeSum / (double) TOTAL_RUN; + System.out.println("Avg run time: " + avgTime + "ms"); + System.out.println("Avg write: " + avgWrite); + System.out.println("Calculated estimate avg run time: " + (avgTime / 1000 + avgTime) + "s"); + } +} diff --git a/src/test/java/com/cleverthis/interview/SolutionTest.java b/src/test/java/com/cleverthis/interview/SolutionTest.java index 64a0cdc..26eaf92 100644 --- a/src/test/java/com/cleverthis/interview/SolutionTest.java +++ b/src/test/java/com/cleverthis/interview/SolutionTest.java @@ -1,30 +1,13 @@ package com.cleverthis.interview; import com.cleverthis.interview.padlock.PadlockImpl; -import org.junit.jupiter.api.Test; - -import java.util.Random; - -import static org.junit.jupiter.api.Assertions.*; /** * This is a simple placeholder to show how unit test works. - * You can replace it with your own test. */ -class SolutionTest { - private void solve(PadlockImpl padlock) { +class SolutionTest extends SolutionTestBase { + @Override + protected void solve(PadlockImpl padlock) { new Solution().solve(padlock); } - - @Test - void verify(){ - Random random = new Random(); - PadlockImpl padlock = new PadlockImpl(random.nextInt(1, 8)); - - long startTime = System.currentTimeMillis(); - solve(padlock); - long endTime = System.currentTimeMillis(); - assertTrue(padlock.isPasscodeCorrect()); - System.out.println("Time usage: " + (endTime - startTime) + "ms"); - } } \ No newline at end of file diff --git a/src/test/java/com/cleverthis/interview/SolutionTestBase.java b/src/test/java/com/cleverthis/interview/SolutionTestBase.java new file mode 100644 index 0000000..cbe86e5 --- /dev/null +++ b/src/test/java/com/cleverthis/interview/SolutionTestBase.java @@ -0,0 +1,30 @@ +package com.cleverthis.interview; + +import com.cleverthis.interview.padlock.PadlockImpl; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * This is a base class for verifying the correctness of the solution. + */ +public abstract class SolutionTestBase { + + /** + * Implement your solution in this function. + * */ + protected abstract void solve(PadlockImpl padlock); + + protected void verify(int numpadSize) { + PadlockImpl padlock = new PadlockImpl(numpadSize); + solve(padlock); + assertTrue(padlock.isPasscodeCorrect()); + } + + @Test + void verify1to7() { + for (int i = 1; i <= 7; i++) { + verify(i); + } + } +}