Understanding how SQL queries impact app performance, with dotTrace 6.1

Application performance depends on a huge variety of factors: code design, web server performance, possible network lags, and so on. Effective execution of SQL queries is an additional concern. Naturally, you want your profiler to cover as many of these performance aspects as possible.

In dotTrace 6.1 we introduced the long-awaited ‘SQL queries profiling’ feature. Now you can get exact data on how long a particular query took to execute, as well as what method ran the query.

Important notes:

  • SQL queries can be profiled only in the Timeline profiling mode.

  • The feature is available not only when profiling standalone applications, but also when you attach dotTrace to an already running process.

  • The following data providers are supported: SQLClient, OLE DB, Entity Framework, and ODBC.

  • As dotTrace uses ETW for collecting SQL events, profiling overhead is minimal. SQL query time is measured very accurately.

Now, let’s take a look at how it works.

How This Works

The only change you’ll notice in the UI is the new SQL Client filter in Interval Filters. Your workflow in Timeline Viewer is still straightforward: after profiling your app, you can start by looking at the Interval Filters.

Interval Filters

In our example, the app works with SQL for 497 ms. Let’s investigate how.

SQL Client Subfilters

Once the SQL Client filter is on, two more sub-filters appear: SQL Client: Connection String and SQL Client: Command. The first one is helpful in case your app uses several SQL connections. The second one shows how SQL Client time is distributed among particular SQL queries. In other words, it shows how long a particular query took to execute.

As query contents may be long, investigating them in the small Filters window could be problematic. That’s why we added a separate SQL Queries window which formats queries for a more convenient view:

SQL Queries Window

After you find a problematic query, the next step is to determine its origin, i.e. the method that called it. The SQL Queries window (like all other windows in Timeline Viewer) can work as a filter. Double-click a particular query, and all other filters (including Call Stack) will show data only for the time interval when this query executed:

Top Methods

Top Methods will show us the method that is responsible for running the query. Once you select the method in Top Methods, the Source View will show its code:

Source View

Thanks for reading! We hope you’ll find this new feature useful. Do try it on your own by downloading the latest dotTrace 6.1.

 

Posted in dotTrace Tips&Tricks, How-To's | Tagged , , | 3 Comments

C# 6.0 support in ReSharper 9.1

One of the interesting challenges in creating an IDE is tracking changes to a language as it evolves. ReSharper 9.0 shipped with early support for C# 6.0, but the language wasn’t done yet. Now that the design is stable, the recently released ReSharper 9.1 provides full support for the new language features. It obviously understands the new syntax, but also includes new functionality to take advantage of the changes in C# and to help bring your code up to date with the new language version.

In many respects, C# 6.0 is an incremental update to the language. There are no major features like LINQ or async/await. Instead, we have a number of syntax changes that reduce some of the boilerplate and ceremony of writing C#. Put together, these new features allow us to write code that is terser, shorter and easier to read.

Let’s take a look at how ReSharper supports some of these features.

Auto-properties

C# 6.0 adds two new features to auto-properties – you can make them getter only, and provide initializers for them. A getter only auto-property is different to a property with a private setter in that it is immutable. A private setter can be called from anywhere else in the class, but a read-only auto-property can only be set in the constructor, or via a property initializer, much like a field can be initialized where it’s declared.

ReSharper’s inspections spot when a property’s setter is only called from the constructor, and marks the unused setter as dead code. A quick Alt+Enter, and we’ve got a read-only auto-property:

Properties with redundant setter, converted to read-only auto property

Using static

While static methods are very useful, it can get a little verbose constantly having to qualify them with their class name. The ‘using static’ feature allows you to import the class in much the same way as you import a namespace. Once imported, you can access static members of that class without having to specify the class name. ReSharper offers Context Actions to easily import a class from the Alt+Enter menu, shows when qualifying the class name is redundant, and offers bulk cleanup:

Context action to convert references to the Math static class to a using static statement and back

Conditional access expressions

This is likely to be C# 6.0’s best loved feature. Null references are a fact of life to C# developers, and “if” statements have been the only way to handle them so far. The new conditional access operator “?.” will replace many of these null checks. Unlike the normal “.” dot access operator, the conditional access operator will not throw an exception if the preceding expression is null. Instead, it returns null and stops evaluating the rest of the expression. This is going to have a huge, positive impact on your codebase.

Convert if statements to conditional access operator

