From d962f338c895503b3b1cc55fc4d75b57a2df673b Mon Sep 17 00:00:00 2001 From: Jery Jacob Arangath Date: Sun, 13 Jun 2021 11:40:08 +0530 Subject: [PATCH 1/5] Add Benchmarks --- .criterion/Single pixel/new/estimates.json | 1 + .criterion/Single pixel/new/sample.json | 1 + .criterion/Single pixel/new/tukey.json | 1 + .criterion/single pixel/new/estimates.json | 1 + .criterion/single pixel/new/sample.json | 1 + .criterion/single pixel/new/tukey.json | 1 + Cargo.lock | 548 ++++++++++++++++++++- Cargo.toml | 8 + {tests => benches}/main.rs | 26 +- 9 files changed, 560 insertions(+), 28 deletions(-) create mode 100644 .criterion/Single pixel/new/estimates.json create mode 100644 .criterion/Single pixel/new/sample.json create mode 100644 .criterion/Single pixel/new/tukey.json create mode 100644 .criterion/single pixel/new/estimates.json create mode 100644 .criterion/single pixel/new/sample.json create mode 100644 .criterion/single pixel/new/tukey.json rename {tests => benches}/main.rs (88%) diff --git a/.criterion/Single pixel/new/estimates.json b/.criterion/Single pixel/new/estimates.json new file mode 100644 index 0000000..05e2d55 --- /dev/null +++ b/.criterion/Single pixel/new/estimates.json @@ -0,0 +1 @@ +{"Mean":{"confidence_interval":{"confidence_level":0.95,"lower_bound":100087.14304294973,"upper_bound":105660.18233388735},"point_estimate":102648.37102685607,"standard_error":1427.4708552015632},"Median":{"confidence_interval":{"confidence_level":0.95,"lower_bound":95739.90610328638,"upper_bound":100005.83333333334},"point_estimate":97087.77680652682,"standard_error":1197.7661657633253},"MedianAbsDev":{"confidence_interval":{"confidence_level":0.95,"lower_bound":4695.385390716573,"upper_bound":10312.862155758132},"point_estimate":6755.377825508063,"standard_error":1505.554812028954},"Slope":{"confidence_interval":{"confidence_level":0.95,"lower_bound":104094.6497823457,"upper_bound":115069.06030600445},"point_estimate":108933.7632136348,"standard_error":2842.6264828624},"StdDev":{"confidence_interval":{"confidence_level":0.95,"lower_bound":8964.451360165,"upper_bound":20076.747117709485},"point_estimate":14345.38249583391,"standard_error":3051.841317598086}} \ No newline at end of file diff --git a/.criterion/Single pixel/new/sample.json b/.criterion/Single pixel/new/sample.json new file mode 100644 index 0000000..b0bcc60 --- /dev/null +++ b/.criterion/Single pixel/new/sample.json @@ -0,0 +1 @@ +[[12.0,24.0,36.0,48.0,60.0,72.0,84.0,96.0,108.0,120.0,132.0,144.0,156.0,168.0,180.0,192.0,204.0,216.0,228.0,240.0,252.0,264.0,276.0,288.0,300.0,312.0,324.0,336.0,348.0,360.0,372.0,384.0,396.0,408.0,420.0,432.0,444.0,456.0,468.0,480.0,492.0,504.0,516.0,528.0,540.0,552.0,564.0,576.0,588.0,600.0,612.0,624.0,636.0,648.0,660.0,672.0,684.0,696.0,708.0,720.0,732.0,744.0,756.0,768.0,780.0,792.0,804.0,816.0,828.0,840.0,852.0,864.0,876.0,888.0,900.0,912.0,924.0,936.0,948.0,960.0,972.0,984.0,996.0,1008.0,1020.0,1032.0,1044.0,1056.0,1068.0,1080.0,1092.0,1104.0,1116.0,1128.0,1140.0,1152.0,1164.0,1176.0,1188.0,1200.0],[1140400.0,2790000.0,3438900.0,5141100.0,5976200.0,6857300.0,9998700.0,11390500.0,11775400.0,11528400.0,12819600.0,13666000.0,14437800.0,14832900.0,18329400.0,17376400.0,19013200.0,19872300.0,21422200.0,23953600.0,24292100.0,28796100.0,24512300.0,27457100.0,30061500.0,30281900.0,29850600.0,30916800.0,31516200.0,35492400.0,39420300.0,41367800.0,37367100.0,41691500.0,41216800.0,42311000.0,40644600.0,43765400.0,44599200.0,43088400.0,47538600.0,48820800.0,48671200.0,50793500.0,51041900.0,51440500.0,57007000.0,53287300.0,55029700.0,54934900.0,57172000.0,58543800.0,59483700.0,70904400.0,63061100.0,62633900.0,64934500.0,68824900.0,65442800.0,67339000.0,67988900.0,70512700.0,72285500.0,74492900.0,118550100.0,78494900.0,76946800.0,81086900.0,79234700.0,77271400.0,81570400.0,83753100.0,114565700.0,94340800.0,94597800.0,86383200.0,107857400.0,113163400.0,103417200.0,112926400.0,110027300.0,108457900.0,109360400.0,108550900.0,122359800.0,117816300.0,107178500.0,109627600.0,111140700.0,109157200.0,123080700.0,140326200.0,133785600.0,125213200.0,118674000.0,128847200.0,116364600.0,230021200.0,139626700.0,151769700.0]] \ No newline at end of file diff --git a/.criterion/Single pixel/new/tukey.json b/.criterion/Single pixel/new/tukey.json new file mode 100644 index 0000000..2cd43cc --- /dev/null +++ b/.criterion/Single pixel/new/tukey.json @@ -0,0 +1 @@ +[53245.05768073173,73798.54407997121,128607.84114460985,149161.32754384936] \ No newline at end of file diff --git a/.criterion/single pixel/new/estimates.json b/.criterion/single pixel/new/estimates.json new file mode 100644 index 0000000..ec2ef4c --- /dev/null +++ b/.criterion/single pixel/new/estimates.json @@ -0,0 +1 @@ +{"Mean":{"confidence_interval":{"confidence_level":0.95,"lower_bound":79378.71157785057,"upper_bound":84114.75170172598},"point_estimate":81643.81677343912,"standard_error":1207.0764885975943},"Median":{"confidence_interval":{"confidence_level":0.95,"lower_bound":77214.16083916085,"upper_bound":80546.96356275304},"point_estimate":78400.48605240913,"standard_error":910.3124418937862},"MedianAbsDev":{"confidence_interval":{"confidence_level":0.95,"lower_bound":5302.332472881076,"upper_bound":9589.095483605674},"point_estimate":6946.342397858504,"standard_error":1058.5415168392733},"Slope":{"confidence_interval":{"confidence_level":0.95,"lower_bound":79397.85363350455,"upper_bound":83801.52501077761},"point_estimate":81477.93450114242,"standard_error":1122.8225159263054},"StdDev":{"confidence_interval":{"confidence_level":0.95,"lower_bound":9250.65257596628,"upper_bound":14663.917937719661},"point_estimate":12165.566855574716,"standard_error":1381.9362249756155}} \ No newline at end of file diff --git a/.criterion/single pixel/new/sample.json b/.criterion/single pixel/new/sample.json new file mode 100644 index 0000000..712c74b --- /dev/null +++ b/.criterion/single pixel/new/sample.json @@ -0,0 +1 @@ +[[13.0,26.0,39.0,52.0,65.0,78.0,91.0,104.0,117.0,130.0,143.0,156.0,169.0,182.0,195.0,208.0,221.0,234.0,247.0,260.0,273.0,286.0,299.0,312.0,325.0,338.0,351.0,364.0,377.0,390.0,403.0,416.0,429.0,442.0,455.0,468.0,481.0,494.0,507.0,520.0,533.0,546.0,559.0,572.0,585.0,598.0,611.0,624.0,637.0,650.0,663.0,676.0,689.0,702.0,715.0,728.0,741.0,754.0,767.0,780.0,793.0,806.0,819.0,832.0,845.0,858.0,871.0,884.0,897.0,910.0,923.0,936.0,949.0,962.0,975.0,988.0,1001.0,1014.0,1027.0,1040.0,1053.0,1066.0,1079.0,1092.0,1105.0,1118.0,1131.0,1144.0,1157.0,1170.0,1183.0,1196.0,1209.0,1222.0,1235.0,1248.0,1261.0,1274.0,1287.0,1300.0],[1277700.0,2213500.0,2775000.0,3513600.0,8012300.0,7645800.0,11188200.0,12949600.0,8429000.0,11839300.0,11155600.0,9763500.0,13272700.0,14193200.0,15495200.0,17287300.0,16781400.0,17417000.0,18654100.0,20244800.0,24168700.0,23755000.0,21251400.0,24831000.0,22892400.0,27103300.0,23061100.0,25761200.0,24955500.0,30027500.0,30578500.0,33746400.0,44185600.0,36398400.0,34063100.0,33428900.0,32725800.0,40276200.0,45191100.0,40562500.0,55800700.0,56144600.0,45575700.0,44166500.0,44734500.0,44985500.0,46506200.0,47407600.0,54819900.0,53851600.0,55116000.0,50692500.0,48804100.0,49811900.0,51161900.0,56976400.0,59685300.0,73767300.0,55670900.0,56505900.0,57154100.0,64642600.0,60645900.0,60110500.0,63339100.0,60598800.0,61537400.0,64611100.0,65520100.0,72675200.0,99458300.0,109072000.0,87408300.0,85787800.0,97121200.0,84960400.0,85104700.0,79791100.0,75225800.0,80602300.0,84429500.0,88103700.0,79811600.0,83438500.0,86006100.0,117336100.0,91636500.0,86976700.0,93388700.0,97507200.0,96682100.0,103690300.0,100415500.0,101354700.0,94461500.0,98798500.0,98337600.0,99082500.0,103774600.0,113035200.0]] \ No newline at end of file diff --git a/.criterion/single pixel/new/tukey.json b/.criterion/single pixel/new/tukey.json new file mode 100644 index 0000000..2a9b1f5 --- /dev/null +++ b/.criterion/single pixel/new/tukey.json @@ -0,0 +1 @@ +[47793.725490196084,61064.81146304676,96454.37405731523,109725.46003016591] \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 28be01d..279c96b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,13 +2,48 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a2e47a1fbe209ee101dd6d61285226744c6c8d3c21c8dc878ba6cb9f467f3a" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "approx" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" dependencies = [ - "num-traits", + "num-traits 0.2.14", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", ] [[package]] @@ -17,19 +52,123 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "backtrace" +version = "0.3.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282" +dependencies = [ + "addr2line", + "cc", + "cfg-if 1.0.0", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "cast" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "011941fb53da1a8ac3e4132a1becc367c44fe13f630769f3143d8c66c91c6cb6" + +[[package]] +name = "cc" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "criterion" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58b0200bf321214bdda8c797cf0071bcc638171c40ec198c3f652a4edaacde3" +dependencies = [ + "clap", + "criterion-plot", + "criterion-stats", + "failure", + "failure_derive", + "isatty", + "itertools 0.4.19", + "log 0.3.9", + "serde", + "serde_derive", + "serde_json", + "simplelog", +] + +[[package]] +name = "criterion-plot" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "885431f7865f9d4956b466126674e5ea40a0f193d42157e56630c356c5501957" +dependencies = [ + "byteorder", + "cast", + "itertools 0.5.10", +] + +[[package]] +name = "criterion-stats" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c71521cb4c7b7eac76b540e75447fb0172c4234d6333729001b886aaa21d6da4" +dependencies = [ + "cast", + "num-traits 0.1.43", + "num_cpus 0.2.13", + "rand 0.3.23", + "thread-scoped", +] + [[package]] name = "crossbeam-channel" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", ] @@ -39,7 +178,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-epoch", "crossbeam-utils", ] @@ -50,7 +189,7 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52fb27eab85b17fbb9f6fd667089e07d6a2eb8743d02639ee7f6a7a7729c9c94" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", "lazy_static", "memoffset", @@ -64,7 +203,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 1.0.0", "lazy_static", ] @@ -74,17 +213,51 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "getrandom" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] +[[package]] +name = "gimli" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" + [[package]] name = "hermit-abi" version = "0.1.18" @@ -94,6 +267,49 @@ dependencies = [ "libc", ] +[[package]] +name = "isatty" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "redox_syscall", + "winapi 0.3.9", +] + +[[package]] +name = "itertools" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a9b56eb56058f43dc66e58f40a214b2ccbc9f3df51861b63d51dec7b65bc3f" + +[[package]] +name = "itertools" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4833d6978da405305126af4ac88569b5d71ff758581ce5a987dbfa3755f694fc" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -106,6 +322,24 @@ version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" +[[package]] +name = "log" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" +dependencies = [ + "log 0.4.14", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "matrixmultiply" version = "0.3.1" @@ -115,6 +349,12 @@ dependencies = [ "rawpointer", ] +[[package]] +name = "memchr" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" + [[package]] name = "memoffset" version = "0.6.3" @@ -124,6 +364,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + [[package]] name = "nalgebra" version = "0.26.2" @@ -134,7 +384,7 @@ dependencies = [ "matrixmultiply", "num-complex", "num-rational", - "num-traits", + "num-traits 0.2.14", "simba", "typenum", ] @@ -145,7 +395,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" dependencies = [ - "num-traits", + "num-traits 0.2.14", ] [[package]] @@ -155,7 +405,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" dependencies = [ "autocfg", - "num-traits", + "num-traits 0.2.14", ] [[package]] @@ -166,7 +416,16 @@ checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" dependencies = [ "autocfg", "num-integer", - "num-traits", + "num-traits 0.2.14", +] + +[[package]] +name = "num-traits" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" +dependencies = [ + "num-traits 0.2.14", ] [[package]] @@ -178,6 +437,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3" +dependencies = [ + "libc", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -188,6 +456,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.25.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a38f2be3697a57b4060074ff41b44c16870d916ad7877c17696e063257482bc7" +dependencies = [ + "memchr", +] + [[package]] name = "paste" version = "1.0.5" @@ -200,6 +477,47 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "proc-macro2" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi 0.3.9", +] + [[package]] name = "rand" version = "0.8.3" @@ -208,7 +526,7 @@ checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.2", "rand_hc", ] @@ -219,9 +537,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.2", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", ] +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.2" @@ -237,7 +570,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core", + "rand_core 0.6.2", ] [[package]] @@ -268,24 +601,80 @@ dependencies = [ "crossbeam-deque", "crossbeam-utils", "lazy_static", - "num_cpus", + "num_cpus 1.13.0", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + [[package]] name = "rtrcrs" version = "0.1.0" dependencies = [ + "criterion", "nalgebra", - "rand", + "rand 0.8.3", "rayon", ] +[[package]] +name = "rustc-demangle" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "410f7acf3cb3a44527c5d9546bad4bf4e6c460915d5f9f2fc524498bfe8f70ce" + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "serde" +version = "1.0.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" + +[[package]] +name = "serde_derive" +version = "1.0.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "simba" version = "0.4.0" @@ -294,18 +683,145 @@ checksum = "5132a955559188f3d13c9ba831e77c802ddc8782783f050ed0c52f5988b95f4c" dependencies = [ "approx", "num-complex", - "num-traits", + "num-traits 0.2.14", "paste", ] +[[package]] +name = "simplelog" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24b615b1a3cc51ffa565d9a1d0cfcc49fe7d64737ada84eca284cddb0292d125" +dependencies = [ + "log 0.3.9", + "term", + "time", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "syn" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + +[[package]] +name = "term" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1" +dependencies = [ + "kernel32-sys", + "winapi 0.2.8", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "thread-scoped" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcbb6aa301e5d3b0b5ef639c9a9c7e2f1c944f177b460c04dc24c69b1fa2bd99" + +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi 0.3.9", +] + [[package]] name = "typenum" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 0d95716..26a860a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,11 @@ edition = "2018" nalgebra = "0.26" rand = "0.8.0" rayon = "1.5" + + +[dev-dependencies] +criterion = "0.1.2" + +[[bench]] +name = "main" +harness = false \ No newline at end of file diff --git a/tests/main.rs b/benches/main.rs similarity index 88% rename from tests/main.rs rename to benches/main.rs index 3494233..1690b30 100644 --- a/tests/main.rs +++ b/benches/main.rs @@ -1,9 +1,11 @@ -#![feature(test)] +// #![feature(test)] +#[macro_use] +extern crate criterion; -extern crate test; +use criterion::Criterion; -use std::sync::Arc; use rayon::prelude::*; +use std::sync::Arc; use rtrcrs::{ camera::Camera, @@ -20,7 +22,7 @@ use rtrcrs::{ const ASPECT_RATIO: f64 = 16.0 / 9.0; const IMAGE_WIDTH: i32 = 400; const IMAGE_HEIGHT: i32 = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as i32; -const SAMPLES_PER_PIXEL: i32 = 100; +const SAMPLES_PER_PIXEL: i32 = 8; const MAX_DEPTH: i32 = 50; struct RayTracer { @@ -96,18 +98,15 @@ impl RayTracer { } } -#[bench] -fn benchmark_single_pixel(b: &mut test::Bencher) { +fn benchmark_single_pixel(c: &mut Criterion) { let tracer = RayTracer::default(); - - b.iter(|| tracer.color(2, 3)); + c.bench_function("Single pixel", |b| b.iter(|| tracer.color(2, 3))); } -#[bench] -fn benchmark_image(b: &mut test::Bencher) { +fn benchmark_image(c: &mut Criterion) { let tracer = RayTracer::default(); - b.iter(|| { + c.bench_function("Image", |b|b.iter(|| { (0..IMAGE_HEIGHT) .into_par_iter() .rev() @@ -117,5 +116,8 @@ fn benchmark_image(b: &mut test::Bencher) { .collect::>() }) .collect::>() - }); + })); } + +criterion_group!(benches, benchmark_single_pixel, benchmark_image); +criterion_main!(benches); From c3d2657998e11e9b98b9453ea95df0f270dbf592 Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Tue, 22 Jun 2021 15:58:02 +0530 Subject: [PATCH 2/5] Add test for camera, ray-tracer --- src/camera.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/camera.rs b/src/camera.rs index a72688e..951b95d 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -58,3 +58,20 @@ impl Camera { ) } } + + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn ray_test() { + let vec = &Vec3::new(1.0, 1.0, 1.0); + let cam = Camera::new(vec, vec, vec, 1.0, 1.0, 1.0, 1.0); + let nan_vec = Vec3::new(f64::NAN, f64::NAN, f64::NAN); + let expected = format!("{:?}", Ray::new(nan_vec, nan_vec)); + let ray = format!("{:?}", cam.get_ray(1.0, 1.0)); + + assert_eq!(ray, expected); + } +} \ No newline at end of file From a5016db7ab778ab04851866635a900b710e3e3d5 Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Tue, 22 Jun 2021 17:18:09 +0530 Subject: [PATCH 3/5] Add tests to various definitions in rtrcrs --- src/definitions.rs | 57 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/definitions.rs b/src/definitions.rs index 02766a1..3662693 100644 --- a/src/definitions.rs +++ b/src/definitions.rs @@ -94,28 +94,21 @@ pub fn random_scene() -> HittableList { 1000.0, ground_material, ))); - let rand = || random_double(0.0, 1.0); - let rand_color = |min, max| { - Color::new( - random_double(min, max), - random_double(min, max), - random_double(min, max), - ) - }; + for a in -11..11 { for b in -11..11 { - let center = Point3::new(a as f64 + 0.9 * rand(), 0.2, b as f64 + 0.9 * rand()); + let center = Point3::new(a as f64 + 0.9, 0.2, b as f64 + 0.9); let point = center - Point3::new(4.0, 0.2, 0.0); if point.dot(&point).sqrt() > 0.9 { - match (rand() * 100.0) as u8 { + match ((a / b) as f64 * 100.0) as u8 { 0..=79 => { - let albedo = rand_color(0.0, 1.0); + let albedo = Color::new(0.8, 0.2, 0.3); let sphere_material = Arc::new(Lambertian::new(albedo)); world.add(Arc::new(Sphere::new(center, 0.2, sphere_material))); } 8..=94 => { - let albedo = rand_color(0.5, 1.0); - let fuzz = random_double(0.0, 0.5); + let albedo = Color::new(0.5, 0.8, 0.9); + let fuzz = 0.31415; let sphere_material = Arc::new(Metal::new(albedo, fuzz)); world.add(Arc::new(Sphere::new(center, 0.2, sphere_material))); } @@ -150,3 +143,41 @@ pub fn random_scene() -> HittableList { world } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn degrees_to_radians_test() { + assert_eq!(degrees_to_radians(1.0), 0.017453292519943295); + } + + #[test] + fn random_double_test() { + let r = random_double(0.5, 0.9); + assert!( r >= 0.5 && r <= 0.9); + } + + #[test] + fn near_zero_test() { + assert!(near_zero(Vec3::new(0.00000000001,0.00000000001,0.00000000001))); + } + + #[test] + fn reflect_test() { + let n = Vec3::new(0.1,0.1,0.1); + assert_eq!(reflect(&n,&n), Vec3::new(0.094, 0.094, 0.094)); + } + + #[test] + fn refract_test() { + let n = Vec3::new(0.1,0.1,0.1); + assert_eq!(refract(&n,&n, 0.1), Vec3::new(-0.09028588550390501, -0.09028588550390501, -0.09028588550390501)); + } + + #[test] + fn reflectance_test() { + assert_eq!(reflectance(0.1, 0.1), 0.8646247933884298); + } +} \ No newline at end of file From 6135b72093d61f2b93b58658f45487dc5a73b6d0 Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Tue, 22 Jun 2021 17:19:34 +0530 Subject: [PATCH 4/5] Refactor example into modular code --- examples/example.rs | 163 +++++++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 70 deletions(-) diff --git a/examples/example.rs b/examples/example.rs index 4972cad..823b86a 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -2,104 +2,127 @@ use rayon::prelude::*; use std::{ io::{stderr, Write}, sync::Arc, + time::Instant, }; use rtrcrs::{ camera::Camera, color::{anti_aliased, Color}, - definitions::{random_double, random_scene}, + definitions::random_double, + hittable_list::HittableList, material::{Dielectric, Lambertian, Metal}, ray::Point3, sphere::Sphere, Vec3, }; +// Image +const ASPECT_RATIO: f64 = 16.0 / 9.0; +const IMAGE_WIDTH: i32 = 1920; +const IMAGE_HEIGHT: i32 = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as i32; +const SAMPLES_PER_PIXEL: i32 = 100; +const MAX_DEPTH: i32 = 50; + +pub struct RayTracer { + world: HittableList, + camera: Camera, +} + +impl RayTracer { + pub fn default() -> Self { + //World + let mut world = HittableList::default(); + + let ground_material = Arc::new(Lambertian::new(Color::new(0.5, 0.5, 0.5))); + world.add(Arc::new(Sphere::new( + Point3::new(0.0, -1000.0, 0.0), + 1000.0, + ground_material, + ))); + + let material1 = Arc::new(Dielectric::new(1.5)); + world.add(Arc::new(Sphere::new( + Point3::new(0.0, 1.0, 0.0), + 1.0, + material1, + ))); + + let material2 = Arc::new(Lambertian::new(Color::new(0.4, 0.2, 0.1))); + world.add(Arc::new(Sphere::new( + Point3::new(-4.0, 1.0, 0.0), + 1.0, + material2, + ))); + + let material3 = Arc::new(Metal::new(Color::new(0.7, 0.6, 0.5), 0.0)); + world.add(Arc::new(Sphere::new( + Point3::new(4.0, 1.0, 0.0), + 1.0, + material3, + ))); + + // Camera + let camera = Camera::new( + &Point3::new(13.0, 2.0, 3.0), + &Point3::new(0.0, 0.0, 0.0), + &Vec3::new(0.0, 1.0, 0.0), + 20.0, + ASPECT_RATIO, + 0.1, + 10.0, + ); + + Self { world, camera } + } + + pub fn render(&self, i: u16, j: u16) -> Vec { + let pixel_color: Color = (0..SAMPLES_PER_PIXEL) + .map(|_| { + let (u, v) = ( + (i as f64 + random_double(0.0, 1.0)) / (IMAGE_WIDTH - 1) as f64, + (j as f64 + random_double(0.0, 1.0)) / (IMAGE_HEIGHT - 1) as f64, + ); + self.camera.get_ray(u, v).color(&self.world, MAX_DEPTH) + }) + .sum(); + anti_aliased(pixel_color, SAMPLES_PER_PIXEL) + .iter() + .map(|c| (c * 255.9) as u8) + .collect::>() + } +} + fn main() { - // Image - const ASPECT_RATIO: f64 = 16.0 / 9.0; - const IMAGE_WIDTH: i32 = 400; - const IMAGE_HEIGHT: i32 = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as i32; - const SAMPLES_PER_PIXEL: i32 = 100; - const MAX_DEPTH: i32 = 50; - - //World - let mut world = random_scene(); - - let material_ground = Arc::new(Lambertian::new(Color::new(0.8, 0.8, 0.0))); - let material_center = Arc::new(Lambertian::new(Color::new(0.1, 0.2, 0.5))); - let material_left = Arc::new(Dielectric::new(1.5)); - let material_right = Arc::new(Metal::new(Color::new(0.8, 0.6, 0.2), 0.0)); - - world.add(Arc::new(Sphere::new( - Point3::new(0.0, -100.5, -1.0), - 100.0, - material_ground, - ))); - world.add(Arc::new(Sphere::new( - Point3::new(-1.0, 0.0, -1.0), - 0.5, - material_left.clone(), - ))); - world.add(Arc::new(Sphere::new( - Point3::new(-1.0, 0.0, -1.0), - -0.45, - material_left, - ))); - world.add(Arc::new(Sphere::new( - Point3::new(1.0, 0.0, -1.0), - 0.5, - material_right, - ))); - world.add(Arc::new(Sphere::new( - Point3::new(0.0, 0.0, -1.0), - 0.5, - material_center, - ))); - - // Camera - let camera = Camera::new( - &Point3::new(13.0, 2.0, 3.0), - &Point3::new(0.0, 0.0, 0.0), - &Vec3::new(0.0, 1.0, 0.0), - 20.0, - ASPECT_RATIO, - 0.1, - 10.0, - ); + let now = Instant::now(); + let tracer = Arc::new(RayTracer::default()); // Render println!("P3\n{} {}\n255", IMAGE_WIDTH, IMAGE_HEIGHT); - std::env::set_var("RAYON_NUM_THREADS", "4"); + // std::env::set_var("RAYON_NUM_THREADS", "4"); let image = (0..IMAGE_HEIGHT) .into_par_iter() .rev() .flat_map(|j| { eprint!("\rScanlines remaining: {} ", j); stderr().flush().unwrap(); - (0..IMAGE_WIDTH) - .flat_map(|i| { - let pixel_color: Color = (0..SAMPLES_PER_PIXEL) - .map(|_| { - let (u, v) = ( - (i as f64 + random_double(0.0, 1.0)) / (IMAGE_WIDTH - 1) as f64, - (j as f64 + random_double(0.0, 1.0)) / (IMAGE_HEIGHT - 1) as f64, - ); - camera.get_ray(u, v).color(&world, MAX_DEPTH) - }) - .sum(); - anti_aliased(pixel_color, SAMPLES_PER_PIXEL) - .iter() - .map(|c| (c * 255.9) as u8) - .collect::>() - }) - .collect::>() + let mut scanline = vec![]; + for i in 0..IMAGE_WIDTH { + eprint!("\r[{}, {}]", i, j); + stderr().flush().unwrap(); + scanline.append(&mut tracer.render(i as u16, j as u16)); + } + scanline }) .collect::>(); + eprintln!("Pixel rendering: {} nanoseconds", now.elapsed().as_nanos()); + for pc in image.chunks(3) { println!("{} {} {}", pc[0], pc[1], pc[2]); } + eprintln!("Pixel printing: {} nanoseconds", now.elapsed().as_nanos()); + eprintln!("\rImage Generated."); stderr().flush().unwrap(); } From a816951fdf212d4e6a8377fe212eead9ed556f0f Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Wed, 23 Jun 2021 12:45:14 +0530 Subject: [PATCH 5/5] Add minimum 1 test per unit --- benches/main.rs | 24 +++++++++++++----------- src/camera.rs | 3 +-- src/color.rs | 13 +++++++++++++ src/definitions.rs | 26 +++++++++++++++++++------- src/hittable.rs | 16 ++++++++++++++++ src/hittable_list.rs | 15 +++++++++++++++ src/material.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/ray.rs | 1 + src/sphere.rs | 16 ++++++++++++++++ tests/images.rs | 1 + 10 files changed, 137 insertions(+), 20 deletions(-) create mode 100644 tests/images.rs diff --git a/benches/main.rs b/benches/main.rs index 1690b30..a7f41ef 100644 --- a/benches/main.rs +++ b/benches/main.rs @@ -106,17 +106,19 @@ fn benchmark_single_pixel(c: &mut Criterion) { fn benchmark_image(c: &mut Criterion) { let tracer = RayTracer::default(); - c.bench_function("Image", |b|b.iter(|| { - (0..IMAGE_HEIGHT) - .into_par_iter() - .rev() - .flat_map(|j| { - (0..IMAGE_WIDTH) - .flat_map(|i| tracer.color(i, j)) - .collect::>() - }) - .collect::>() - })); + c.bench_function("Image", |b| { + b.iter(|| { + (0..IMAGE_HEIGHT) + .into_par_iter() + .rev() + .flat_map(|j| { + (0..IMAGE_WIDTH) + .flat_map(|i| tracer.color(i, j)) + .collect::>() + }) + .collect::>() + }) + }); } criterion_group!(benches, benchmark_single_pixel, benchmark_image); diff --git a/src/camera.rs b/src/camera.rs index 951b95d..1ae09aa 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -59,7 +59,6 @@ impl Camera { } } - #[cfg(test)] mod test { use super::*; @@ -74,4 +73,4 @@ mod test { assert_eq!(ray, expected); } -} \ No newline at end of file +} diff --git a/src/color.rs b/src/color.rs index 3872a02..eeea540 100644 --- a/src/color.rs +++ b/src/color.rs @@ -7,3 +7,16 @@ pub fn anti_aliased(color: Color, samples_per_pixel: i32) -> Color { Color::new(sample(color.x), sample(color.y), sample(color.z)) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn anti_aliasing_test() { + let c = Color::new(0.9, 0.2, 0.1); + let aac = anti_aliased(c, 2); + + assert_eq!(aac, Color::new(0.6708203932499369, 0.31622776601683794, 0.22360679774997896)); + } +} \ No newline at end of file diff --git a/src/definitions.rs b/src/definitions.rs index 3662693..0b97c89 100644 --- a/src/definitions.rs +++ b/src/definitions.rs @@ -85,6 +85,7 @@ pub fn reflectance(cosine: f64, ref_idx: f64) -> f64 { r0 + (1.0 - r0) * (1.0 - cosine).powi(5) } +/// Generates a random scene HittableList pub fn random_scene() -> HittableList { let mut world = HittableList::default(); @@ -156,28 +157,39 @@ mod test { #[test] fn random_double_test() { let r = random_double(0.5, 0.9); - assert!( r >= 0.5 && r <= 0.9); + assert!(r >= 0.5 && r <= 0.9); } #[test] fn near_zero_test() { - assert!(near_zero(Vec3::new(0.00000000001,0.00000000001,0.00000000001))); + assert!(near_zero(Vec3::new( + 0.00000000001, + 0.00000000001, + 0.00000000001 + ))); } #[test] fn reflect_test() { - let n = Vec3::new(0.1,0.1,0.1); - assert_eq!(reflect(&n,&n), Vec3::new(0.094, 0.094, 0.094)); + let n = Vec3::new(0.1, 0.1, 0.1); + assert_eq!(reflect(&n, &n), Vec3::new(0.094, 0.094, 0.094)); } #[test] fn refract_test() { - let n = Vec3::new(0.1,0.1,0.1); - assert_eq!(refract(&n,&n, 0.1), Vec3::new(-0.09028588550390501, -0.09028588550390501, -0.09028588550390501)); + let n = Vec3::new(0.1, 0.1, 0.1); + assert_eq!( + refract(&n, &n, 0.1), + Vec3::new( + -0.09028588550390501, + -0.09028588550390501, + -0.09028588550390501 + ) + ); } #[test] fn reflectance_test() { assert_eq!(reflectance(0.1, 0.1), 0.8646247933884298); } -} \ No newline at end of file +} diff --git a/src/hittable.rs b/src/hittable.rs index 88b099c..942f48a 100644 --- a/src/hittable.rs +++ b/src/hittable.rs @@ -39,3 +39,19 @@ pub trait Hittable: Sync + Send { /// Defines the hit operation on the object with the provided Ray. fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option; } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn set_face_normal_test() { + let mut hr = HitRecord::default(); + let on = Vec3::new(1.5, 0.1, 0.9); + let r = Ray::new(on, on); + hr.set_face_normal(&r, &on); + + assert_eq!(hr.front_face, false); + assert_eq!(hr.normal, -on); + } +} diff --git a/src/hittable_list.rs b/src/hittable_list.rs index e3b1ae6..fcb3520 100644 --- a/src/hittable_list.rs +++ b/src/hittable_list.rs @@ -40,3 +40,18 @@ impl Hittable for HittableList { hit_anything } } + +#[cfg(test)] +mod test { + use super::*; + use crate::Vec3; + + #[test] + fn hit_nothing_test() { + let hl = HittableList::default(); + let v = Vec3::new(0.1, 0.1, 0.1); + let r = Ray::new(v, v); + + assert!(hl.hit(&r, 0.1, 0.1).is_none()); + } +} diff --git a/src/material.rs b/src/material.rs index da265dc..72a82a6 100644 --- a/src/material.rs +++ b/src/material.rs @@ -90,3 +90,45 @@ impl Material for Dielectric { Some((Color::new(1.0, 1.0, 1.0), Ray::new(rec.point, direction))) } } + +#[cfg(test)] +mod test { + use super::*; + use crate::Vec3; + + #[test] + fn lambertian_test() { + let l = Lambertian::new(Color::new(0.4, 0.5, 0.6)); + let on = Vec3::new(1.5, 0.1, 0.9); + let r = Ray::new(on, on); + let hr = HitRecord::default(); + let s = l.scatter(&r, &hr, &r); + assert!(s.is_some()); + + let (sc, _) = s.unwrap(); + assert_eq!(sc, Color::new(0.4, 0.5, 0.6)); + } + + #[test] + fn metal_test() { + let m = Metal::new(Color::new(0.4, 0.5, 0.6), 0.5); + let on = Vec3::new(1.2, 1.0, 1.1); + let r = Ray::new(on, on); + let hr = HitRecord::default(); + let s = m.scatter(&r, &hr, &r); + assert!(s.is_none()); + } + + #[test] + fn dielectric_test() { + let d = Dielectric::new(0.6); + let on = Vec3::new(1.5, 0.1, 0.9); + let r = Ray::new(on, on); + let hr = HitRecord::default(); + let s = d.scatter(&r, &hr, &r); + assert!(s.is_some()); + + let (sc, _) = s.unwrap(); + assert_eq!(sc, Color::new(1.0, 1.0, 1.0)); + } +} diff --git a/src/ray.rs b/src/ray.rs index c9fe666..137ff0f 100644 --- a/src/ray.rs +++ b/src/ray.rs @@ -67,6 +67,7 @@ mod tests { assert_eq!(ray.direction(), dir); assert_eq!(ray.at(3.0), origin + dir * 3.0); } + #[test] fn color_test() { let origin = Point3::new(3.0, 2.0, 1.0); diff --git a/src/sphere.rs b/src/sphere.rs index 41f91c2..5050550 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -62,3 +62,19 @@ impl Hittable for Sphere { Some(rec) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::material::Metal; + + #[test] + fn sphere_test() { + let v = Point3::new(3.0, 2.0, 1.0); + let s = Sphere::new(v, 0.5, Arc::new(Metal::new(v, 0.1))); + let r = Ray::new(v, v); + let h = s.hit(&r, 0.3, 0.4); + + assert!(h.is_none()); + } +} \ No newline at end of file diff --git a/tests/images.rs b/tests/images.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/images.rs @@ -0,0 +1 @@ +