Skip to content

Instantly share code, notes, and snippets.

@luk3thomas
Last active March 27, 2018 03:30
Show Gist options
  • Save luk3thomas/7cfe7370a27e555610e9 to your computer and use it in GitHub Desktop.
Save luk3thomas/7cfe7370a27e555610e9 to your computer and use it in GitHub Desktop.

Revisions

  1. luk3thomas revised this gist Mar 18, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion send-stats.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    // 1. Go to chrome://memory-redirect/
    // 2. Paste this script into the developer console

    (function(){
    var SERVER = 'http://localhost:4567/',
    var SERVER = 'http://localhost:4567/', // Post metrics to any server
    STRINGS = 'chrome://memory-redirect/strings.js',
    INTERVAL = 10e3,
    RE_INCLUDE = /./; // only include certain results, if you want.
  2. luk3thomas revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion send-stats.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    var SERVER = 'http://localhost:4567/',
    STRINGS = 'chrome://memory-redirect/strings.js',
    INTERVAL = 10e3,
    RE_INCLUDE = /librato/i;
    RE_INCLUDE = /./; // only include certain results, if you want.

    function getMemoryStats() {
    console.log('Getting stats.')
  3. luk3thomas revised this gist Mar 18, 2016. 2 changed files with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    source 'https://rubygems.org'
    gem 'sinatra'
    gem 'librato-metrics'
    File renamed without changes.
  4. luk3thomas revised this gist Mar 18, 2016. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions poller.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    (function(){
    var SERVER = 'http://localhost:4567/',
    STRINGS = 'chrome://memory-redirect/strings.js',
    INTERVAL = 10e3,
    RE_INCLUDE = /librato/i;

    function getMemoryStats() {
    console.log('Getting stats.')
    xhr({ method: 'GET', url: 'chrome://memory-redirect/strings.js', callback: sendMemoryStats })
    xhr({ method: 'GET', url: STRINGS, callback: sendMemoryStats })
    }

    function sendMemoryStats(req){
    eval(req.responseText.replace(/loadTimeData./, ''))
    xhr({ method: 'POST', url: 'http://localhost:4567/', data: formatData(data) })
    xhr({ method: 'POST', url: SERVER, data: formatData(data) })
    }

    function xhr(opts){
    @@ -39,7 +44,7 @@
    return memory;
    })
    .filter(function(item){
    return /librato/i.test(item.source)
    return RE_INCLUDE.test(item.source)
    })
    });
    }
    @@ -63,5 +68,5 @@

    console.info('Start collecting memory stats...')
    getMemoryStats()
    setInterval(getMemoryStats, 10e3)
    setInterval(getMemoryStats, INTERVAL)
    })()
  5. luk3thomas revised this gist Mar 18, 2016. 2 changed files with 82 additions and 48 deletions.
    101 changes: 59 additions & 42 deletions poller.js
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,67 @@
    (function(){
    function getMemoryStats() {
    makeXHR({ method: 'GET', url: 'chrome://memory-redirect/strings.js', callback: sendMemoryStats })
    }
    function getMemoryStats() {
    console.log('Getting stats.')
    xhr({ method: 'GET', url: 'chrome://memory-redirect/strings.js', callback: sendMemoryStats })
    }

    function sendMemoryStats(xhr){
    eval(xhr.responseText.replace(/loadTimeData./, ''))
    makeXHR({ method: 'POST', url: 'http://localhost:4567/', data: formatData(data) })
    }
    function sendMemoryStats(req){
    eval(req.responseText.replace(/loadTimeData./, ''))
    xhr({ method: 'POST', url: 'http://localhost:4567/', data: formatData(data) })
    }

    function makeXHR(opts){
    var xhr, type, url, callback;
    method = opts.method;
    url = opts.url;
    callback = opts.callback || function(){};
    data = opts.data;
    xhr = new XMLHttpRequest()
    xhr.open(method, url, true)
    xhr.onreadystatechange = function(){
    if (xhr.readyState === 4 && xhr.status === 200) {
    callback(xhr)
    }
    }
    xhr.send(data)
    function xhr(opts){
    var req, type, url, callback;
    method = opts.method;
    url = opts.url;
    callback = opts.callback || function(){};
    data = opts.data;
    req = new XMLHttpRequest()
    req.open(method, url, true)
    req.onreadystatechange = function(){
    if (req.readyState === 4 && req.status === 200) {
    callback(req)
    }
    }
    req.send(data)
    }

    function formatData(data){
    var formatted;
    formatted =
    data.jstemplateData.child_data.map(function(item){
    return {
    value: item.ws_priv * 1000,
    source: formatSource(item.child_name, item.titles.slice(-1)[0])
    }
    })
    return JSON.stringify(formatted);
    }
    function formatData(data){
    return JSON.stringify({
    browsers: data.jstemplateData.browsers.map(function(browser){
    memory = getMemory(browser);
    memory.source = formatSource(browser.name);
    return memory;
    }),
    processes: data.jstemplateData.child_data
    .map(function(item){
    memory = getMemory(item);
    memory.source = formatSource(item.child_name, item.titles.slice(-1)[0]);
    return memory;
    })
    .filter(function(item){
    return /librato/i.test(item.source)
    })
    });
    }

    function formatSource() {
    var args = [].slice.call(arguments);
    return args.join(" ")
    .toLowerCase()
    .replace(/[^a-z\- ]/g, '')
    .replace(/ [ ]*/g, ' ')
    .replace(/ /g, '.')
    .replace(/\.$/g, '')
    }
    function getMemory(measurement) {
    return {
    virtual: measurement.comm_priv * 1000,
    private: measurement.ws_priv * 1000
    };
    }

    function formatSource() {
    var args = [].slice.call(arguments);
    return args.join(" ")
    .toLowerCase()
    .replace(/[^a-z\- ]/g, ' ')
    .replace(/ [ ]*/g, ' ')
    .replace(/ /g, '.')
    .replace(/\.$/g, '')
    }

    setInterval(getMemoryStats, 5e3)
    console.info('Start collecting memory stats...')
    getMemoryStats()
    setInterval(getMemoryStats, 10e3)
    })()
    29 changes: 23 additions & 6 deletions server.rb
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    require 'librato/metrics'

    Librato::Metrics.authenticate ENV['email'], ENV['token']
    Librato::Metrics.api_endpoint = ENV['api']

    post '/' do
    queue = Librato::Metrics::Queue.new
    @@ -12,12 +11,30 @@
    "Access-Control-Allow-Headers" => "*",
    "Access-Control-Allow-Methods" => "*",
    "Access-Control-Allow-Origin" => "*"
    measurements = JSON.parse(request.body.read)
    measurements.each do |measurement|
    queue.add 'browser.memory.private' => {
    value: measurement['value'],
    source: measurement['source']

    req = JSON.parse(request.body.read)

    req['processes'].each do |item|
    queue.add 'chrome.process.memory.private' => {
    value: item['private'],
    source: item['source']
    }
    queue.add 'chrome.process.memory.virtual' => {
    value: item['virtual'],
    source: item['source']
    }
    end

    req['browsers'].each do |item|
    queue.add 'chrome.memory.private' => {
    value: item['private'],
    source: item['source']
    }
    queue.add 'chrome.memory.virtual' => {
    value: item['virtual'],
    source: item['source']
    }
    end

    queue.submit
    end
  6. luk3thomas revised this gist Mar 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion poller.js
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@
    formatted =
    data.jstemplateData.child_data.map(function(item){
    return {
    value: item.ws_priv,
    value: item.ws_priv * 1000,
    source: formatSource(item.child_name, item.titles.slice(-1)[0])
    }
    })
  7. luk3thomas created this gist Mar 17, 2016.
    3 changes: 3 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    source 'https://rubygems.org'
    gem 'sinatra'
    gem 'librato-metrics'
    50 changes: 50 additions & 0 deletions poller.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    (function(){
    function getMemoryStats() {
    makeXHR({ method: 'GET', url: 'chrome://memory-redirect/strings.js', callback: sendMemoryStats })
    }

    function sendMemoryStats(xhr){
    eval(xhr.responseText.replace(/loadTimeData./, ''))
    makeXHR({ method: 'POST', url: 'http://localhost:4567/', data: formatData(data) })
    }

    function makeXHR(opts){
    var xhr, type, url, callback;
    method = opts.method;
    url = opts.url;
    callback = opts.callback || function(){};
    data = opts.data;
    xhr = new XMLHttpRequest()
    xhr.open(method, url, true)
    xhr.onreadystatechange = function(){
    if (xhr.readyState === 4 && xhr.status === 200) {
    callback(xhr)
    }
    }
    xhr.send(data)
    }

    function formatData(data){
    var formatted;
    formatted =
    data.jstemplateData.child_data.map(function(item){
    return {
    value: item.ws_priv,
    source: formatSource(item.child_name, item.titles.slice(-1)[0])
    }
    })
    return JSON.stringify(formatted);
    }

    function formatSource() {
    var args = [].slice.call(arguments);
    return args.join(" ")
    .toLowerCase()
    .replace(/[^a-z\- ]/g, '')
    .replace(/ [ ]*/g, ' ')
    .replace(/ /g, '.')
    .replace(/\.$/g, '')
    }

    setInterval(getMemoryStats, 5e3)
    })()
    23 changes: 23 additions & 0 deletions server.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    require 'sinatra'
    require 'json'
    require 'librato/metrics'

    Librato::Metrics.authenticate ENV['email'], ENV['token']
    Librato::Metrics.api_endpoint = ENV['api']

    post '/' do
    queue = Librato::Metrics::Queue.new
    headers \
    "Access-Control-Allow-Credentials" => "true",
    "Access-Control-Allow-Headers" => "*",
    "Access-Control-Allow-Methods" => "*",
    "Access-Control-Allow-Origin" => "*"
    measurements = JSON.parse(request.body.read)
    measurements.each do |measurement|
    queue.add 'browser.memory.private' => {
    value: measurement['value'],
    source: measurement['source']
    }
    end
    queue.submit
    end