And of course, ReSharper understands how this operator (and indeed, the other changes to C# 6.0) affects the control flow of your code, and can use that to warn about possible null references, or redundant conditional access:

Control flow analysis of redundant conditional access operators

The nameof operator

C# is a nice, strongly-typed language. Usually, the compiler will check that we’re using valid type and member names, but this falls down sometimes, when we have to use “magic strings”, such as the argument name to ArgumentNullException, or a typename to Reflection, or property names for INotifyPropertyChanged. The compiler won’t check these values, and it’s easy to introduce errors, especially when we rename a type or member.

To help with these problems, ReSharper can resolve the argument name in ArgumentNullException, and has had extensive support for INotifyPropertyChanged since version 7. C# 5 introduced caller information attributes, and now C# 6.0 introduces a more generic mechanism – the nameof operator.

Instead of typing the name of a type or member as an opaque, constant string literal, you can now use the nameof operator and refer to the type or member as a symbol, and take advantage of C#’s strong typing, and compiler checking. If the symbol in the nameof operator is unknown, the compiler reports it as an error. And since it’s a symbol, ReSharper can include it in Find Usages, renames and will even provide suggestions to convert magic strings to usages of nameof:

Suggestion and quick fix to change magic string to nameof operator

String interpolation

This feature is intended to make it easier to format strings. On the face of it, string.Format isn’t exactly difficult, but again, it’s not type-safe. It relies on the order of format specifiers in a string, and the arguments passed being correct. If the order is wrong, the formatting is wrong, but you’ll only discover this at runtime. String interpolation provides a solution by specifying expressions inline, inside the format string. There’s no chance for mismatched specifiers and arguments, and no excuse for getting things in the wrong order.

ReSharper provides an inspection to convert string.Format calls to string interpolation, and naturally provides code completion and find usages and rename for members used in the expressions inside the interpolated strings:

Convert to interpolated string, show code completion and rename inside string

Expression-bodied members

Another feature designed to reduce the amount of boilerplate in your code. You can now define simple properties and methods using a lambda-like syntax, rather than wrapping the code in braces and newlines. They’re exactly the same as regular methods and getter only properties, but take less keystrokes. And, yes, we mean Alt+Enter.

Convert getter only property to expression bodied member

The real power of these features comes out when we combine them. Take this example. ReSharper shows us a suggestion that the null check can be replaced by the conditional access operator. Alt+Enter can quickly fix this up for us, and give us a simple return statement. Alt+Enter again invokes the “To expression body” context action, and we’ve now very quickly moved from a multi-line method to a simple, readable, single line method:

Putting it all together. Null propogation, inline variable, to expression bodied member

But what happens if you’ve got a C# 5.0 project, and you want to use the lovely new C# 6.0 features? By default, ReSharper won’t allow or suggest using new features for a project targeting an older version of the compiler. You can change the supported language level in the Properties window when the project is selected in Solution Explorer. Or take the easy route – if ReSharper complains that you’re using a newer feature, just use Alt+Enter to change the supported language level:

Enable language level from alt-enter

And of course, ReSharper understands the other changes to C# 6.0, such as dictionary initializers, exception filters and using await in catch/finally. The new C# 6.0 support requires Visual Studio 2015, and ReSharper 9.1, part of the ReSharper Ultimate suite of .NET tools.

Posted in ReSharper Tips&Tricks | Tagged , , , , | 4 Comments

Introducing ReSharper C++

Throughout its 11-year history, ReSharper has focused primarily on the .NET state of affairs. This commenced with support for C#, then VB.NET, and has slowly encompassed both the desktop (WinForms, WPF) as well as web (ASP.NET, HTML/CSS/JS/TS) languages and technologies.

Now, we are happy to announce the release of ReSharper C++ — a new product specifically targeted at modern C++ developers. Without wasting any more time, let’s talk about some of the features coming your way in this initial release.

Live Templates and Code Generation

ReSharper’s powerful Live Template ecosystem has been adapted to C++. This means that C++ benefits with support from the following:

  • Live templates act like Visual Studio’s snippets but are much more context-sensitive, offering hints as well as programmability options.
    Live templates in ReSharper C++
  • Surround templates let you pick a chunk of code and then surround it with a block of code such as an if or try-catch statement. Surround templates are instantly available on any selection via Alt+Enter:
    Surround templates in ReSharper C++
  • File templates let you generate either individual files or, indeed, several files at the same time. For example, the Class template can generate both the header and CPP files in one click.

In addition to generating code via live templates, ReSharper C++ supports quite an extensive Generate menu. Here’s a visual of some of the things you can generate:

Generate menu in ReSharper C++

Refactorings

An ability to change the structure of code while preserving correctness is ReSharper’s calling card, so here are some of the refactorings that are available out of the box:

  • Rename — this refactoring lets you seamlessly rename a symbol (e.g., a macros, a class or a field name) throughout the entire project. ReSharper automatically updates all usings of the symbol.
  • Introduce Variable — this refactoring lets you select a part of a calculation and introduce a new variable for it:
    Introduce Variable refactoring in ReSharper C++
  • Extract Method lets you extract a calculation into a separate function:
    Extract Method refactoring in ReSharper C++

Search & Navigation

ReSharper C++ lets you quickly navigate large code bases by providing the following navigation mechanisms:

  • Go to Everything lets you quickly find a particular entity (class, class member, file or folder name) in the entire solution. You can also filter the result set with Go to Type, Go to File or Folder and Go to Symbol.
  • Go to File Member lets you quickly find a member within the file you are currently working with.
  • You can Go to Base/Derived classes or class members and also quickly Go to Definition.
  • Go to Related Files lets you navigate to files related to your current position. This may involve, for example, a file with a declaration related to a definition you are working with, or an included file. Additionally, ReSharper supports an option to switch between header and source files.
  • Navigate to Specializations lets you find all the instances where a template is specialized:
    Navigate to Specializations in ReSharper C++

The Find Usages quickly finds all the locations where a particular symbol is used. It gives you a preview of what’s been found, with plenty of filtering and navigation options:

ReSharper provides plenty of contextual navigation, too. Pressing Alt+Enter on a symbol offers various navigation options:

Contect actions in ReSharper C++

In addition to the direct navigation facilities, ReSharper also provides a couple of tool windows for helping you navigate complex code bases. These include the File Structure window, which visualizes and helps you quickly navigate the structure of the currently opened file:

File Structure window in ReSharper C++

Another useful window is the Inheritance Hierarchy window which, as the name suggests, helps you quickly visualize the inheritance tree for a particular type:

Inheritance Hierarchy window in ReSharper C++

Finally, ReSharper provides general-purpose navigation options such as Go to Next/Previous Member, Go to Next/Previous Issue, and others. You can also jump to the containing declaration.

Code Analysis

As soon as you open up a C++ solution in Visual Studio, ReSharper starts analyzing your code and offering hints and suggestions. These may include things like having to add a namespace prefix and an #include statement:

Code Analysis in ReSharper C++

If you have a declaration without a definition, ReSharper will let you know, and will help you generate a definition – whether inline or in an implementation file:

Generate definition in ReSharper C++

If you are using a function such as printf(), ReSharper can check the arguments for you and offer fixes there, too:

printf() argument check in ReSharper C++

Macros and Templates

ReSharper processes macros and templates in their entirety, ensuring that the results of analyses always reflect the correctly postprocessed code. This lets it do some other things: for example, you can inline macro definitions:

Inline macro definitions with ReSharper C++

The same ideas of fidelity go towards macro evaluation: you can write a static_assert and ReSharper will instantly check this assert and complain if it doesn’t work:

static_assert checking in ReSharper C++

Conclusion

We have a video with a live demonstration of all of these features in action. Check it out:

With all that said, we invite you to check out ReSharper C++! It is bundled as an optional component of the ReSharper Ultimate installation — you can get a 30-day trial version here. Enjoy!

Posted in News and Events | Tagged , , , | 31 Comments

Updating from previous versions of ReSharper to 9.1 from within Visual Studio

Hello everyone,

Due to an unforeseen technical issue, updating from ReSharper 9.0.1 or ReSharper 9.1 EAP builds to ReSharper 9.1 RTM fails if you try to update from within ReSharper (via ReSharper | Help | Check for Updates). We apologize for the inconvenience that this issue might cause you and recommend the following workaround:

  1. Download ReSharper 9.1 RTM (full or web installer) from the web site
  2. Run the installer. It will automatically detect whether you have previous versions installed.
  3. If the Update option for ReSharper is not pre-selected, select it and proceed with the upgrade installation.
  4. Forgive us.

We hope that you enjoy the latest update despite this issue.

Posted in How-To's | Tagged , , | 5 Comments

Meet ReSharper 9.1, ReSharper C++ 1.0, dotTrace 6.1 and more ReSharper Ultimate product updates

We’ve just finalized a joint update to our .NET tools, added the first ever public version of ReSharper for C++, and as a result, the new release of ReSharper Ultimate is now available for download!

Specifically, this update consists of ReSharper 9.1, dotTrace 6.1, dotCover 3.1, dotMemory 4.3, dotPeek 1.4, and ReSharper C++ 1.0, a new product to join the ReSharper Ultimate family.

ReSharper Ultimate update

In addition to 770+ fixes, ReSharper 9.1 highlights include:

  • Improved support for Visual Studio 2015 and .NET 4.6. ReSharper 9.1 integrates actions based on Visual Studio Roslyn, so when you want to make changes to your code, you can choose either ReSharper or Visual Studio to do it for you, from the same list of Alt+Enter actions:
    Visual Studio actions in ReSharper menu
  • Better C #6.0 support that makes it easier to migrate an existing codebase to C# 6.0. In addition to such language constructs as static usings and exception filters, we’ve added support for string interpolation and the nameof() operator. To simplify the process of migrating your projects to C# 6.0, ReSharper now offers quick-fixes to transform your code in the scope of a file, project or the whole solution.Quick-fix to use nameof operator
  • JavaScript and TypeScript support improvements including JSDoc support, as well as improved TypeScript 1.5 and EcmaScript 6 support and full support for TypeScript 1.4.
  • New Evaluate expression context action that allows previewing the results of your code execution right in the editor. You can learn and play with most of .NET base class library APIs without even running your application. ReSharper can evaluate nearly full set of C# expressions, including LINQ and some new C# 6.0 constructs, so you get REPL-like experience directly in the code editor. Evaluate expression context action
  • Improved code completion: we’ve implemented a new mechanism that lets you order items by relevance so that the best fitting options are suggested higher in the code completion popup list.
  • Find types on NuGet. Copy-pasting code from StackOverflow has never been easier: When you have a type or namespace used inside your project that can’t be resolved to referenced libraries or packages, ReSharper can now search for this type or namespace in the NuGet package gallery, display the list of matching packages and easily download and install the package that you choose. As most great things about ReSharper, the search on NuGet starts with pressing Alt+Enter:
    Obtaining references from nuget.org in ReSharper 9.1
  • New Source Templates that can be created anywhere in the code of your project as extension methods and might be very handy in case you need to produce some repeatable code that is only relevant in your current project or solution.
    Source Templates in ReSharper 9.1

The other .NET tools in ReSharper Ultimate have been enhanced as well:

  • dotCover 3.1 enhances MSTest and WinStore tests support and ships numerous fixes for console tools.
  • dotTrace 6.1 receives the long-awaited SQL queries support in Timeline profiling: Now you can determine exactly how long a particular query executed for and what method ran the query.
  • The rich set of informative views in dotMemory 4.3 is extended with Dominators sunburst chart. With just one glance at the dominators’ hierarchy, you know what objects are crucial and how memory is retained in your application.
    Sunburst chart in dotMemory 4.3
  • Welcome dotMemory Unit framework — state-of-the-art .NET memory monitoring. Extend your unit testing framework with the functionality of a memory profiler. Please check more details in our recent blog post.
  • dotPeek 1.4 adds support for Visual Studio 2015 and C# 6.0.

In addition to these upgrades to our .NET tools, we are rolling out the inaugural release of ReSharper C++. A separate product for C++ developers who work in Visual Studio, ReSharper C++ inherits most features of ReSharper including its powerful navigation, coding assistance and code generation. To learn more, please visit the ReSharper C++ web site and watch out for new blog posts.

In terms of licensing and upgrades, there are several options available:

  • ReSharper 9.1 is a free upgrade for you if you have a valid ReSharper upgrade subscription or a per-major-version license to ReSharper 9. Updates to dotMemory, dotTrace and dotCover are free if you have a valid upgrade subscription to the respective product or to ReSharper Ultimate.
  • ReSharper C++ can be purchased as a separate license or as part of ReSharper Ultimate. For pricing and licensing options, see ReSharper C++ Buy page.
  • You may purchase a single license for ReSharper Ultimate, which includes ReSharper, ReSharper C++, dotTrace, dotCover, and dotMemory. Learn more about ReSharper Ultimate.
  • If you need a formal quote or any other assistance, you are welcome to contact JetBrains sales.
Posted in News and Events | Tagged , , , , , , , | 36 Comments

Exploring .NET Core with ReSharper Ultimate

We recently started the EAP for ReSharper 9.1, and it might have been easy to miss that the EAP is not just for ReSharper, but for ReSharper Ultimate – that is, our entire .NET tools product range. Starting with ReSharper 9.0, we unified the way we both build and distribute our .NET tools, so that they share a common install. This has many benefits, from licensing to sharing in-memory caches between products. So not only is it the ReSharper 9.1 EAP, but it’s the EAP for dotCover 3.1, dotTrace 6.1, dotMemory 4.3 and dotPeek 1.4.

With EAP03, we’re introducing support for .NET Core to ReSharper Ultimate. This means you can get code coverage and performance and memory profiling for your .NET Core apps.

But let’s back up. What’s .NET Core?

This is Microsoft’s new, Open Source, cross platform .NET stack. It is the next generation of .NET, designed to replace the need for Portable Class Libraries by providing a common, modular .NET implementation across all platforms, including ASP.NET vNext. (One slightly confusing part is that it doesn’t replace the .NET Framework. This will still be Microsoft’s .NET stack on the Windows desktop, and will remain in use, and supported for both compatibility reasons and to support desktop apps, such as WPF.)

It’s made up of a couple of pieces – the CoreCLR project is the runtime, containing the JIT and the Garbage Collector, and the Base Class Library is being reimplemented in the CoreFX project.

One of the nice things about a .NET Core application is that it is xcopy deployable. That is, .NET Core apps don’t require an install, or a GAC, or anything. All of the files required to run an application can live in a single folder.

Explorer showing the files in the HelloWorld sample

This test application is the HelloWorld app from the CoreFX Lab repo. As we can see, the folder contains everything we need to run the application. The BCL System assemblies are present, perhaps a few more than you’re used to on the desktop – this allows for a more modular distribution of the BCL, only distributing the assemblies you use. You can also see mscorlib.dll, which provides the primitive types, such as Object and String, and coreclr.dll, which is the actual runtime, including the JIT and the garbage collector. Most interestingly, we have three other files – HelloWorld.dll, CoreRun.exe and HelloWorld.exe.

dotPeek showing decompiled code

If we take a look at these files in dotPeek, we can see that the System assemblies and HelloWorld.dll are just normal looking assemblies, although they’re now targeting .NET Core v4.5, rather than the .NET Framework. Furthermore, we can see that HelloWorld.dll contains our Program class, with a normal static Main method – this is the entry point to the application. And coreclr is listed as “not supported”, as we know that’s a dll containing the native code for the runtime itself.

So what are HelloWorld.exe and CoreRun.exe?

These files are also coming up as “not supported” in dotPeek, and if we look at the their file sizes, we see that they’re both very small, only about 40Kb each. These files are native stubs, and are used to bootstrap the .NET Core runtime and launch the .NET application. The stub will load coreclr.dll, initialize the runtime and point it to the managed code to run.

The CoreRun.exe process is a generic launcher that requires the HelloWorld.dll to be passed in as a command line parameter, and provides extra command line parsing to enable attaching a debugger, and providing verbose output. The HelloWorld.exe process is simpler, and requires no command line parameters, automatically launching HelloWorld.dll and passing any command line parameters to the .NET application.

These stubs are required for .NET Core primarily to allow for running cross platform. Traditional .NET Framework applications combine the bootstrap stub and the .NET application code and metadata (HelloWorld.dll) so the .NET application just works transparently (in fact, versions of Windows later than Windows XP recognize .NET applications at the Operating System level, and bootstrap the .NET Framework automatically). But if we want to run the HelloWorld .NET application on OS X or Linux, then we can’t just run HelloWorld.exe – other operating systems don’t know how to launch .exe files, and might not even be Intel-based! So on other operating systems, the stub can be replaced with another executable, without having to replace the .NET application.

Image of HelloWorld console application running

We can dive deeper into .NET Core applications, by profiling and analyzing them at runtime.

We can use dotCover to provide code coverage – note that DrawWindows is being called, but not DrawLinux or DrawMac. (While .NET Core is cross-platform, ReSharper Ultimate is a set of .NET Framework applications, using WPF, and as such, tied to Windows.)

dotCover showing coverage for the HelloWorld application

Performance profiling is handled by dotTrace, which can provide a nice timeline view of the performance of a .NET Core application, with powerful filtering capabilities to filter based on CPU, CPU state, thread and so on. Hopefully you’ll be working on slightly more interesting applications!

dotTrace showing performance information about HelloWorld application

And finally, dotMemory will allow you to examine the memory allocations of .NET Core apps, too.

dotMemory analysing memory snapshot of HelloWorld application

All of this support for .NET Core is handled transparently by ReSharper Ultimate. You simply need to start profiling or coverage for a .NET application, passing in either of the stubs as the startup application, and ReSharper Ultimate will do the rest.

Also, this is initial support. There are a few things that aren’t working correctly just now, but we’re working on them, and they should be fixed in future EAP builds.

Grab yourself a copy of the latest EAP and give it a go. There’s more to come. Stay tuned!

Posted in dotCover Tips&Tricks, dotMemory Tips&Tricks, dotPeek Tips&Tricks, dotTrace Tips&Tricks, How-To's | Tagged , , , , , , , | 12 Comments

JavaScript Support in ReSharper 9

ReSharper 9 comes with plenty of JavaScript-related changes. We’ve already mentioned some of them in our TypeScript blog post and, additionally, we have a blog post on converting JavaScript to TypeScript, but now we are going to focus on JavaScript proper. So, what does ReSharper 9 have in store?

Tools like JSHint and JSLint are increasingly popular nowadays. Their inspections are great, but sometimes you want not just to know where the error is, but also to be able to fix it, and you want to see the error as you write code, not as a post-build step. ReSharper is here to help: we have already implemented several inspections similar to JSHint, for example, the variable and function naming inspections and inspections for using undeclared global variables, and we offer quick-fixes for them. ReSharper 9 offers additional JSHint-inspired inspections and quick-fixes for them:

  • Assignment inside conditional expression, with a quick-fix to change = to ===.Assignment inside conditional expressionJust in case you do want to perform assignments in conditional statements, ReSharper gives you an option to indicate such expressions by wrapping the assignee with an extra set of round braces. There’s even a fix for this:

    Wrapping the assignee with an extra set of round braces quick-fix

    The option to ignore based on redundant braces is configurable: you can find it in ReSharper | Options | Code Editing | JavaScript | Inspections.

  • Constructor call not used or possibly used for side effects — we provide a quick-fix for replacing it with a function call.
    Quick-fix for replacing constructor call with a function call
  • Caller/callee usings (warning; error in strict mode).
    Caller/callee usings
  • Coerced usage of equals (use of ==/!= instead of ===/!==) with a quick-fix to perform appropriate replacements.Coerced usage of equals code inspection
  • Extending/overwriting prototype of native objectExtending/overwriting prototype of native object
  • Variable/function used before it is declared with a quick-fix to normalize declarations.
  • Variable/function used outside of block where it is declared, again with a suitable quick-fix.Variable/function used outside of block where it is declared
  • Local function is redefined laterLocal function is redefined later
  • Incorrect argument comparison with typeof expression with a quick-fix to replace with the correct value.Incorrect argument comparison with typeof expression with a quick-fix to replace with the correct value
  • Closure on a variable modified in loop of outer scopeClosure on a variable modified in loop of outer scope
  • There is no .hasOwnProperty() check in a body of for in loop — this will cause an enumeration of prototype properties. ReSharper provides a quick-fix to surround the body with a .hasOwnProperty() check.Quick-fix to surround the body with a .hasOwnProperty() check

Also, ReSharper supports JSHint’s /* global ... */ annotations for global variables:

Support for annotations for global variables

In addition to the above features, ReSharper 9.1 EAP (which you can download here) comes with initial support for ECMAScript 6. This includes support for destructuring, let and const declarations, template strings, lambdas, and a lot more. This support is early-stage, but we do plan to fully support ECMAScript 6 in due time. Check out the EAP for how these features are shaping up, but do bear in mind that VS2015 CTP will highlight correct ECMAScript 6 code as errors in many situations. We just have to give it a bit more time to catch up. 

Posted in How-To's, ReSharper Tips&Tricks | Tagged , , | 4 Comments

Unit Testing and Memory Profiling: Can They Be Combined?

Memory profilers can hardly be called an “everyday tool.” Typically, developers start thinking about profiling their product closer to its release. This approach may work fine until some last-minute issue like a leak or huge memory traffic crushes all your deadlines. The proactive approach would be to profile your app’s functionality on a daily basis, but who’s got the resources to do that? Well, we think there may be a solution.

If you employ unit testing in your development process, it is likely that you regularly run a number of tests on app logic. Now imagine that you can write some special “memory profiling” tests, e.g. a test that identifies leaks by checking memory for objects of particular type, or a test that tracks memory traffic and fails in case the traffic exceeds some threshold. This is exactly what dotMemory Unit framework allows you to do. The framework is distributed as a NuGet package and can be used to perform the following scenarios:

  • Checking memory for  objects of a certain type.

  • Checking memory traffic.

  • Getting the difference between memory snapshots.

  • Saving memory snapshots for further investigation in dotMemory (a standalone .NET memory profiler from JetBrains).

In other words, dotMemory Unit extends your unit testing framework with the functionality of a memory profiler.

IMPORTANT: dotMemory Unit is currently in the EAP (Early Access Program) stage. Please use it for evaluation purposes only!

How It Works

  • dotMemory Unit is distributed as a NuGet package installed to your test project:
    PM> Install-Package JetBrains.DotMemoryUnit -pre

  • dotMemory Unit requires ReSharper unit test runner. To run tests that use dotMemory Unit, you should have either dotCover 3.1 EAP or ReSharper 9.1 EAP05 installed on your system.

  • After you install the dotMemory Unit package, ReSharper’s menus for unit tests will include an additional item, Run Unit Tests under dotMemory Unit. In this mode, the test runner will execute dotMemory Unit calls as well as ordinary test logic. If you run a test the ‘normal’ way (without dotMemory Unit support), all dotMemory Unit calls will be ignored.

    Unit Tests Menu

  • dotMemory Unit works with all of the unit-testing frameworks supported by ReSharper’s unit test runner including MSTest and NUnit.

  • A standalone launcher for integrating with CI systems like JetBrains TeamCity is planned for future releases.

Now let’s take a look at some examples to better understand what dotMemory Unit does.

Example 1: Checking for Specific Objects

Let’s start with something simple. One of the most useful cases can be finding a leak by checking memory for objects of a specific type.

GetObjects Assertion

  1. A lambda is passed to the Check method of the static dotMemory class. This method will be called only in case you run the test using Run test under dotMemory Unit.

  2. The memory object passed to the lambda contains all memory data for the current execution point.

  3. The GetObjects method returns a set of objects that match the condition passed in another lambda. This line slices the memory by leaving only objects of the Foo type. The Assert expression asserts that there should be 0 objects of the Foo type.
    Note that dotMemory Unit does not force you to use any specific Assert syntax. Simply use the syntax of the framework your test is written for. For example, the line in the example uses NUnit syntax but could be easily updated for MSTest:
    MSTest Assertion

With dotMemory Unit you can select a set of objects by almost any condition, get the number of objects in this set and their size, and use these data in your assertions.
In the following example, we check for objects in the large object heap:

Checking for Specific Objects

 

Example 2: Checking Memory Traffic

The test for checking memory traffic is even simpler. All you need do to is mark the test with the AssertTraffic attribute. In the example below, we assert that the amount of memory allocated by all the code in TestMethod1 does not exceed 1,000 bytes.

AssertTraffic Attribute Example

Example 3: Complex Scenarios for Checking Memory Traffic

If you need to get more complex information about memory traffic (say, check for traffic of objects of a particular type during some specific time interval), you can use a similar approach to the one from the first example. The lambdas passed to the dotMemory.Check method slice and dice traffic data by various conditions.

Check Traffic with Traffic Type

  1. To mark time intervals where memory traffic can be analyzed, use checkpoints created by dotMemory.Check (as you probably guessed, this method simply takes a memory snapshot).

  2. The checkpoint that defines the starting point of the interval is passed to the GetTrafficFrom method.
    For example, this line asserts that the total size of objects implementing the IFoo interface created in the interval between memoryCheckPoint1 and memoryCheckPoint2 is less than 1,000 bytes.

  3. You can get traffic data for any checkpoint that was set earlier. Thus, this line gets traffic between the current dotMemory.Check call and memoryCheckPoint2.

Example 4: Comparing Snapshots

Like in the ‘standalone’ dotMemory profiler, you can use checkpoints not only to compare traffic but for all kinds of snapshot comparisons. In the example below we assert that no objects from the MyApp namespace survived garbage collection in the interval between memoryCheckPoint1 and the second dotMemory.Check call.

Compare Snapshots

Conclusion

dotMemory Unit is very flexible and allows you to check almost any aspect of app memory usage. Use “memory” tests in the same way as unit tests on app logic:

  • After you manually find an issue (such as a leak), write a memory test that covers it.

  • Write tests for proactive testing – to ensure that new product features do not create any memory issues, like objects left in memory or large traffic.

Thanks for reading and don’t hesitate to try dotMemory Unit EAP on your own! It’s absolutely free, and the only requirement is ReSharper or dotCover installed on your machine.

Posted in dotMemory Tips&Tricks, How-To's, News and Events | Tagged , | 10 Comments

String Interning: Effective Memory Management with dotMemory

Starting with version 4.1, dotMemory offers the String duplicates inspection. The idea behind it is quite simple: it automatically checks memory for string objects with the same value. After you open a memory snapshot, you will see the list of such strings:

String duplicates inspection in dotMemory

How can this help? Well, string duplicates possibly indicate ineffective memory usage. Why create a new string if it is already in memory?

Imagine, for example, that in the background your app parses some text files with repetitive content (say, some XML logs).

Code example for log file processing

So, dotMemory finds a lot of strings with identical content. What can we do?

Inspection results for the example

The obvious answer – rewrite our app so that it allocates strings with unique content just once. Actually, there are at least two ways this can be done. The first one is to use the string interning mechanism provided by .NET.

CLR Intern Pool

.NET automatically performs string interning for all string literals. This is done by means of an intern pool – a special table that stores references to all unique strings. But why  aren’t the strings in our example interned? The thing is that only explicitly declared string literals are interned on the compile stage. The strings created at runtime are not checked for being already added to the pool. For example:

Interning example

Of course, you can circumvent this limitation by working with the intern pool directly. For this purpose, .NET offers two methods: String.Intern and String.IsInterned. If the string value passed to String.Intern is already in the pool, the method returns the reference to the string. Otherwise, the method adds the string to the pool and returns the reference to it. If you want to just check if a string is already interned, you should use the String.IsInterned method. It returns the reference to the string if its value is in the pool, or null of it isn’t.

Thus, the fix for our log parsing algorithm could look as follows:

CLR interning example

Further memory profiling will show that strings are successfully interned.

Inspection after the fix

Nevertheless, such an implementation has one rather serious disadvantage – the interned strings will stay in memory “forever” (or, to be more correct, they will persist for the lifetime of AppDomain, as the intern pool will store references to the strings even if they are no longer needed).

If, for example, our app has to parse a large number of different log files, this could be a problem. In such a case, a better solution would be to create a local analogue of the intern pool.

Local Intern Pool

The simplest (though very far from optimal) implementation might look like this:

Local pool code example

The processing algorithm will change a little bit as well:

Local pool example

In this case, pool will be removed from memory with the next garbage collection after ProcessLogFile is done working.

Thanks for reading! We hope this post was helpful. If you want to try dotMemory and the full set of its automatic inspections on your code, just download your free 5-day trial here.

Posted in dotMemory Tips&Tricks, How-To's | Tagged , | 10 Comments

Ways and Advantages of Migrating JavaScript Code to TypeScript

Nowadays, more and more new languages are created with the aim of providing an alternative to JavaScript. One of these languages is TypeScript. It is compiled to readable JavaScript code; its main goals are to perform type checking at compile time and to support the object-oriented paradigm. In this post we will not discuss the pros and cons of concrete languages or type systems. Instead, we’ll describe an easy way of performing migrations to TypeScript, and demonstrate how the conversion from JavaScript to TypeScript helps discover interesting artifacts (potential errors) as well as making your code clearer.

Ways Of Migrating

In the JavaScript world, people like to use patterns to represent entities, but unfortunately, these are not supported on the language level with constructs such as classes with inheritance, modules and other sorts of abstraction. There are lots of ways to implement these patterns. Sometimes, this abundance of mechanisms for emulating classical OOP constructs leads to confusion. On the other hand, TypeScript explicitly supports these abstractions in its syntax. This is why the process of converting JavaScript to TypeScript makes your code much clearer. The most straightforward way is, of course, manual conversion (see, e.g., this article), but that takes a lot of time and resources. In this post we try using a semi-automatic tool for this and attempt to assess its advantages and disadvantages.

Using a Semi-Automatic Tool

We took some real-world industrial code: bits of JavaScript that are part of node.js framework. It is about 25k lines of JavaScript code with classes and other constructs. For migration to TypeScript, we used a new ability of ReSharper 9, which performs the transformation of the provided JavaScript code. This feature is capable of handling the following JavaScript patterns:

Internal modules
Immediately-invoked function expressions
Immediately-invoked function expressions with namespace object passed as an argument
External modules
AMD format
CommonJs format
Classes and inheritance
Prototype.js
Native class implementation
Misc
Function expression to lambda
Member assigned by a function expression to function member
Usage of parent class via invocation of call/apply to keyword super
Creating dynamic property to ambient declaration
Recovering types
Infer possible overload signatures by function body/vsDoc

In general, support for these patterns allows ReSharper to very quickly produce an OOP baseline implementation. This is the tool’s main advantage. As a result we get approximate TypeScript code, which requires some error correction before it can be put into production.

So, what errors did we get after performing the conversion? We got about 1,200 errors and, after a certain amount of classification (thanks to ReSharper’s Solution-Wide Analysis) we managed to pinpoint these particular types of errors:

  • “Cannot resolve symbol” and other related errors – 70% of cases

  • “Supplied parameters do not match any signature of call target” – 25% of cases

  • Other cases – 5%

The occurrence of the “lack of matched signature” errors is related to the fact that the inference of overload signatures is not always possible statically without special annotation libraries. See example below.

Original JavaScript code:

JavaScript to TypeScript converter: original JavaScript code

Generated TypeScript code:

JavaScript to TypeScript converter: generated TypeScript code

But this is not the only source of problems. The next example illustrates a more frequent situation:

 

You may notice that the fail function does not declare optional parameters. That is why ReSharper is unable to infer the overload signature with optional parameters. This code can be confusing, and may cause errors.

The most frequent type of error, however, is the inability to resolve a particular symbol, as well as related issues. These also appear because of dynamic nature of the language:

In general, these sorts of errors are expected. At the moment, it is the developer’s responsibility to fix these errors but, in the future, ReSharper might be able to resolve them automatically.

Errors from the other category are more interesting. Some of them shows that the lack of common convention leads to confusing. The next example shows a typical deviation from convention CommonJs modules.

Declaration of Buffer as exported variable:

Usage of the Buffer symbol, which was not imported:

Symbol Buffer is global type, by design, of node.js, but it is not obvious by code.

The next example demonstrates multiple inheritance. If your parent classes have properties with the same name and you don’t notice it, it obviously leads to undefined behavior. The compiler can’t help you either. And if you use multiple inheritance in code, you will have to replace it on composition yourself. TypeScript doesn’t support it.

Original JavaScript code:

multiple inheritance in JavaScript code

Generated Ts code:

multiple inheritance in generated TypeScript code

How To Perform Conversion?

Obsolete code can be converted with ReSharper’s highlightings and the corresponding quick-fixes. There are different granularities for conversion:

  • Element under caret
  • In file
  • In folder
  • In project
  • In solution

Converting JavaScript to TypeScript with ReSharper

In conclusion, this tool speeds up the process of converting JavaScript to TypeScript. Give it a try!

Posted in How-To's, ReSharper Tips&Tricks | Tagged , , | 10 Comments