|
| 1 | +/* |
| 2 | + * Scala.js (https://www.scala-js.org/) |
| 3 | + * |
| 4 | + * Copyright EPFL. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (https://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package org.scalajs.linker |
| 14 | + |
| 15 | +import scala.concurrent._ |
| 16 | + |
| 17 | +import org.junit.Test |
| 18 | +import org.junit.Assert._ |
| 19 | + |
| 20 | +import org.scalajs.ir.Names._ |
| 21 | +import org.scalajs.ir.Trees._ |
| 22 | +import org.scalajs.ir.Types._ |
| 23 | + |
| 24 | +import org.scalajs.junit.async._ |
| 25 | + |
| 26 | +import org.scalajs.linker.analyzer._ |
| 27 | +import org.scalajs.linker.frontend.IRLoader |
| 28 | +import org.scalajs.linker.interface._ |
| 29 | +import org.scalajs.linker.standard._ |
| 30 | + |
| 31 | +import org.scalajs.linker.testutils._ |
| 32 | +import org.scalajs.linker.testutils.TestIRBuilder._ |
| 33 | + |
| 34 | +import org.scalajs.logging._ |
| 35 | + |
| 36 | +class LibrarySizeTest { |
| 37 | + import scala.concurrent.ExecutionContext.Implicits.global |
| 38 | + import LibrarySizeTest._ |
| 39 | + |
| 40 | + @Test |
| 41 | + def juRegexSize(): AsyncResult = await { |
| 42 | + val PatternClass = ClassName("java.util.regex.Pattern") |
| 43 | + val MatcherClass = ClassName("java.util.regex.Matcher") |
| 44 | + |
| 45 | + def line(pattern: String, flags: Int, input: String): Tree = { |
| 46 | + val compiledPattern = ApplyStatic(EAF, PatternClass, |
| 47 | + m("compile", List(T, I), ClassRef(PatternClass)), |
| 48 | + List(str(pattern), int(flags)))( |
| 49 | + ClassType(PatternClass)) |
| 50 | + |
| 51 | + val matcher = Apply(EAF, compiledPattern, |
| 52 | + m("matcher", List(ClassRef("java.lang.CharSequence")), ClassRef(MatcherClass)), |
| 53 | + List(str(input)))( |
| 54 | + ClassType(MatcherClass)) |
| 55 | + |
| 56 | + consoleLog(Apply(EAF, matcher, m("matches", Nil, Z), Nil)(BooleanType)) |
| 57 | + } |
| 58 | + |
| 59 | + val classDefs = Seq( |
| 60 | + mainTestClassDef(Block( |
| 61 | + line("[c-f]", 0, "d"), |
| 62 | + line("[c-f]", java.util.regex.Pattern.CASE_INSENSITIVE, "D") |
| 63 | + )) |
| 64 | + ) |
| 65 | + |
| 66 | + testLinkedSizes( |
| 67 | + expectedFastLinkSize = 185765, |
| 68 | + expectedFullLinkSizeWithoutClosure = 172906, |
| 69 | + expectedFullLinkSizeWithClosure = 31670, |
| 70 | + classDefs, |
| 71 | + moduleInitializers = MainTestModuleInitializers |
| 72 | + ) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +object LibrarySizeTest { |
| 77 | + private val reqsFactory = SymbolRequirement.factory("unit test") |
| 78 | + |
| 79 | + def testLinkedSizes(expectedFastLinkSize: Int, |
| 80 | + expectedFullLinkSizeWithoutClosure: Int, |
| 81 | + expectedFullLinkSizeWithClosure: Int, |
| 82 | + classDefs: Seq[ClassDef], |
| 83 | + symbolRequirements: SymbolRequirement = reqsFactory.none(), |
| 84 | + moduleInitializers: Seq[ModuleInitializer] = Nil, |
| 85 | + config: StandardConfig = StandardConfig())( |
| 86 | + implicit ec: ExecutionContext): Future[Unit] = { |
| 87 | + |
| 88 | + val logger = new ScalaConsoleLogger(Level.Error) |
| 89 | + |
| 90 | + val fullLinkConfig = config |
| 91 | + .withSemantics(_.optimized) |
| 92 | + .withClosureCompilerIfAvailable(true) |
| 93 | + |
| 94 | + val fastLinker = StandardImpl.linker(config) |
| 95 | + val fullLinker = StandardImpl.linker(fullLinkConfig) |
| 96 | + |
| 97 | + val classDefsFiles = classDefs.map(MemClassDefIRFile(_)) |
| 98 | + |
| 99 | + val fastOutput = MemOutputDirectory() |
| 100 | + val fullOutput = MemOutputDirectory() |
| 101 | + |
| 102 | + for { |
| 103 | + fulllib <- TestIRRepo.fulllib |
| 104 | + irFiles = fulllib ++ classDefsFiles |
| 105 | + fastLinkReport <- fastLinker.link(irFiles, moduleInitializers, fastOutput, logger) |
| 106 | + fullLinkReport <- fullLinker.link(irFiles, moduleInitializers, fullOutput, logger) |
| 107 | + } yield { |
| 108 | + val fastSize = fastOutput.content("main.js").get.length |
| 109 | + val fullSize = fullOutput.content("main.js").get.length |
| 110 | + |
| 111 | + val expectedFullLinkSize = |
| 112 | + if (fullLinkConfig.closureCompiler) expectedFullLinkSizeWithClosure |
| 113 | + else expectedFullLinkSizeWithoutClosure |
| 114 | + |
| 115 | + def roughlyEquals(expected: Int, actual: Int, tolerance: Int): Boolean = |
| 116 | + actual >= expected - tolerance && actual <= expected + tolerance |
| 117 | + |
| 118 | + if (!roughlyEquals(expectedFastLinkSize, fastSize, 500) || |
| 119 | + !roughlyEquals(expectedFullLinkSize, fullSize, 100)) { |
| 120 | + fail( |
| 121 | + s"FastLink expected $expectedFastLinkSize but got $fastSize\n" + |
| 122 | + s"FullLink expected $expectedFullLinkSize but got $fullSize") |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments