Code Monkey home page Code Monkey logo

ruby_perf_counters's Introduction

PerfCounters

Experimental, do not use in production, could blow up your Rubies.

What

Read the CPU's performance counters from Ruby using perf_event_open(2).

Installation

$ gem install perf_counters

Usage

require 'perf_counters'
events = [
  Event::INSTRUCTIONS,
  Event::CPU_CYCLES,
  Event::CACHE_MISSES,
]
perf = PerfCounters::Measurement.new(
  exclude_kernel: true,
  events: events,
)
perf.start
# do something here
perf.stop
=> {:instructions=>3276, :cpu_cycles=>18651, :cache_misses=>24}

Sample Rack middleware in a Rails app

class PerformanceCountersRackMiddleware
  def initialize app
    @app = app
  end

  def call env
    events = [
      Event::INSTRUCTIONS,
      Event::CPU_CYCLES,
      Event::CACHE_MISSES,
      Event::CONTEXT_SWITCHES,
      Event::BUS_CYCLES,
      Event::PAGE_FAULTS_MIN,
    ]
    perf_data = PerfCounters.measure(exclude_kernel: true, events: events) do
      @status, @headers, @response = @app.call(env)
    end

    Rails.logger.info "#{perf_data}"

    [@status, @headers, @response]
  end
end

For 2 request it outputs something like:

{:instructions=>93930165, :cpu_cycles=>69263494, :cache_misses=>101678, :context_switches=>0, :bus_cycles=>2663999, :page_faults_min=>216}
[...]
Completed 200 OK in 71ms (Views: 54.7ms)


{:instructions=>93984918, :cpu_cycles=>74644389, :cache_misses=>116050, :context_switches=>0, :bus_cycles=>2870920, :page_faults_min=>246}
[...]
Completed 200 OK in 50ms (Views: 38.2ms)

Contributing

Requirements

A modern Linux machine (unfortunately, most VMs don't virtualize perf counters) with perf installed.

Install

$ bin/setup

Compile

$ rake compile

Run tests

$ rake [test]

ruby_perf_counters's People

Contributors

javierhonduco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.