|
|
@ -3,9 +3,8 @@ package com.sothr.imagetools.engine.hash |
|
|
|
import java.io.File |
|
|
|
import javax.imageio.ImageIO |
|
|
|
|
|
|
|
import com.sothr.imagetools.engine.{TestParams, BaseTest, AppConfig} |
|
|
|
import com.sothr.imagetools.engine.dto.ImageHashDTO |
|
|
|
import com.sothr.imagetools.TestParams |
|
|
|
import com.sothr.imagetools.engine.{AppConfig, BaseTest, TestParams} |
|
|
|
import net.sf.ehcache.Element |
|
|
|
|
|
|
|
import scala.collection.mutable |
|
|
@ -20,10 +19,10 @@ class HashServiceTest extends BaseTest { |
|
|
|
// Define the number of runs the benchmarking tests should use |
|
|
|
val benchmarkRuns = 10 |
|
|
|
|
|
|
|
def dhashTestCase(filePath:String):Long = { |
|
|
|
val sample = new File(filePath) |
|
|
|
val image = ImageIO.read(sample) |
|
|
|
HashService.getDhash(image) |
|
|
|
def dhashTestCase(filePath: String): Long = { |
|
|
|
val sample = new File(filePath) |
|
|
|
val image = ImageIO.read(sample) |
|
|
|
HashService.getDhash(image) |
|
|
|
} |
|
|
|
|
|
|
|
test("Benchmark DHash") { |
|
|
@ -31,21 +30,27 @@ class HashServiceTest extends BaseTest { |
|
|
|
info("DHash Large Image 3684x2736") |
|
|
|
val time = new mutable.MutableList[Long]() |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { dhashTestCase(TestParams.LargeSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
dhashTestCase(TestParams.LargeSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val largeMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms") |
|
|
|
time.clear() |
|
|
|
info("DHash Medium Image 1824x1368") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { dhashTestCase(TestParams.MediumSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
dhashTestCase(TestParams.MediumSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val mediumMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms") |
|
|
|
time.clear() |
|
|
|
info("DHash Small Image 912x684") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { dhashTestCase(TestParams.SmallSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
dhashTestCase(TestParams.SmallSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val smallMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms") |
|
|
@ -54,15 +59,15 @@ class HashServiceTest extends BaseTest { |
|
|
|
} |
|
|
|
|
|
|
|
test("Confirm Largest DHash Output ") { |
|
|
|
val testData:Array[Array[Int]] = Array( |
|
|
|
Array(1,2,3,4,5,6,7,8), |
|
|
|
Array(16,15,14,13,12,11,10,9), |
|
|
|
Array(17,18,19,20,21,22,23,24), |
|
|
|
Array(32,31,30,29,28,27,26,25), |
|
|
|
Array(33,34,35,36,37,38,39,40), |
|
|
|
Array(48,47,46,45,44,43,42,41), |
|
|
|
Array(49,50,51,52,53,54,55,56), |
|
|
|
Array(64,63,62,61,60,59,58,57)) |
|
|
|
val testData: Array[Array[Int]] = Array( |
|
|
|
Array(1, 2, 3, 4, 5, 6, 7, 8), |
|
|
|
Array(16, 15, 14, 13, 12, 11, 10, 9), |
|
|
|
Array(17, 18, 19, 20, 21, 22, 23, 24), |
|
|
|
Array(32, 31, 30, 29, 28, 27, 26, 25), |
|
|
|
Array(33, 34, 35, 36, 37, 38, 39, 40), |
|
|
|
Array(48, 47, 46, 45, 44, 43, 42, 41), |
|
|
|
Array(49, 50, 51, 52, 53, 54, 55, 56), |
|
|
|
Array(64, 63, 62, 61, 60, 59, 58, 57)) |
|
|
|
val hash = DHash.getHash(testData) |
|
|
|
debug(s"Hash of test array: $hash") |
|
|
|
assert(hash == Long.MaxValue) |
|
|
@ -78,7 +83,7 @@ class HashServiceTest extends BaseTest { |
|
|
|
debug(s"Testing that $hash = 4004374827879799635L") |
|
|
|
assert(hash == 4004374827879799635L) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
test("Calculate DHash Medium Sample Image 1") { |
|
|
|
debug("Starting 'Calculate DHash Medium Sample Image 1' test") |
|
|
|
val sample = new File(TestParams.MediumSampleImage1) |
|
|
@ -89,7 +94,7 @@ class HashServiceTest extends BaseTest { |
|
|
|
debug(s"Testing that $hash = 4004374827879799635L") |
|
|
|
assert(hash == 4004374827879799635L) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
test("Calculate DHash Small Sample Image 1") { |
|
|
|
debug("Starting 'Calculate DHash Small Sample Image 1' test") |
|
|
|
val sample = new File(TestParams.SmallSampleImage1) |
|
|
@ -100,17 +105,17 @@ class HashServiceTest extends BaseTest { |
|
|
|
debug(s"Testing that $hash = 4004383623972821843L") |
|
|
|
assert(hash == 4004383623972821843L) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
test("DHash Of Large, Medium, And Small Sample 1 Must Be Similar") { |
|
|
|
val largeHash = dhashTestCase(TestParams.LargeSampleImage1) |
|
|
|
val mediumHash = dhashTestCase(TestParams.MediumSampleImage1) |
|
|
|
val smallHash = dhashTestCase(TestParams.SmallSampleImage1) |
|
|
|
assert(HashService.areDhashSimilar(largeHash,mediumHash)) |
|
|
|
assert(HashService.areDhashSimilar(largeHash,smallHash)) |
|
|
|
assert(HashService.areDhashSimilar(mediumHash,smallHash)) |
|
|
|
assert(HashService.areDhashSimilar(largeHash, mediumHash)) |
|
|
|
assert(HashService.areDhashSimilar(largeHash, smallHash)) |
|
|
|
assert(HashService.areDhashSimilar(mediumHash, smallHash)) |
|
|
|
} |
|
|
|
|
|
|
|
def ahashTestCase(filePath:String):Long = { |
|
|
|
def ahashTestCase(filePath: String): Long = { |
|
|
|
val sample = new File(filePath) |
|
|
|
val image = ImageIO.read(sample) |
|
|
|
HashService.getAhash(image) |
|
|
@ -121,21 +126,27 @@ class HashServiceTest extends BaseTest { |
|
|
|
info("AHash Large Image 3684x2736") |
|
|
|
val time = new mutable.MutableList[Long]() |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { ahashTestCase(TestParams.LargeSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
ahashTestCase(TestParams.LargeSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val largeMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms") |
|
|
|
time.clear() |
|
|
|
info("AHash Medium Image 1824x1368") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { ahashTestCase(TestParams.MediumSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
ahashTestCase(TestParams.MediumSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val mediumMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms") |
|
|
|
time.clear() |
|
|
|
info("AHash Small Image 912x684") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { ahashTestCase(TestParams.SmallSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
ahashTestCase(TestParams.SmallSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val smallMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms") |
|
|
@ -180,12 +191,12 @@ class HashServiceTest extends BaseTest { |
|
|
|
val largeHash = ahashTestCase(TestParams.LargeSampleImage1) |
|
|
|
val mediumHash = ahashTestCase(TestParams.MediumSampleImage1) |
|
|
|
val smallHash = ahashTestCase(TestParams.SmallSampleImage1) |
|
|
|
assert(HashService.areAhashSimilar(largeHash,mediumHash)) |
|
|
|
assert(HashService.areAhashSimilar(largeHash,smallHash)) |
|
|
|
assert(HashService.areAhashSimilar(mediumHash,smallHash)) |
|
|
|
assert(HashService.areAhashSimilar(largeHash, mediumHash)) |
|
|
|
assert(HashService.areAhashSimilar(largeHash, smallHash)) |
|
|
|
assert(HashService.areAhashSimilar(mediumHash, smallHash)) |
|
|
|
} |
|
|
|
|
|
|
|
def phashTestCase(filePath:String):Long = { |
|
|
|
def phashTestCase(filePath: String): Long = { |
|
|
|
val sample = new File(filePath) |
|
|
|
val image = ImageIO.read(sample) |
|
|
|
HashService.getPhash(image) |
|
|
@ -196,21 +207,27 @@ class HashServiceTest extends BaseTest { |
|
|
|
info("PHash Large Image 3684x2736") |
|
|
|
val time = new mutable.MutableList[Long]() |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { phashTestCase(TestParams.LargeSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
phashTestCase(TestParams.LargeSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val largeMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms") |
|
|
|
time.clear() |
|
|
|
info("PHash Medium Image 1824x1368") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { phashTestCase(TestParams.MediumSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
phashTestCase(TestParams.MediumSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val mediumMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms") |
|
|
|
time.clear() |
|
|
|
info("PHash Small Image 912x684") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { phashTestCase(TestParams.SmallSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
phashTestCase(TestParams.SmallSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val smallMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms") |
|
|
@ -255,12 +272,12 @@ class HashServiceTest extends BaseTest { |
|
|
|
val largeHash = phashTestCase(TestParams.LargeSampleImage1) |
|
|
|
val mediumHash = phashTestCase(TestParams.MediumSampleImage1) |
|
|
|
val smallHash = phashTestCase(TestParams.SmallSampleImage1) |
|
|
|
assert(HashService.arePhashSimilar(largeHash,mediumHash)) |
|
|
|
assert(HashService.arePhashSimilar(largeHash,smallHash)) |
|
|
|
assert(HashService.arePhashSimilar(mediumHash,smallHash)) |
|
|
|
assert(HashService.arePhashSimilar(largeHash, mediumHash)) |
|
|
|
assert(HashService.arePhashSimilar(largeHash, smallHash)) |
|
|
|
assert(HashService.arePhashSimilar(mediumHash, smallHash)) |
|
|
|
} |
|
|
|
|
|
|
|
def md5TestCase(filePath:String):String = { |
|
|
|
def md5TestCase(filePath: String): String = { |
|
|
|
HashService.getMD5(filePath) |
|
|
|
} |
|
|
|
|
|
|
@ -269,21 +286,27 @@ class HashServiceTest extends BaseTest { |
|
|
|
info("MD5 Large Image 3684x2736") |
|
|
|
val time = new mutable.MutableList[Long]() |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { md5TestCase(TestParams.LargeSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
md5TestCase(TestParams.LargeSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val largeMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms") |
|
|
|
time.clear() |
|
|
|
info("MD5 Medium Image 1824x1368") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { md5TestCase(TestParams.MediumSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
md5TestCase(TestParams.MediumSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val mediumMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms") |
|
|
|
time.clear() |
|
|
|
info("MD5 Small Image 912x684") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { md5TestCase(TestParams.SmallSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
md5TestCase(TestParams.SmallSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val smallMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms") |
|
|
@ -312,19 +335,19 @@ class HashServiceTest extends BaseTest { |
|
|
|
assert(hash == "b137131bd55896c747286e4d247b845e") |
|
|
|
} |
|
|
|
|
|
|
|
def imageHashTestWithCacheCase(filePath:String):ImageHashDTO = { |
|
|
|
def imageHashTestWithCacheCase(filePath: String): ImageHashDTO = { |
|
|
|
val cache = AppConfig.cacheManager.getCache("images") |
|
|
|
var result:ImageHashDTO = null |
|
|
|
var result: ImageHashDTO = null |
|
|
|
if (cache.get(filePath) != null) { |
|
|
|
result = cache.get(filePath).getObjectValue.asInstanceOf[ImageHashDTO] |
|
|
|
} else { |
|
|
|
result = imageHashTestCase(filePath) |
|
|
|
cache.put(new Element(filePath,result)) |
|
|
|
cache.put(new Element(filePath, result)) |
|
|
|
} |
|
|
|
result |
|
|
|
} |
|
|
|
|
|
|
|
def imageHashTestCase(filePath:String):ImageHashDTO = { |
|
|
|
def imageHashTestCase(filePath: String): ImageHashDTO = { |
|
|
|
HashService.getImageHashes(filePath) |
|
|
|
} |
|
|
|
|
|
|
@ -333,21 +356,27 @@ class HashServiceTest extends BaseTest { |
|
|
|
info("getImageHashes with cache Large Image 3684x2736") |
|
|
|
val time = new mutable.MutableList[Long]() |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { imageHashTestWithCacheCase(TestParams.LargeSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
imageHashTestWithCacheCase(TestParams.LargeSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val largeMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms") |
|
|
|
time.clear() |
|
|
|
info("getImageHashes with cache Medium Image 1824x1368") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { imageHashTestWithCacheCase(TestParams.MediumSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
imageHashTestWithCacheCase(TestParams.MediumSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val mediumMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms") |
|
|
|
time.clear() |
|
|
|
info("getImageHashes with cache Small Image 912x684") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { imageHashTestWithCacheCase(TestParams.SmallSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
imageHashTestWithCacheCase(TestParams.SmallSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val smallMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms") |
|
|
@ -360,21 +389,27 @@ class HashServiceTest extends BaseTest { |
|
|
|
info("getImageHashes Large Image 3684x2736") |
|
|
|
val time = new mutable.MutableList[Long]() |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { imageHashTestCase(TestParams.LargeSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
imageHashTestCase(TestParams.LargeSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val largeMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms") |
|
|
|
time.clear() |
|
|
|
info("getImageHashes Medium Image 1824x1368") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { imageHashTestCase(TestParams.MediumSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
imageHashTestCase(TestParams.MediumSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val mediumMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms") |
|
|
|
time.clear() |
|
|
|
info("getImageHashes Small Image 912x684") |
|
|
|
for (runNum <- 0 until benchmarkRuns) { |
|
|
|
time += getTime { imageHashTestCase(TestParams.SmallSampleImage1) } |
|
|
|
time += getTime { |
|
|
|
imageHashTestCase(TestParams.SmallSampleImage1) |
|
|
|
} |
|
|
|
} |
|
|
|
val smallMean = getMean(time.toArray[Long]) |
|
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms") |
|
|
@ -386,9 +421,9 @@ class HashServiceTest extends BaseTest { |
|
|
|
val largeHash = imageHashTestCase(TestParams.LargeSampleImage1) |
|
|
|
val mediumHash = imageHashTestCase(TestParams.MediumSampleImage1) |
|
|
|
val smallHash = imageHashTestCase(TestParams.SmallSampleImage1) |
|
|
|
assert(HashService.areImageHashesSimilar(largeHash,mediumHash)) |
|
|
|
assert(HashService.areImageHashesSimilar(largeHash,smallHash)) |
|
|
|
assert(HashService.areImageHashesSimilar(mediumHash,smallHash)) |
|
|
|
assert(HashService.areImageHashesSimilar(largeHash, mediumHash)) |
|
|
|
assert(HashService.areImageHashesSimilar(largeHash, smallHash)) |
|
|
|
assert(HashService.areImageHashesSimilar(mediumHash, smallHash)) |
|
|
|
} |
|
|
|
|
|
|
|
test("Calculate ImageHash Large Sample Image 1") { |
|
|
|