-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014-2017 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.takes.rs.xe; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import org.xembly.Directive; | ||
import org.xembly.Directives; | ||
|
||
/** | ||
* Xembly source to report memory usage. | ||
* | ||
* <p>Add this Xembly source to your page like this: | ||
* | ||
* <pre> new RsXembly( | ||
* new XsStylesheet("/xsl/home.xsl"), | ||
* new XsAppend( | ||
* "page", | ||
* new XsMemory() | ||
* ) | ||
* )</pre> | ||
* | ||
* <p>And expect this attribute in the XML: | ||
* | ||
* <pre><?xml version="1.0"?> | ||
* <?xml-stylesheet href="/xsl/home.xsl" type="text/xsl"?> | ||
* <page> | ||
* <memory>free="1344" max="2048" total="334"</memory> | ||
* </page> | ||
* </pre> | ||
* | ||
* <p>The class is immutable and thread-safe. | ||
* | ||
* @author Yegor Bugayenko (yegor256@gmail.com) | ||
* @version $Id$ | ||
* @since 1.2 | ||
*/ | ||
@EqualsAndHashCode(callSuper = true) | ||
public final class XeMemory extends XeWrap { | ||
|
||
/** | ||
* Ctor. | ||
*/ | ||
public XeMemory() { | ||
this("memory"); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param node Node name | ||
*/ | ||
public XeMemory(final CharSequence node) { | ||
super( | ||
new XeSource() { | ||
@Override | ||
public Iterable<Directive> toXembly() { | ||
return new Directives().add(node.toString()) | ||
.attr( | ||
"total", | ||
XeMemory.mbs(Runtime.getRuntime().totalMemory()) | ||
) | ||
.attr( | ||
"free", | ||
XeMemory.mbs(Runtime.getRuntime().freeMemory()) | ||
) | ||
.attr( | ||
"max", | ||
XeMemory.mbs(Runtime.getRuntime().maxMemory()) | ||
); | ||
} | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Format memory. | ||
* @param bytes Bytes | ||
* @return Mbytes | ||
*/ | ||
private static long mbs(final long bytes) { | ||
// @checkstyle MagicNumber (1 line) | ||
return bytes >> 20; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014-2017 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.takes.rs.xe; | ||
|
||
import com.jcabi.matchers.XhtmlMatchers; | ||
import java.io.IOException; | ||
import org.apache.commons.io.IOUtils; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test case for {@link XeMemory}. | ||
* @author Yegor Bugayenko (yegor256@gmail.com) | ||
* @version $Id$ | ||
* @since 1.2 | ||
*/ | ||
public final class XeMemoryTest { | ||
|
||
/** | ||
* XeMemory can build XML response. | ||
* @throws IOException If some problem inside | ||
*/ | ||
@Test | ||
public void buildsXmlResponse() throws IOException { | ||
MatcherAssert.assertThat( | ||
IOUtils.toString( | ||
new RsXembly( | ||
new XeAppend( | ||
"root", | ||
new XeMemory() | ||
) | ||
).body() | ||
), | ||
XhtmlMatchers.hasXPaths( | ||
"/root/memory[@total and @max and @free]" | ||
) | ||
); | ||
} | ||
|
||
} |